Jump to content


Photo

array problem


  • Please log in to reply
8 replies to this topic

#1 firelior

firelior
  • Members
  • PipPip
  • Member
  • 26 posts

Posted 21 September 2006 - 07:17 PM

Hi,
I got this 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);
    }
} 
And all of the time it returns only the last one.
for example if I got this table:
title
home
news
about us

it will return only "about us"
I want it to return all of them like this:
array(array('name' => 'home),array('name' => 'news'),array('name' => 'about us'))


#2 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 21 September 2006 - 07:19 PM

Change
$elements=array($key => $row->$key);
to
$elements[]=array($key => $row->$key);


#3 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 21 September 2006 - 07:29 PM

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
<?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;
    
}
?>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#4 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 21 September 2006 - 07:31 PM

One thing I do not understand why you are doing is this:
$row = mysql_fetch_object($result);
while($row = mysql_fetch_object($result)) {
What is the point in that?

#5 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 21 September 2006 - 07:42 PM

Good eyes, missed that. That will just throw the first record away so it doesn't get processed. Editted my post.
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#6 firelior

firelior
  • Members
  • PipPip
  • Member
  • 26 posts

Posted 21 September 2006 - 07:44 PM

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

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

#7 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 21 September 2006 - 07:47 PM

Try:
<?php
$result = mysql_query("select title from content");
while($row = mysql_fetch_object($result))
{
	$elements[] = array('title'=>$row->title,'id'=>$row->id);
}
?>


#8 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 21 September 2006 - 07:51 PM

or, simpler
<?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>';
?>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#9 firelior

firelior
  • Members
  • PipPip
  • Member
  • 26 posts

Posted 21 September 2006 - 07:57 PM

wow!
thanks! :D
great forum.
I am staying here 100%.
Thanks Barand,Daniel0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users