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
nitediver
Forum Contributor
Posts: 109 Joined: Tue Feb 24, 2009 9:05 am
Post
by nitediver » Mon May 03, 2010 5:23 am
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
Post
by minorDemocritus » Tue May 04, 2010 7:39 pm
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
Post
by nitediver » Wed May 05, 2010 7:51 am
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>";
}
AbraCadaver
DevNet Master
Posts: 2572 Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:
Post
by AbraCadaver » Wed May 05, 2010 10:41 am
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
Post
by nitediver » Wed May 05, 2010 11:21 am
I will try that, I have been doing searching but can't find, thanks.