Vivid Lust Posted September 2, 2008 Share Posted September 2, 2008 Say I had this: <select> <option value ="volvo">Volvo</option> <option value ="saab">Saab</option> <option value ="opel">Opel</option> <option value ="audi">Audi</option> </select> Is there anyway using PHP that I could make say "audi" already selected without editing the HTML??? (its because im making a kind of "Edit Profile Script"; where there are a drop down of options and I want their option selected thats from the database rather than the default top option) Thanks if anyone can help. Quote Link to comment https://forums.phpfreaks.com/topic/122346-make-a-selected-with-php/ Share on other sites More sharing options...
gaza165 Posted September 2, 2008 Share Posted September 2, 2008 If you want audi to be selected everytime as the first car then simply do this code <select> <option value ="volvo">Volvo</option> <option value ="saab">Saab</option> <option value ="opel">Opel</option> <option selected value ="audi">Audi</option> </select> Quote Link to comment https://forums.phpfreaks.com/topic/122346-make-a-selected-with-php/#findComment-631740 Share on other sites More sharing options...
JasonLewis Posted September 2, 2008 Share Posted September 2, 2008 Logic. You would get the value from the database first. $selectedOption = $row['selectedOption']; Then you can do it with if statements, or if you make an array of all the values, a loop. $options = array("", "", "", ""); //Make x amount of blank fields, where x is the amount of options if($selectedOption == "volvo"){ $options[0] = "selected='selected'"; }elseif($selectedOption == "saab"){ $options[1] = "selected='selected'"; } //etc etc Then you just echo out each option element in the option tag: <select> <option value ="volvo" <?php echo $option[0]; ?>>Volvo</option> <option value ="saab" <?php echo $option[1]; ?>>Saab</option> <option value ="opel" <?php echo $option[2]; ?>>Opel</option> <option value ="audi" <?php echo $option[3]; ?>>Audi</option> </select> Hope that makes some sense. Quote Link to comment https://forums.phpfreaks.com/topic/122346-make-a-selected-with-php/#findComment-631743 Share on other sites More sharing options...
Vivid Lust Posted September 2, 2008 Author Share Posted September 2, 2008 So would I do something like this??? <select> <option<?php if($option = "volvo" ){ echo "selected=\"selected\" " } ?> value ="volvo">Volvo</option> <option <?php if($option = "saab"){ echo "selected=\"selected\" "} ?>value ="saab">Saab</option> <option <?php if($option = "opel" ){ echo "selected=\"selected\" " } ?>value ="opel">Opel</option> <option <?php if($option = "audi" ){ echo "selected=\"selected\" " } ?> value ="audi">Audi</option> </select> And then of course the $option would be the value in the database for this Selection. Quote Link to comment https://forums.phpfreaks.com/topic/122346-make-a-selected-with-php/#findComment-631745 Share on other sites More sharing options...
Vivid Lust Posted September 2, 2008 Author Share Posted September 2, 2008 Thanks Project Fear, only just saw your post after I did mine, that cleared some things up Quote Link to comment https://forums.phpfreaks.com/topic/122346-make-a-selected-with-php/#findComment-631746 Share on other sites More sharing options...
john-formby Posted September 2, 2008 Share Posted September 2, 2008 Do you mean something like this: SQL DB Tables -- -- Table structure for table `tblcars` -- CREATE TABLE `tblcars` ( `cID` int(11) NOT NULL auto_increment, `cmake` varchar(100) NOT NULL, PRIMARY KEY (`cID`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ; -- -- Dumping data for table `tblcars` -- INSERT INTO `tblcars` (`cID`, `cmake`) VALUES (1, 'Audi'), (2, 'Volvo'), (3, 'Saab'), (4, 'Ford'), (5, 'Fiat'), (6, 'Honda'); -- -------------------------------------------------------- -- -- Table structure for table `tbluser` -- CREATE TABLE `tbluser` ( `uID` int(11) NOT NULL auto_increment, `cID` int(11) NOT NULL, `uname` varchar(100) NOT NULL, PRIMARY KEY (`uID`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ; -- -- Dumping data for table `tbluser` -- INSERT INTO `tbluser` (`uID`, `cID`, `uname`) VALUES (1, 2, 'Bob Smith'), (2, 5, 'Dave Jones'); index.php <?php $dbHost = "localhost"; $dbUser = "USERNAME"; $dbPass = "PASSWORD"; $dbname = "dbcarselect"; $db = mysql_connect($dbHost,$dbUser,$dbPass); mysql_select_db($dbname,$db); $sql1 = mysql_query("SELECT * FROM tbluser ORDER BY uname ASC"); echo '<html> <head> <title>Car DB</title> </head> <body>'; while($row1 = mysql_fetch_array($sql1)) { $uID = $row1['uID']; $cID = $row1['cID']; $uname = $row1['uname']; $sql2 = mysql_query("SELECT * FROM tblcars ORDER BY cmake ASC"); echo 'Name: '.$uname.' - Car: <select name="cmake">'; while($row2 = mysql_fetch_array($sql2)) { echo '<option ' . ($cID==$row2['cID'] ? 'selected' : '') . ' value="'.$row2['cID'].'">'.$row2['cmake'].'</option>'; } echo '</select><p />'; } echo '</body> </html>'; ?> I have created 2 tables, one for cars and one for users. The PHP script will run a while loop to echo out each user along with a select box containing their car make stored in the user table. John Quote Link to comment https://forums.phpfreaks.com/topic/122346-make-a-selected-with-php/#findComment-631761 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.