FishWagon Posted August 24, 2009 Share Posted August 24, 2009 Hello! I have a registration form with a listbox where people can select multiple items. I Implode the array of selected items and store them into a MYSQL DB. So, for example, the field in that database will have a value of: SelectedValue1, SelectedValue4, SelectedValue7, SelectedValue10, SelectedValue11 When the users login, they can change info about their record. So on this page, I have the listbox with the same entries as the listbox on the registration form. I pre-populate all the other fields on this "user record form" with my recordset and allow them to change things. What I can't figure out, is to how to "pre-select" the items for them so I can show them what they originally selected when registering. So I guess my question is, how can I programatically select an item in an already constructed listbox, using a recordset where the value is an imploded array? Thanks for the help, pretty new to PHP programming..... Have a great day everyone, Rich Link to comment https://forums.phpfreaks.com/topic/171572-solved-programatically-select-listbox-items-from-imploded-array/ Share on other sites More sharing options...
FishWagon Posted August 24, 2009 Author Share Posted August 24, 2009 Of course, if I am doing this in a half-#$@#$ way, I'm open to other ways to accomplish this. The only way I could figure out how to store the array into the MYSQL Database was to implode it. Link to comment https://forums.phpfreaks.com/topic/171572-solved-programatically-select-listbox-items-from-imploded-array/#findComment-905086 Share on other sites More sharing options...
kickstart Posted August 24, 2009 Share Posted August 24, 2009 Hi Store the "array" as seperate rows on a 2nd table. This way when you generate a drop down list you can do an outer join between your full list of options and the list for that particular person. All the best Keith Link to comment https://forums.phpfreaks.com/topic/171572-solved-programatically-select-listbox-items-from-imploded-array/#findComment-905094 Share on other sites More sharing options...
FishWagon Posted August 24, 2009 Author Share Posted August 24, 2009 Understood. However, what I am having problems trying to do is to highlight their previous selections. So once I populate the listbox (which I can do no problem), I want to highlight for them their previous selections so they can see what they already selected when they registered. Is there a way to programatically select multiple items in a listbox? Link to comment https://forums.phpfreaks.com/topic/171572-solved-programatically-select-listbox-items-from-imploded-array/#findComment-905235 Share on other sites More sharing options...
kickstart Posted August 24, 2009 Share Posted August 24, 2009 Hi Say you have 3 tables Users UserId Name Items ItemId ItemName UsersItems UserId ItemId This rough code would do it for you (please excuse any typos):- <?php $dbms = 'mysql'; $dbhost = 'db.MyIsp.com'; $dbname = 'name'; $dbuser = 'userid'; $dbpasswd = 'password'; // Make the database connection. $conn = mysql_connect($dbhost,$dbuser,$dbpasswd) or die(mysql_error()); mysql_select_db($dbname,$conn); $sql = "SELECT c.Name AS Name, a.ItemId AS ItemId, a.ItemName AS ItemName FROM Items a LEFT OUTER JOIN UsersItems b ON a.ItemId = b.ItemId LEFT OUTER JOIN Users c ON b.UserId = c.UserId WHERE c.UserId = '".$GET['UserId']."'"; if ( ($result = mysql_query($sql,$conn)) ) { echo 'Welcome back '.$GET['UserId'].'<br />'; echo '<select name="ItemSelect" mulitple>'; while ($row = mysql_fetch_array($result)) { echo '<option value="'.$row['ItemId'].'" '.(($row['Name'] != '') ? "select='selected'" : '' ).' >'.$row['ItemName'].'</option>'; } echo '</select>'; } ?> You would have to clean this up a bit (ie, sanitise the entered userid before using it in the SQL), but hopefully this will give you the basic idea. All the best Keith Link to comment https://forums.phpfreaks.com/topic/171572-solved-programatically-select-listbox-items-from-imploded-array/#findComment-905249 Share on other sites More sharing options...
FishWagon Posted August 24, 2009 Author Share Posted August 24, 2009 Thanks Keith, that did it for me. I appreciate it very much.....just have to tweak a little like you said. Thanks again.... Link to comment https://forums.phpfreaks.com/topic/171572-solved-programatically-select-listbox-items-from-imploded-array/#findComment-905294 Share on other sites More sharing options...
sunnypal Posted September 14, 2009 Share Posted September 14, 2009 Hello, I have a very similar issue , my script is only able to set the very first value as selected, if a listbox has multiple values that were previously inserted, it is only able to assign the first value as selected value. Below the outer while retrieves the listbox citizenship values from MS SQL database that were previously inserted and then,the inner while is for oracle (that is why you see oci call) that hits oracle table and retrieves all citizenship values and compares with that values that were inserted into the MS SQL database and tried to set them as selected but is sets only the first record as selected. Any help would be appreciated?? while ($msrow = mssql_fetch_array($result,MSSQL_BOTH)) { $CITZ = $msrow['CITZ']; while ($row = oci_fetch_assoc($stmt)) { $sel =((trim($msrow['CITZ']) == trim($row['CODE'])) ? 'selected' : NULL ); echo "<option value='". $row['CODE'] ."' '. $sel .'>". $row['DESC'] ."</option>"; } } Link to comment https://forums.phpfreaks.com/topic/171572-solved-programatically-select-listbox-items-from-imploded-array/#findComment-918557 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.