Jump to content

Self Generating Preselected Dropdown List


Sleeper
Go to solution Solved by mac_gyver,

Recommended Posts

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.

Link to comment
Share on other sites

  • Solution

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.