Jump to content

array problem


firelior

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]
Link to comment
https://forums.phpfreaks.com/topic/21566-array-problem/
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]
Link to comment
https://forums.phpfreaks.com/topic/21566-array-problem/#findComment-96257
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!
Link to comment
https://forums.phpfreaks.com/topic/21566-array-problem/#findComment-96277
Share on other sites

Archived

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

×
×
  • Create New...

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.