tooNight Posted April 20, 2006 Share Posted April 20, 2006 Hi there,Trying to have a selectbox that queries the database which contains two fields countryID and countryName and outputs countryID. I can get it to work ok but not when I put [!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]if($form->value("country") == "$row[countryID]"){ echo "selected"; }[/quote] which needs to be in to write the value to my procedures. If someone would be so kind to point me in the right direction and tell me what syntax mistake I am making it would be a great help.Thanks very muchThe code below works but I need to insert if($form->value("country") == "$row[countryID]"){ echo "selected"; } where it says HERE, what is the best way of doing this?[code]<?php$p2=mysql_query("SELECT countries.countryID, countries.countryName FROM countries") or die(mysql_error());echo "<select name=country>";while($row=mysql_fetch_assoc($p2)) {echo "<option value=$row[countryID]HERE>$row[countryName]</option>";}echo "</select>";?>[/code]My shocking attempt at it which I keep getting errors with.[code] <?php$p2=mysql_query("SELECT countries.countryID, countries.countryName FROM countries") or die(mysql_error());echo "<select name=country>";while($row=mysql_fetch_assoc($p2)) {echo "<option value=$row[countryID] {if($form->value("country") == "$row[countryID]"){ echo "selected"; }}>$row[countryName]</option>";}echo "</select>";?>[/code] Quote Link to comment Share on other sites More sharing options...
Barand Posted April 20, 2006 Share Posted April 20, 2006 try[code]<?phpinclude 'db.php'; // db connectfunction countryOptions($current) { $sql = "SELECT ID, countryName FROM country ORDER BY countryName"; $str = "<option value=''>- select country -</option>\n"; $res = mysql_query($sql) or die (mysql_error()); while (list ($id, $name) = mysql_fetch_row($res)) { $selected = $id==$current ? 'SELECTED' : ''; $str .= "<option value='$id' $selected>$name</option>\n"; } return $str;}$country = '';if (isset($_GET['country'])) { $country = $_GET['country']; echo "You selected $country";}?><FORM method='GET'><select name='country'> <?php echo countryOptions($country)?></select><INPUT TYPE='SUBMIT' name='submit' value='Submit'></FORM>[/code] Quote Link to comment Share on other sites More sharing options...
tooNight Posted April 20, 2006 Author Share Posted April 20, 2006 Thanks this code should could in handy though fixed my code by changing:so this:[code]echo "<option value=$row[countryID] {if($form->value("country") == "$row[countryID]"){ echo "selected"; }}>$row[countryName]</option>";} [/code]becomes:[code]echo "<option value=$row[countryID] ".(($form->value('country')==$row['countryID'])?'selected':'').">$row[countryName]</option>";} [/code]But think your way is more efficient, so maybe will implement it later if I have time.Thanks Quote Link to comment 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.