Sleeper Posted September 12, 2013 Share Posted September 12, 2013 Ok, I think I'm way over thinking this, and I've been sitting here for hours trying to figure out what I'm missing. Using pdo and php to pull from a data base I want to generate a drop down list and already have any of the items that were alllready selected set as selected. Now I was able to do something like echo "<select class='form' name='subcategory' multiple='multiple' size='5'>\n"; $i = 0; while ($i <= ($count-1)) { if ($category[$i]==$categoryname[0] || $category[$i]==$categoryname[1] || $category[$i]==$categoryname[2] || $category[$i]==$categoryname[3]){ echo "<option value='$category[$i]' selected>$category[$i]</option>\n"; }else{ echo "<option value='$category[$i]'>$category[$i]</option>\n"; } $i++; } echo "</select>"; This works just fine, however the categoryname value is manualy set. I need this to change as well based on how many categoryname options their are. I was able to do ... $A = 0; while ($A <= ($addcount-1)){ $arraytest[]='$category[$i]=='.$categoryname[$A]; $A++; } $arraytest=implode('||', $arraytest); and that generates the "if" list, but I have no idea how to get that into the if statment without breaking it.Any ideas? I'm sure I'm overthinking it here. Thanks in advance. Quote Link to comment Share on other sites More sharing options...
fastsol Posted September 13, 2013 Share Posted September 13, 2013 I didn't read through your code but based on your questions this might be of some help http://amecms.com/article/Retaining-Drop-down-Selection-After-Form-Submit Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted September 13, 2013 Share Posted September 13, 2013 it would help if you posted some sample data for $category and $categoryname and what result you want from that data, but in_array() is probably part of the solution. - if(in_array($category[$i],$categoryname)) Quote Link to comment Share on other sites More sharing options...
Solution mac_gyver Posted September 13, 2013 Solution Share Posted September 13, 2013 based on what you posted, all that code can be replaced with the following - echo "<select class='form' name='subcategory[]' multiple size='5'>\n"; foreach($category as $cat){ $sel = in_array($cat,$categoryname) ? 'selected' : ''; echo "<option value='$cat' $sel>$cat</option>\n"; } echo "</select>"; note: for the select to work with multiple choices, the field name must be an array. Quote Link to comment Share on other sites More sharing options...
Sleeper Posted September 13, 2013 Author Share Posted September 13, 2013 mac_gyver TYVM! that simplified things. I've self tought my self in all I know so I'm a bit short on knowlage in arrays. Still expanding my knowlage on that topic (and many others..lol) One thing I did continue on to notice last night is that I needed to make sure the sub category didn't match the main catagory or it caused a glitched. So I ened up putting in an extra if to make sure if category == pgategory. You asked for some more info on what the variable are so in one example //Existing Categories $category=array(Remote-Start,Remote-Start-Alarm,Accessories); //Preselected additional categories $categoryname=array(Remote-Start-Alarm,Accessories); //Parent Category $pcategory="Remote-Start-Alarm"; echo "<select class='form' name='addlcategory[]' multiple='multiple' size='5'>\n"; $i = 0; while ($i <= ($count-1)) { if ($category[$i]==$pcategory) echo "<option value='$category[$i]'>$category[$i]</option>\n"; elseif ($category[$i]==$categoryname[0] || $category[$i]==$categoryname[1] || $category[$i]==$categoryname[2] || $category[$i]==$categoryname[3]){ echo "<option value='$category[$i]' selected>$category[$i]</option>\n"; }else{ echo "<option value='$category[$i]'>$category[$i]</option>\n"; } $i++; } echo "</select>"; So in this case I would only want the option of Accessories to be preselected as the other is already the parent category.As I will need to read up more on why your coding works, I'm not sure how I would add that into it. Sorry to ask for more help after you allready answered the orginal question, but I hope you don't mind. Thank You in advance. Jim Quote Link to comment Share on other sites More sharing options...
Sleeper Posted September 18, 2013 Author Share Posted September 18, 2013 Did I say something to scare everyone away or upset them? Post went dead after I asked that last part. Quote Link to comment Share on other sites More sharing options...
Sleeper Posted September 18, 2013 Author Share Posted September 18, 2013 After spending some time reading up I was able to use.. echo "<select class='form' name='addlcategory[]' multiple size='5'>\n"; foreach($category as $cat){ if ($cat == $pcategory){ echo "<option value='$cat'>$cat</option>\n"; }else{ $sel = in_array($cat,$categoryname) ? 'selected' : ''; echo "<option value='$cat' $sel>$cat</option>\n"; }} echo "</select>"; Seems to work. Thanks for the help. 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.