Jump to content


Photo

array problem


  • Please log in to reply
6 replies to this topic

#1 firelior

firelior
  • Members
  • PipPip
  • Member
  • 26 posts

Posted 22 September 2006 - 03:17 PM

Hi,
I got this array:
while($row = mysql_fetch_assoc($result))
		{
			$elements[] = $row;
		}
        return $elements;
now I want to extract it.
I don't have the database or anything I tryed this:
$option=$elements;
				foreach ($option as $key => $val){
					foreach ($option as $val){
						echo "<option>$val</option>";
					}
				}


#2 Daniel0

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

Posted 22 September 2006 - 03:19 PM

Just do this:
foreach($elements as $key => $val)
{
	echo "<option>$val</option>";
}


#3 firelior

firelior
  • Members
  • PipPip
  • Member
  • 26 posts

Posted 22 September 2006 - 03:21 PM

hi again, well it doesn't work,
because elements = Array ( [ 0 ] => Array ( [id] => 1 [name] => firelior ) )
and what you did is return an array not "1" or "firelior"

#4 Daniel0

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

Posted 22 September 2006 - 03:32 PM

foreach($elements as $val)
{
	foreach($val as $key => $val)
	{
		echo "<option>$val</option>";
	}
}

Then this should do it.

#5 firelior

firelior
  • Members
  • PipPip
  • Member
  • 26 posts

Posted 22 September 2006 - 03:45 PM

thanks, never mind i solved it:
foreach ($option as $key => $val){
					foreach ($val as $key => $val1){

						echo "<option>$val1</option>";
					}
				}

but i got another problem
a little one
how do I test if one is an integer(number) and one is a string?

#6 Daniel0

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

Posted 22 September 2006 - 03:46 PM

To check for numbers: is_int or is_numeric (the latter one is probably the best).
To check for strings: is_string

#7 Barand

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

Posted 22 September 2006 - 07:10 PM

Are you sure you don't want this

<?php
echo "<select name='test'>\n";
foreach ($elements as $val){
		echo "<option value='{$val['id']}'>{$val['name']}</option>\n";
}
echo "</select>\n";
?>

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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users