Set Default Option List

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
nitediver
Forum Contributor
Posts: 109
Joined: Tue Feb 24, 2009 9:05 am

Set Default Option List

Post by nitediver »

I want to display default/selected list(from database), is that possible.
All I know to display default item from option list is like this, as far as I know, tell me if Im wrong.

Code: Select all

<select name="category">
<option value="cat_id">1</option>
<option value="cat_id" select>2</option>
<option value="cat_id">3</option>
</select>

Code: Select all

function cat(){
echo "<select name=\"prod_cat\">";
$onk = mysql_query("SELECT * FROM cat")or die("Error");
while($result=mysql_fetch_array($onk))
{
echo ("<option value=$result[cat_id] selected=\"$qprod[prod_cat]\">$result[cat_name]</option>");
}
echo "</select>";
}
minorDemocritus
Forum Commoner
Posts: 96
Joined: Thu Apr 01, 2010 7:28 pm
Location: Chicagoland, IL, USA

Re: Set Default Option List

Post by minorDemocritus »

Code: Select all

<option value="cat_id" selected="selected">2</option>
And by the way, you can use [ syntax=html ] for straight html highlighting.
nitediver
Forum Contributor
Posts: 109
Joined: Tue Feb 24, 2009 9:05 am

Re: Set Default Option List

Post by nitediver »

Thanks but, how todo that with these php code?

Code: Select all

function cat(){
echo "<select name=\"prod_cat\">";
$onk = mysql_query("SELECT * FROM cat")or die("Error");
while($result=mysql_fetch_array($onk))
{
echo ("<option value=$result[cat_id] selected=\"$qprod[prod_cat]\">$result[cat_name]</option>");
}
echo "</select>";
}
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Set Default Option List

Post by AbraCadaver »

nitediver wrote:Thanks but, how todo that with these php code?
Probably something like this:

Code: Select all

while($result=mysql_fetch_array($onk))
{
   $selected = '';
   if($result['cat_id'] == $qprod['prod_cat']) {
      $selected = ' selected';
   }
   echo '<option value="' . $result['cat_id'] . '"' . $selected. '>' . $result['cat_name'] . '</option>';
}
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
nitediver
Forum Contributor
Posts: 109
Joined: Tue Feb 24, 2009 9:05 am

Re: Set Default Option List

Post by nitediver »

I will try that, I have been doing searching but can't find, thanks.
Post Reply