mikey3521 Posted October 5, 2007 Share Posted October 5, 2007 Hello, Right now i'm using this to pull information from a database and display it in a dropdown list. The problem is I am using the name code when I goto the "Update" page, but i'm not sure how to add the "selected" field, that way what ever I saved in the database will load. Example: I goto add and it add's the "id" of 7 to the field, when I load the page again it auto's back to 1 instead of clicking in-to 7 as thats whats already there... = ) If anyones got a quick fix I would greatly appreciate that. THanks. <?php include("connect.php"); $query="SELECT catagory_name,id FROM catalog_categories"; $result = mysql_query ($query); echo "<select name='catagory_parent' value=''>Student Name</option>"; // printing the list box select command while($nt=mysql_fetch_array($result)){//Array or records stored in $nt echo "<option value=$nt[id]>$nt[catagory_name]</option>"; /* Option values are added by looping through the array */ } echo "</select>";// Closing of list box ?> Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted October 5, 2007 Share Posted October 5, 2007 Add a SELECTED value if there is a match: while($nt=mysql_fetch_array($result)){//Array or records stored in $nt echo "<option value=$nt[id]"; if ($nt[id] == $selected_id) { echo " SELECTED"; } echo ">$nt[catagory_name]</option>"; /* Option values are added by looping through the array */ } Quote Link to comment Share on other sites More sharing options...
$username Posted October 5, 2007 Share Posted October 5, 2007 Try this <? $sql = "SELECT * FROM table ORDER BY `ID` DESC"; if ($result = mysql_query($sql)) { if (mysql_num_rows($result)) { while ($row = mysql_fetch_assoc($result)) { echo "<option value=\"{$row['Field from Table']}\">{$row['Field from Table']}</option>"; } } } ?> Brett Quote Link to comment Share on other sites More sharing options...
mikey3521 Posted October 5, 2007 Author Share Posted October 5, 2007 hmm thank you both for your solution, neither of them worked though perhaps I didn't make myself clear with what I want to happen. Right now the Dropdown box looks like this when I load it -1 -(default selected) -2 -3 -4 -5 Always in that order. If I select 5 and save it to the database.... when I load the script again, I want the default to be 5 so now it will show 5 in the dropdown as the default instead of 1... Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted October 5, 2007 Share Posted October 5, 2007 yes, you need to add SELECTED inside the <OPTION> that matches, like so: if ($nt[id] == $selected_id) { echo " SELECTED"; } ...where selected_id is the one to be selected. Quote Link to comment Share on other sites More sharing options...
MmmVomit Posted October 5, 2007 Share Posted October 5, 2007 You're going to have to store the last value selected in some manner. If you want this to persiste between visits to the website, you're going to have to store it in the database somewhere. If you only want it to work per visit to the website, do what BlueSkyIS suggested, but base it on the POST or GET data. Quote Link to comment Share on other sites More sharing options...
mikey3521 Posted October 5, 2007 Author Share Posted October 5, 2007 haha! BlueSkyIS < my hero haha sorry I didn't follow the first time, i've got it working perfect now. Thank you all = ) specially BlueSkyIS Quote Link to comment Share on other sites More sharing options...
nahid Posted May 23, 2009 Share Posted May 23, 2009 hmm thank you both for your solution, neither of them worked though perhaps I didn't make myself clear with what I want to happen. Right now the Dropdown box looks like this when I load it -1 -(default selected) -2 -3 -4 -5 Always in that order. If I select 5 and save it to the database.... when I load the script again, I want the default to be 5 so now it will show 5 in the dropdown as the default instead of 1... 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.