Jump to content

Archived

This topic is now archived and is closed to further replies.

firelior

array problem

Recommended Posts

Hi,
I got this code:
[code]
$result = mysql_query("select title from content");
$row = mysql_fetch_object($result);
while($row = mysql_fetch_object($result)){
    foreach ($row as $key => $val){
      $elements=array($key => $row->$key);
    }
}
[/code]
And all of the time it returns only the last one.
for example if I got this table:
[table][tr][td]title[/td][/tr][tr][td]home[/td][/tr][tr][td]news[/td][/tr][tr][td]about us[/td][/tr][/table]
it will return only "about us"
I want it to return all of them like this:
[code]
array(array('name' => 'home),array('name' => 'news'),array('name' => 'about us'))
[/code]

Share this post


Link to post
Share on other sites
Change [code]$elements=array($key => $row->$key);[/code] to [code]$elements[]=array($key => $row->$key);[/code]

Share this post


Link to post
Share on other sites
Instead of the complication of an array of single-element arrays, would it not be simpler, as you only select a single column to create an array like

array ('home', 'news', 'about us')

with
[code]<?php
$result = mysql_query("select title from content");
$row = mysql_fetch_object($result);                      // EDIT remove this line
while($row = mysql_fetch_object($result)) {
   
      $elements[] = $row->title;
   
}
?>
[/code]

Share this post


Link to post
Share on other sites
One thing I do not understand why you are doing is this: [code]$row = mysql_fetch_object($result);
while($row = mysql_fetch_object($result)) {[/code]
What is the point in that?

Share this post


Link to post
Share on other sites
Good eyes, missed that. That will just throw the first record away so it doesn't get processed. Editted my post.

Share this post


Link to post
Share on other sites
thanks for your fast answer,
i did a multi dimentional array because i want more things other then title
for example
[code]
$result = mysql_query("select id,title from content");
while($row = mysql_fetch_object($result)){
    foreach ($row as $key => $val){
      $elements[]=array($key => $row->$key);
    }
}
[/code]
now there is a problem.
I want it to write something like this:

[code]elements=array(array('title' => 'home','id' => 1),array('title' => 'news','id' => 2));[/code]
but it doesn't..any help?
thanks!

Share this post


Link to post
Share on other sites
Try: [code]<?php
$result = mysql_query("select title from content");
while($row = mysql_fetch_object($result))
{
$elements[] = array('title'=>$row->title,'id'=>$row->id);
}
?>[/code]

Share this post


Link to post
Share on other sites
or, simpler
[code]
<?php
$result = mysql_query("select id, title from content");
while($row = mysql_fetch_assoc($result))
{
$elements[] = $row;
}


//check results
echo '<pre>', print_r($elements, true), '</pre>';
?>
[/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.