Select Option ERROR

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kanchan
Forum Commoner
Posts: 80
Joined: Tue Nov 30, 2004 12:03 pm

Select Option ERROR

Post 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>";
?>
User avatar
N1gel
Forum Commoner
Posts: 95
Joined: Sun Apr 30, 2006 12:01 pm

Re: Select Option ERROR

Post 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>";
 
?>
 
kanchan
Forum Commoner
Posts: 80
Joined: Tue Nov 30, 2004 12:03 pm

Re: Select Option ERROR

Post 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?
User avatar
N1gel
Forum Commoner
Posts: 95
Joined: Sun Apr 30, 2006 12:01 pm

Re: Select Option ERROR

Post 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>";
 
?>
 
kanchan
Forum Commoner
Posts: 80
Joined: Tue Nov 30, 2004 12:03 pm

Re: Select Option ERROR

Post by kanchan »

Thanks N1gel for your kind support :wink:
Post Reply