Page 1 of 1

Select Option ERROR

Posted: Wed May 21, 2008 8:00 am
by kanchan
Take a look at this code, whats wrong with this? i just wanna call that list of select menu from a table, that code does but only displays the last row
i wanna display all the rows from that table.........

Code: Select all

<?
include 'connect.php';
$result = mysql_query('SELECT * FROM ev_category order by cat_id desc'); 
while($row = mysql_fetch_array($result)) { 
$a=$row[0];
$b=$row[1];
}
?>
 
Select Category
<?
echo "<select name=\"category\">
<option value=\"$a\" selected=\"selected\">$b</option>
</select>";
?>

Re: Select Option ERROR

Posted: Wed May 21, 2008 8:28 am
by N1gel
You need your row to be inside the loop so multiple rows get added.

Try this.

Code: Select all

 
<?
include 'connect.php';
 
$result = mysql_query('SELECT * FROM ev_category order by cat_id desc');
 
echo "<select name=\"category\">";
while($row = mysql_fetch_array($result)) 
{
$a=$row[0];
$b=$row[1];
 
echo "<option value=\"$a\" selected=\"selected\">$b</option>";
}
echo "</select>";
 
?>
 

Re: Select Option ERROR

Posted: Wed May 21, 2008 9:27 pm
by kanchan
N1gel wrote:You need your row to be inside the loop so multiple rows get added.

Try this.

Code: Select all

 
<?
include 'connect.php';
 
$result = mysql_query('SELECT * FROM ev_category order by cat_id desc');
 
echo "<select name=\"category\">";
while($row = mysql_fetch_array($result)) 
{
$a=$row[0];
$b=$row[1];
 
echo "<option value=\"$a\" selected=\"selected\">$b</option>";
}
echo "</select>";
 
?>
 

yeah thanks dude...and one more question
how do i use that "selected" option i mean should i use if statment, can you please give me some suggestions for that too?

Re: Select Option ERROR

Posted: Thu May 22, 2008 3:51 am
by N1gel
Yes I normally just use an if statment like shown below. There might be a better way to do it but this is nice and simple.

I'm using 5 as a random example

Code: Select all

 
<?
include 'connect.php';
 
$result = mysql_query('SELECT * FROM ev_category order by cat_id desc');
 
$selected = 5;
 
echo "<select name=\"category\">";
while($row = mysql_fetch_array($result))
{
  $a=$row[0];
  $b=$row[1];
 
  if($row[0] == $selected)
  {
    echo "<option value=\"$a\" selected=\"selected\">$b</option>";
  }
  else
  {
    echo "<option value=\"$a\">$b</option>";
  }
}
echo "</select>";
 
?>
 

Re: Select Option ERROR

Posted: Thu May 22, 2008 9:31 pm
by kanchan
Thanks N1gel for your kind support :wink: