unistake Posted October 20, 2010 Share Posted October 20, 2010 Hi all, I have a html drop down menu and want to show the initially selected value as the one stored in the mysql database that a member selected. I have 180+ rows in mysql that a member could have selected and would like to basically do this: IF (value of option == $row['value'] ) { echo "selected=\"selected\"; } I could put this in every one of the rows it would be much better to have an if statement. As there are so many values I do not know where to begin. Can someone point me in the right direction? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/216390--/ Share on other sites More sharing options...
Pikachu2000 Posted October 20, 2010 Share Posted October 20, 2010 How do you construct the list of <option>s in the form? Quote Link to comment https://forums.phpfreaks.com/topic/216390--/#findComment-1124472 Share on other sites More sharing options...
PFMaBiSmAd Posted October 20, 2010 Share Posted October 20, 2010 You should be dynamically building the <option></option> list using php code in a loop so that the current value IS in a variable and you can easily output the selected="selected" when it needs to be. Quote Link to comment https://forums.phpfreaks.com/topic/216390--/#findComment-1124474 Share on other sites More sharing options...
unistake Posted October 20, 2010 Author Share Posted October 20, 2010 Would something like this work? <?php include ("cxn.php"); $sql = "SELECT manufacturer FROM list"; $result = mysqli_query($cxn,$sql) or die ("Can't execute query!"); ?> <div align="left"> <select name="manufacturer" id="manufacturer"> <option value="” selected="selected">Select a Manufacturer..</option> <?php $manufacturer // FROM ANOTHER MYSQL DB while ($row = mysqli_fetch_assoc($result)) { if ($manufacturer == $row['manufacturer'] { $sel = "selected=\"selected\""; } else { $sel = ""; } echo "<option ".$sel." value=\"".$row[manufacturer']."\'">".$row[manufacturer']."</option>""; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/216390--/#findComment-1124485 Share on other sites More sharing options...
The Little Guy Posted October 20, 2010 Share Posted October 20, 2010 Something like this: <?php $sql = mysql_query("SELECT * FROM member_options WHERE member_id = '12345'"); $row = mysql_fetch_assoc($sql); $selItm = $row['selItm']; $sql = mysql_query("SELECT * FROM tbl"); $opt = '<select name="myselect">'; while($row = mysql_fetch_assoc($sql)){ $opt .= '<option value="'.$row['itemvalue'].'"'.(($row['itemvalue']==$selItm) ? ' selected="selected"' : '').'</option>'; } $opt .= '</select>'; echo $opt; ?> Quote Link to comment https://forums.phpfreaks.com/topic/216390--/#findComment-1124491 Share on other sites More sharing options...
phpfreak Posted October 20, 2010 Share Posted October 20, 2010 I think the problem with the original code is that you're quoting the ".$sel" value... Perhaps this mod could work out.. It looks like the output would be: <option value="foo" "selected="selected"">Foo</option> That could be causing your html to break? Try this: echo '<option '.$sel.' value="'.$row['manufacturer'].'">'.$row['manufacturer'].'</option>'."\n"; There's many debates on single quotes and double quotes and I use single quotes to avoid confusion in HTML such as your initial example above. Hope that helps, if not let us know. Quote Link to comment https://forums.phpfreaks.com/topic/216390--/#findComment-1124495 Share on other sites More sharing options...
unistake Posted October 20, 2010 Author Share Posted October 20, 2010 good tip phpfreak. I have not tried the script yet but will do it your way. Thought there may be a better way. On the " and ' debate. When CANT you replace double quotes with singles? I have had problems before echoing images for example or flash files. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/216390--/#findComment-1124500 Share on other sites More sharing options...
phpfreak Posted October 20, 2010 Share Posted October 20, 2010 good tip phpfreak. I have not tried the script yet but will do it your way. Thought there may be a better way. On the " and ' debate. When CANT you replace double quotes with singles? I have had problems before echoing images for example or flash files. Thanks Personally I gave up on double quotes a long time ago. There technically shouldn't be any problem other than you have to constantly escape double quotes in your HTML and that drives me crazy. There's many theories behind developing theme layers versus data layers and some people wouldn't even use HTML in your PHP code - they would use a straight up theme system for that that takes variables / strings and puts them into the template properly, so in your template you may have placeholders like {SOME_VARIABLE} that would be replaced. That's a bit of a pain in the ass to me and I don't mind putting HTML within my PHP. For example, instead of doing: <? $foo = 'Hi, I\'m FOOO!'; echo 'Who is foo? '.$foo.'; ?> Would be how I would do it.. or <? $foo = 'Hi I am Foo'; ?> Who is foo? <?=$foo?> Would be another way.. I'm not sure if this directly answers your question, but I prefer single quotes... Quote Link to comment https://forums.phpfreaks.com/topic/216390--/#findComment-1124591 Share on other sites More sharing options...
unistake Posted October 20, 2010 Author Share Posted October 20, 2010 Yeah thanks, I will give it a go in the next site. I don't fancy going through all my scripts now to or to mix them up! Quote Link to comment https://forums.phpfreaks.com/topic/216390--/#findComment-1124605 Share on other sites More sharing options...
unistake Posted October 20, 2010 Author Share Posted October 20, 2010 OK. This is what I have made. It does not show the first two records in the database and the field that should be 'selected' is not present. Any ideas? <select name="manufacturer" id="manufacturer"> <option value="”>Select a Manufacturer..</option> <?php // SELECTS AIRCRAFT DETAILS FROM THE DATABASE $sql2 = "SELECT * FROM sales WHERE reg='$reg'"; $result2 = mysqli_query($cxn,$sql2) or die ("Can't execute query 2!"); $row2 = mysqli_fetch_assoc($result2); // OPENS THE CONNECTION TO THE AIRCRAFT LIST DATABASE TO COMPARE THE VALUES OF THE AIRCRAFT IN SUBJECT $sql3 = "SELECT manufacturer FROM list ORDER BY id ASC "; $result3 = mysqli_query($cxn,$sql3) or die ("Can't execute query 3!"); $row3 = mysqli_fetch_assoc($result3); while($row3 = mysqli_fetch_assoc($result3)) { if ($row2['manufacturer'] != $row3['manufacturer']) { echo "<option value=\"".$row2['manufacturer'].">".$row3['manufacturer']."</option>"; } else { echo "<option value=\"".$row2['manufacturer']." selected='selected'>".$row3['manufacturer']."</option>"; } } ?> </select> Quote Link to comment https://forums.phpfreaks.com/topic/216390--/#findComment-1124661 Share on other sites More sharing options...
unistake Posted October 20, 2010 Author Share Posted October 20, 2010 solved Quote Link to comment https://forums.phpfreaks.com/topic/216390--/#findComment-1124666 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.