Page 1 of 1

Set Default Option List

Posted: Mon May 03, 2010 5:23 am
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>";
}

Re: Set Default Option List

Posted: Tue May 04, 2010 7:39 pm
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.

Re: Set Default Option List

Posted: Wed May 05, 2010 7:51 am
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>";
}

Re: Set Default Option List

Posted: Wed May 05, 2010 10:41 am
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>';
}

Re: Set Default Option List

Posted: Wed May 05, 2010 11:21 am
by nitediver
I will try that, I have been doing searching but can't find, thanks.