tags:

views:

310

answers:

6

How can i convert mysql results (from mysql_fetch_array) into such a form?

$some = array(
     "comments" => array(
         array( "text" => "hi", "id" => "1" ),
         array( "text" => "hi", "id" => "2" ),
         array( "text" => "hi", "id" => "3" ),
         array( "text" => "hi", "id" => "4" )
    )
);

while the db looks like:

comments

id  text
1   blabla bla
2   bla bla

i've tried to fetch the values with foreach/while and insert it into two arrays but no success...

$some = array(
    "comments" => array()
);

$q = $mysql->sf('*', TBL_QST);
foreach($mysql->fetch($q) as $row) {
    $some[] = $row;
    // $some["comments"][] = $row;
}
A: 

Here's what I would do:

$dbc = mysql_connect(SERVER, USERNAME, PASSWORD);
mysql_select_db($dbc, DATABASE);

$query = "SELECT `id`, `text` FROM `comments`";
$result = mysql_query($dbc, $query);

$some = Array("comments" => Array());
while($row = mysql_fetch_array($result)) {
    array_push($some['comments'], Array($row['id'], $row['text']));
}
Christian Mann
you forgot to close the bracket...and there is no "comments" row in the array..so it doesn't work...i have already tried with push and adding the "comments" into array...still doesn't work
cthulhu
Oh, oops. Misread where you wanted the data. Let me edit my question accordingly...
Christian Mann
A: 

Use mysql_fetch_assoc() instead, or use MYSQL_ASSOC as second argument of mysql_fetch_array().

draganHR
A: 
$some['comments'][] = ...

inside a loop

zerkms
that doesn't work...
cthulhu
what is "doesn't work"? show actual results given with this line (var_dump($some))
zerkms
if i commented this (line), that means it doesn't work...i already checked this with var_dump and as i said it didn't give me the results i wanted...however the problem is solved so thanks...
cthulhu
A: 

What you want is an associative array, and for this reason a special function has been already developed in PHP which is called mysql_fetch_assoc().

Updated
I am sorry, I understood you wrong. In this case you need this block of code below.

$sql="select id,text from comments";
$res=mysql_query($sql);
$arr=array();
while($row=mysql_fetch_assoc($res)){
     $arr[]=array('id'=>$row['id'],'text'=>$row['text']);
}
$some=array('comments'=>$arr);
Bakhtiyor
$qst = array( "questions" => array());$q = $mysql->sf('*', TBL_QST);foreach(mysql_fetch_assoc($q) as $row) { $qst["questions"] = $row; // or $qst["questions"][] = $row;}doesn't work...
cthulhu
See the updated section of my answer. I tested that block of code and it works perfectly. Final result is the array $some.
Bakhtiyor
+2  A: 

My print_r gives:

Array
(
    [comments] => Array
        (
            [0] => Array
                (
                    [text] => lorem ipsum
                    [id] => 0
                )
            [1] => Array
                (
                    [text] => lorem ipsum
                    [id] => 1
                )
            [2] => Array
                (
                    [text] => lorem ipsum
                    [id] => 2
                )
        )
)

And the php I did for it is:

$comments = array(
    'comments' => array()
    );

/* To simualate the loop */
for ($i = 0; $i < 3; $i++) { 
    array_push($comments['comments'], array(
        'text' => 'lorem ipsum',
        'id' => $i
        )
    );
}

I hope this will be to any help and that I didn't misunderstood your question.

hellozimi