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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.