patheticsam Posted January 21, 2009 Share Posted January 21, 2009 Hi! I just have a simple question! Is it possible to define the default value of a listbox depending on the data stored in mySQL? Any help will be appreciated! Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/141783-solved-simple-question-with-listbox-default-value/ Share on other sites More sharing options...
premiso Posted January 21, 2009 Share Posted January 21, 2009 Yep, it is. EDIT: A better question would be, "I have tried to get this listbox to set a default value from a DB but I cannot figure out how. Here is my current code, could someone help me out?" You probably would have got a solution from me then. Can you do this *insert thing* Quote Link to comment https://forums.phpfreaks.com/topic/141783-solved-simple-question-with-listbox-default-value/#findComment-742281 Share on other sites More sharing options...
Maq Posted January 21, 2009 Share Posted January 21, 2009 You need to give more detail. $list = ($row['data'] == "depends") ? "default" : "{$row['data']}";[/code Quote Link to comment https://forums.phpfreaks.com/topic/141783-solved-simple-question-with-listbox-default-value/#findComment-742283 Share on other sites More sharing options...
patheticsam Posted January 21, 2009 Author Share Posted January 21, 2009 Ok here's the code I have now : <?php $db = mysql_connect('localhost', 'username', 'password'); mysql_select_db('database',$db); $sql = "SELECT field1, field2, field3 FROM table1 WHERE id='$id'"; $req = mysql_query($sql) or die('Erreur SQL <br>'.$sql.'<br>'.mysql_error()); while($data= mysql_fetch_assoc($req)) { Basicly if field1 = option2 in mySQL. Then the default selected value should be Option 2 echo " <select name=select selected="$data[field1]"> <option value=option1>Option 1</option> <option value=option2>Option 2</option> <option value=option3>Option 3</option> <option value=option4>Option 4</option> <option value=option5>Option 5</option> </select> "; } ?> How can I do that? Thanks for your help!!! Quote Link to comment https://forums.phpfreaks.com/topic/141783-solved-simple-question-with-listbox-default-value/#findComment-742293 Share on other sites More sharing options...
patheticsam Posted January 21, 2009 Author Share Posted January 21, 2009 Is there anything I'm doing wrong cause ti's really not working this way.....Any help would be greatly appreciated!! Quote Link to comment https://forums.phpfreaks.com/topic/141783-solved-simple-question-with-listbox-default-value/#findComment-742396 Share on other sites More sharing options...
Philip Posted January 21, 2009 Share Posted January 21, 2009 You need to put SELECTED in the options tag, not the select tag. <?php echo " <select name=select> <option value=option1 SELECTED>Option 1</option> <option value=option2>Option 2</option> <option value=option3>Option 3</option> <option value=option4>Option 4</option> <option value=option5>Option 5</option> </select> "; } ?> I would do this: <?php // Create an array of all of the possible values $selectArray = array( 'option1' => 'Option 1', 'option2' => 'Option 2', 'option3' => 'Option 3', 'option4' => 'Option 4', 'option5' => 'Option 5', ) // Start the option list. echo '<select name="select">'; // loop through the array foreach($selectArray as $value => $title) { // Create an option echo '<option value="',$value,'"'; // Check to see if the value is the one that was found in tthe database if($value==$data['field1']) echo ' SELECTED'; // Finish creating the option echo '>',$title,'</option>'; } // Close out the option list echo '</select>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/141783-solved-simple-question-with-listbox-default-value/#findComment-742416 Share on other sites More sharing options...
patheticsam Posted January 21, 2009 Author Share Posted January 21, 2009 Works PERFECTLY!! Thank you so much for the help! Quote Link to comment https://forums.phpfreaks.com/topic/141783-solved-simple-question-with-listbox-default-value/#findComment-742439 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.