alhen Posted February 5, 2010 Share Posted February 5, 2010 Hi there, I'm trying to populate a multiple select field from mysql. I already have the info stored in mysql, and separated by "<br>". Now I'm calling that info back like this: <? $pieces = explode("<br>", $society_inf); // society_inf is the mysql field name AND the variable name, for simplicity ?> <select name="society_inf[]" multiple="multiple" id="society_inf" tabindex="290"> <?php if($pieces[0] !== ""){echo "<option value=\"$pieces[0]\" selected>$pieces[0]</option>"; } else { echo "";}?> <?php if($pieces[1] !== ""){echo "<option value=\"$pieces[1]\" selected>$pieces[1]</option>"; } else { echo "";}?> <?php if($pieces[2] !== ""){echo "<option value=\"$pieces[2]\" selected>$pieces[2]</option>"; } else { echo "";}?> <?php if($pieces[3] !== ""){echo "<option value=\"$pieces[3]\" selected>$pieces[3]</option>"; } else { echo "";}?> <?php if($pieces[4] !== ""){echo "<option value=\"$pieces[4]\" selected>$pieces[4]</option>"; } else { echo "";}?> <option value="AAPM – American Academy of Pain Medicine" selected>AAPM – American Academy of Pain Medicine</option> <option value="APS – American Pain Society">APS – American Pain Society</option> <option value="AACE – American Association of Clinical Endocrinologists">AACE – American Association of Clinical Endocrinologists</option> <option value="ECE – European Congress of Endocrinology">ECE – European Congress of Endocrinology</option> <option value="ENDO – The Endocrine Society">ENDO – The Endocrine Society</option> <option value="ENEA – European Neuro Endocrine Association">ENEA – European Neuro Endocrine Association</option> <option value="ESPE – European Society for Paediatric Endocrinology">ESPE – European Society for Paediatric Endocrinology</option> <option value="LWPES – Lawson-Wilkins Pediatric Endocrine Society">LWPES – Lawson-Wilkins Pediatric Endocrine Society</option> </select> Here's what it looks like: (the two selected ARE supposed to be) You can see this looks kind of ugly, but it's almost working for me. My question is: Is there a way to loop this array to where it only shows me the selected items (still selected) and the non-selected items (still NOT selected), but no duplicates? I'd like it to look like this: [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/191079-looping-an-array-back-into-a-multiple-select-field/ Share on other sites More sharing options...
alhen Posted February 5, 2010 Author Share Posted February 5, 2010 Ok, I'm not getting any hits so I thought I'd try to jump start something... What I imagine happening is something like this: <?php if($pieces[] == "society_inf[]"){echo "<option value=\"$pieces[0]\" selected>$pieces[0]</option>"; } else { echo "";}?> I know that's not right, but is there a way to loop this information through kinda like this? Quote Link to comment https://forums.phpfreaks.com/topic/191079-looping-an-array-back-into-a-multiple-select-field/#findComment-1007595 Share on other sites More sharing options...
alhen Posted February 11, 2010 Author Share Posted February 11, 2010 It's been six days and I've gotten no replies. Is that because this is a difficult question or because it's a stupid question? Any input on any level would be much appreciated. Thank you, ~Alex Quote Link to comment https://forums.phpfreaks.com/topic/191079-looping-an-array-back-into-a-multiple-select-field/#findComment-1010811 Share on other sites More sharing options...
sader Posted February 11, 2010 Share Posted February 11, 2010 For example I dont understand what u want todo. I dont get it this thing how data on database is diferent from this <option value="AAPM – American Academy of Pain Medicine" selected>AAPM – American Academy of Pain Medicine</option> <option value="APS – American Pain Society">APS – American Pain Society</option> <option value="AACE – American Association of Clinical Endocrinologists">AACE – American Association of Clinical Endocrinologists</option> <option value="ECE – European Congress of Endocrinology">ECE – European Congress of Endocrinology</option> <option value="ENDO – The Endocrine Society">ENDO – The Endocrine Society</option> <option value="ENEA – European Neuro Endocrine Association">ENEA – European Neuro Endocrine Association</option> <option value="ESPE – European Society for Paediatric Endocrinology">ESPE – European Society for Paediatric Endocrinology</option> <option value="LWPES – Lawson-Wilkins Pediatric Endocrine Society">LWPES – Lawson-Wilkins Pediatric Endocrine Society</option> and why some options u retriving from db and some just write staticly? Quote Link to comment https://forums.phpfreaks.com/topic/191079-looping-an-array-back-into-a-multiple-select-field/#findComment-1010815 Share on other sites More sharing options...
alhen Posted February 11, 2010 Author Share Posted February 11, 2010 hmmm... I'm not really sure how else to explain this. The images I included show what I am trying to achieve. Lets say the multiple select field has: Option 1 Option 2 Option 3 Option 4 And I selected: Option 1 Option 4 Only those 2 are entered into the database... now - what I want to do is display the the full menu, with Option 1 & 4 still selected - and Option 2 & 3 not selected. Like this: Option 1 (selected) Option 2 (not selected) Option 3 (not selected) Option 4 (selected) As it stands now it looks like this: Option 1 (selected) Option 4 (selected) Option 1 (not selected) Option 2 (not selected) Option 3 (not selected) Option 4 (not selected) As you can see there are duplicate options 1 & 4. Have I clarified this at all or have I obfuscated the question? Quote Link to comment https://forums.phpfreaks.com/topic/191079-looping-an-array-back-into-a-multiple-select-field/#findComment-1010837 Share on other sites More sharing options...
sader Posted February 11, 2010 Share Posted February 11, 2010 wait a sec why I have feeling that u need here not mysql but javascript and ajax? Are u puting selected options into database just to allow user then see his selection in another select box? Quote Link to comment https://forums.phpfreaks.com/topic/191079-looping-an-array-back-into-a-multiple-select-field/#findComment-1010845 Share on other sites More sharing options...
alhen Posted February 11, 2010 Author Share Posted February 11, 2010 I am not sure if I need Javascript or Ajax. I assumed it would be PHP based. You are correct... I am only collecting this data, to be displayed again in another multiple-select box, and possibly changed. This is an internal tool only, to keep track of information. Quote Link to comment https://forums.phpfreaks.com/topic/191079-looping-an-array-back-into-a-multiple-select-field/#findComment-1010851 Share on other sites More sharing options...
sader Posted February 11, 2010 Share Posted February 11, 2010 One more question: what is the point of showing selected items in another select box? whwn they user allready sees what he selected in the first box? or maybe what u realy want to show in another box is sub categories? for example when i click on AACE y display: Aaaaaaaaaaaaaa Bbbbbbbbbbbbbb Cccccccccccccccccc ECE displays: EEEEEEEEEEEE FFFFFFFFFFFFFF TTTTTTTTTTTTTT ? Quote Link to comment https://forums.phpfreaks.com/topic/191079-looping-an-array-back-into-a-multiple-select-field/#findComment-1010867 Share on other sites More sharing options...
alhen Posted February 11, 2010 Author Share Posted February 11, 2010 I have already explain this, here: I am only collecting this data, to be displayed again in another multiple-select box, and possibly changed. It is because I don't need to simply display the data... I'm already way past that. I need to be able to alter it again because it may change periodically. And I do not need sub-categories. I need it how I've described in my previous posts. I really do appreciate your questions, but do you have any actual advice that can guide me in the right direction? Quote Link to comment https://forums.phpfreaks.com/topic/191079-looping-an-array-back-into-a-multiple-select-field/#findComment-1010878 Share on other sites More sharing options...
sader Posted February 11, 2010 Share Posted February 11, 2010 Well maybe I could help but I realy want to understand what u trying to make. The way I see now is that u realy need javascript+ajax, cuz u are playing with UI. You say that "and possibly changed" who changes that stuf how often it's changed? by user that is clicking on the forms or the server which each 10min updates information on database(in this case u will need use ajax or refresh page in old fashon manner) W/O javascript you even will not be able to add new options into select boxes dinamiclly unles u want to click "submit" button every time u have selected your chooses. Heres how some tool could be done short pseudocode <html> <head> <script type="text/javascript"> function clone(sender) { var size = document.myform.box_b.options.length; document.myform.box_b[size] = new Option(sender.text, sender.value, false, true); } </script> </head> <body> <?php //retriive data from mysql database echo "<form name='my_form'>"; echo "<select multiple=multiple name='box_a'>"; while($row = mysql_fetch_assoc($result)) { echo "<option value='".$row['id']."' onclick='clone(this)'>".$row['title']."</option>"; } echo "</select>"; echo "<select multiple='multiple' name='box_b'></select>"; echo "</form>"; ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/191079-looping-an-array-back-into-a-multiple-select-field/#findComment-1010906 Share on other sites More sharing options...
valve79 Posted May 24, 2011 Share Posted May 24, 2011 Did you ever solve this problem ? I have same problem but seems like I cant solve it. Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/191079-looping-an-array-back-into-a-multiple-select-field/#findComment-1219736 Share on other sites More sharing options...
xyph Posted May 24, 2011 Share Posted May 24, 2011 Give me some sample data for $pieces and I'll try to figure out what's going on Quote Link to comment https://forums.phpfreaks.com/topic/191079-looping-an-array-back-into-a-multiple-select-field/#findComment-1219741 Share on other sites More sharing options...
valve79 Posted May 24, 2011 Share Posted May 24, 2011 <? //data from DB $Z3b = $row['Z3b']; $data =explode(",",($Z3b)); print_r($data); // Array ( [0] => 1194.21 Software applications and operating systems [1] => 1194.22 Web-based intranet and internet information and applications [2] => 1194.23 Telecommunications products ) ?> I need to highlight the data that is stored in db in order to check/edit for user. <select disabled=disabled name="Z3b" multiple='multiple' class="required" disabled> <option value="1194.21 Software applications and operating systems">1194.21 Software applications and operating systems</option> <option value="1194.22 Web-based intranet and internet information and applications">1194.22 Web-based intranet and internet information and applications</option> <option value="1194.23 Telecommunications products">1194.23 Telecommunications products</option> <option value="1194.24 Video and multimedia products">1194.24 Video and multimedia products</option> <option value=" 1194.25 Self contained, closed products"> 1194.25 Self contained, closed products</option> <option value="1194.26 Desktop and portable computers">1194.26 Desktop and portable computers</option> </select> Quote Link to comment https://forums.phpfreaks.com/topic/191079-looping-an-array-back-into-a-multiple-select-field/#findComment-1219747 Share on other sites More sharing options...
jcbones Posted May 24, 2011 Share Posted May 24, 2011 Is the options statically coded, or are they dynamic also? My option would be to set the options in an array, then check them against the database as you build the select box. <?php $options = array('AAPM – American Academy of Pain Medicine','APS – American Pain Society','AACE – American Association of Clinical Endocrinologists','ECE – European Congress of Endocrinology', 'ENDO – The Endocrine Society','ENEA – European Neuro Endocrine Association','ESPE – European Society for Paediatric Endocrinology','LWPES – Lawson-Wilkins Pediatric Endocrine Society'); $pieces = explode("<br>", $society_inf); // society_inf is the mysql field name AND the variable name, for simplicity ?> <select name="society_inf[]" multiple="multiple" id="society_inf" tabindex="290"> <?php foreach($options as $value) { $selected = (in_array($value,$pieces)) ? 'selected="selected"' : NULL; echo '<option ' . $selected . '>' . $value . '</option>'; } ?> </select> Quote Link to comment https://forums.phpfreaks.com/topic/191079-looping-an-array-back-into-a-multiple-select-field/#findComment-1219755 Share on other sites More sharing options...
valve79 Posted May 25, 2011 Share Posted May 25, 2011 Thats exactly what i was looking for. Thank you so much Quote Link to comment https://forums.phpfreaks.com/topic/191079-looping-an-array-back-into-a-multiple-select-field/#findComment-1220035 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.