Webmaster Forum

Go Back   Webmaster Forum > Web Development > Coding Forum

Coding Forum Problems with your code? Discuss coding issues, including JavaScript, PHP & MySQL, HTML & CSS, Flash & ActionScript, and more.


Reply
 
Thread Tools Display Modes
Share |
  #1  
Old 11-19-2014, 07:31 AM
basketmen basketmen is offline
Contributing Member
 
Join Date: 03-21-07
Posts: 97
iTrader: 0 / 0%
[PHP] Set selected option of select tag & fetch_array

I have this code, there is no selected yet
Quote:
while($datas=mysql_fetch_array($rs)){
$data .= "<option value='$datas[product]' ">$datas[product]</option>";
}


I want to set selected option, already tried this in red, but its selected all value
Quote:
while($datas=mysql_fetch_array($rs)){
$data .= "<option value='$datas[product]' " . selected . ">$datas[product]</option>";
}



then tried this in red
Quote:
while($datas=mysql_fetch_array($rs)){
$data .= "<option value='$datas[product]' " . if ($datas['product'] == 'test') { echo 'selected' . ">$datas[product]</option>";
}
the result is getting error message like this, maybe its still not correct or cant use php code inside it :
Parse error: syntax error, unexpected T_IF in /home/username/public_html/test.php on line 29




tried put echo, just for test
Quote:
while($datas=mysql_fetch_array($rs)){
$data .= "<option value='$datas[product]' " . echo 'test'; . ">$datas[product]</option>";
}
the result is getting error message like this, so event cant print simple echo inside it :
Parse error: syntax error, unexpected T_ECHO in /home/username/public_html/test.php on line 29



please help share your knowledge guys, what is the right code to set selected in above code

GBU for all the answering
 
Reply With Quote

Advertisement

Advertisement

  #2  
Old 11-20-2014, 12:26 PM
J. H. Rasmussen J. H. Rasmussen is online now
Super Moderator
 
Join Date: 11-11-11
Location: Copenhagen, Denmark
Posts: 5,998
iTrader: 1 / 100%
This PHP-code should do the trick:
PHP Code:
while($datas=mysql_fetch_array($rs)){
    
$data .= "<option value=\"$datas[product]\"" . (($datas['product'] == 'test')?" selected=\"selected\"":"") . ">$datas[product]</option>";

selected="selected" will be inserted to the option start tag just before ">" if $datas['product'] = 'test'.

I have tested the code, so it should work.
 
Reply With Quote
  #3  
Old 11-28-2014, 01:41 PM
ServStrongcom's Avatar
ServStrongcom ServStrongcom is offline
Contributing Member
 
Join Date: 11-11-14
Posts: 62
iTrader: 0 / 0%
PHP Code:
while($datas=mysql_fetch_array($rs)){
if(
$datas['product'] == 'test')
$data .= "<option value='$datas[product]' selected>$datas[product]</option>";
else
$data .= "<option value='$datas[product]' >$datas[product]</option>";

 
Reply With Quote
Go Back   Webmaster Forum > Web Development > Coding Forum

Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
highlight selected link css CORT0619 Coding Forum 2 04-18-2013 05:16 PM
SELECTED in drop down basketmen Coding Forum 1 10-28-2011 01:40 AM


V7N Network
Get exposure! V7N I Love Photography V7N SEO Blog V7N Directory


All times are GMT -7. The time now is 06:49 PM.
Powered by vBulletin
Copyright 2000-2014 Jelsoft Enterprises Limited.
Copyright © 2003 - 2018 VIX-WomensForum LLC