TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Advanced PHP Programming (http://www.talkphp.com/advanced-php-programming/)
-   -   Need to output MYSQL as array(1 => 'January', 'feb', 'March'); (http://www.talkphp.com/advanced-php-programming/4749-need-output-mysql-array-1-january-feb-march.html)

russellharrower 07-17-2009 12:46 PM

Need to output MYSQL as array(1 => 'January', 'feb', 'March');
 
Hi I need to be able to print the output of the mysql request ($quarry) into a array(1 => 'January', 'feb', 'March');

But instead of January, feb and March it needs to have the title output.

If you can help that would be great.

Thanks
Code:

$query = "SELECT id, title, content FROM push_content WHERE type = 0 ORDER by date DESC LIMIT 1";


if ($result = $mysqli->query($query)) {

    /* fetch associative array */
    while ($obj = mysqli_fetch_object($result)) {
        printf ("%s (%s)\n", $obj->title, $obj->content);
                echo"<br/>";
    }

    /* free result set */
    mysqli_free_result($result);


tony 07-17-2009 02:40 PM

why don't you use the fetch associative array static method instead of the mysqli_fetch_object?

russellharrower 07-17-2009 02:45 PM

because i need the ability to call each title by them self.
if you can do an example on how i would do that using that code, that would be helpful.

xenon 07-17-2009 05:15 PM

If you go object oriented style, keep it that way:

PHP Code:

$query "SELECT id, title, content FROM push_content WHERE type = 0 ORDER by date DESC LIMIT 1";


if (
$result $mysqli->query($query)) {

    
/* fetch associative array */
    
while ($obj $result->fetch_object()) {
        
printf ("%s (%s)\n"$obj->title$obj->content);
        echo
"<br/>";
    }


What exactly are you trying to do, though? You may want to give some examples, as well, so we can figure out how to help you. I, for one, did not understand how using an object instead of an array help you.

russellharrower 07-18-2009 02:50 PM

Quote:

Originally Posted by xenon (Post 27150)
If you go object oriented style, keep it that way:

PHP Code:

$query "SELECT id, title, content FROM push_content WHERE type = 0 ORDER by date DESC LIMIT 1";


if (
$result $mysqli->query($query)) {

    
/* fetch associative array */
    
while ($obj $result->fetch_object()) {
        
printf ("%s (%s)\n"$obj->title$obj->content);
        echo
"<br/>";
    }


What exactly are you trying to do, though? You may want to give some examples, as well, so we can figure out how to help you. I, for one, did not understand how using an object instead of an array help you.

Thanks xexon,
I want to be able to call each title like
$title1 = "this is title 1";
$title2 = "this is title2";
$title3 = "this is title3";
etc...

As I want to allow the template designer to choose how many stories to show on the front page in there design.

I also want to show the content this way.
I then want to come up with a way to have global plugins that will allow the users to choose if they want to show the posters name, date published etc...

I hope that helps.

Thanks

adamdecaf 07-18-2009 04:58 PM

Well, this would be easier.

PHP Code:

$query "SELECT id, title, content FROM push_content WHERE type = 0 ORDER by date DESC LIMIT 1"
$data = Array("title""content");
$n 0;

if (
$result $mysqli->query($query)) { 

    
/* fetch associative array */ 
    
while ($obj $result->fetch_object()) { 
        
$data['title'][$n] = $obj->title;
        
$data['content'][$n] = $obj->content;
        
$n++;
    } 


As for the option to display content I would store that into the DB and then only query for the ones that are allowed for the situation.

russellharrower 07-18-2009 05:02 PM

Quote:

Originally Posted by adamdecaf (Post 27160)
Well, this would be easier.

PHP Code:

$query "SELECT id, title, content FROM push_content WHERE type = 0 ORDER by date DESC LIMIT 1"
$data = Array("title""content");
$n 0;

if (
$result $mysqli->query($query)) { 

    
/* fetch associative array */ 
    
while ($obj $result->fetch_object()) { 
        
$data['title'][$n] = $obj->title;
        
$data['content'][$n] = $obj->content;
        
$n++;
    } 


As for the option to display content I would store that into the DB and then only query for the ones that are allowed for the situation.

Hi Just wondering what if i need to show more then 1 say like 10

adamdecaf 07-18-2009 05:07 PM

Quote:

Originally Posted by russellharrower (Post 27161)
Hi Just wondering what if i need to show more then 1 say like 10

PHP Code:

for ($n 0$n <= 9$n++) {
   echo 
"<p><strong>" $data['title'][$n] . "</strong><br />";
   echo 
$data['content'][$n] . "</p>";


Something like that...

russellharrower 07-18-2009 05:11 PM

But i need to give each one a $title->1, $title->2 etc that code will only print the data

adamdecaf 07-18-2009 05:59 PM

Quote:

Originally Posted by russellharrower (Post 27164)
But i need to give each one a $title->1, $title->2 etc that code will only print the data

Why will
PHP Code:

$data['title'][$n

not work?

russellharrower 07-19-2009 06:20 AM

have a look what that does http://www.designvisa.com/beta

adamdecaf 07-19-2009 06:41 PM

Quote:

Originally Posted by russellharrower (Post 27169)
have a look what that does http://www.designvisa.com/beta

The page is empty.

Village Idiot 07-19-2009 08:45 PM

Quote:

Originally Posted by russellharrower (Post 27169)
have a look what that does http://www.designvisa.com/beta

I'm not sure objects are required for that.

russellharrower 07-20-2009 05:51 AM

http://www.designvisa.com/beta/index2.php sorry about that


All times are GMT. The time now is 05:47 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0