PHP select checkbox stage from coma separated string

Hi all, again.

Almost complete this project I am currently developing, but my client requested a sort of last minute change.
The prject is simple. Its a simple cart system with main-categories, sub-categories and products that have 1 main-categories and 1 sub-category assigned to.

My clients request is that he wants to be able to have a any products with multiple main & sub catgeories.

I was planning to do it this way.

1. Pull out the main-categories from my catgeories table and display them as a list with checkboxes.
2. Pull out the sub-categories from my subcategories table and display them as a list with checkboxes.

Let the client select the main and sub categories by clicking the checkboxes and when the form gets submitted, the selected main-catgeories gets insert into a field as a coma seperated list and the sub-catgories gets insert into a field as a coma seperated list.

I managed to display main & sub catgeory as a list with checkboxes onto the left. If my clients clicks any of the sub-categories, a little javascript function will atomatically select the associated main-category. This seems to work alright and without any errors. See below

[] HEALTH & BEAUTY Grooming/Cleaning
	[] Cosmetics <-Sub-Categories List
	[] Feet & Nail Care
	[] Feminine Care
	[] First Aid
	[] Hair & Lashes Care
	[] Skin & Facial Care

[] PET SUPPLIES <-Main-Category 
	[] Eye & Ear Ointments/Drops <-Sub-Categories List
	[] Flea & Tick
	[] Fungal & Yeast Infections
	[] Grooming/Cleaning

Open in new window


What I need to do now is to display the selected checkboxes for the sub-categories he already selected after he submits the form.

Here is the current code that I am using:

<form method="post">
<input type="hidden" name="psku" value="<?php echo $_GET['psku']; ?>">
<?php
echo '<ul>';
#Query my Main Categories Table
$maincatSQL = "SELECT * FROM e2o_mainnav";
$maincatRLT = mysql_query($maincatSQL) or die("Error: ".mysql_error()); # output error message

while($mainROW = mysql_fetch_array($maincatRLT)){
echo '<div class="adminmulticheckboxes">';
echo '<li><input type="checkbox" name="selectedmaincat[]" value="'.$mainROW['maincatbrand'].'"  style="margin-right:10px;" /><span>'.$mainROW['catgroup'].'</span>';
echo '<ul>';

#Inside the main category loop I query my Sub Categories Table
$subcatSQL = "SELECT * FROM e2o_subnav WHERE subcatbrand = '{$mainROW['maincatbrand']}'";
$subcatRLT = mysql_query($subcatSQL) or die("Error: ".mysql_error()); # output error message
while($subROW = mysql_fetch_array($subcatRLT)){

#Inside the sub category loop I query my products Table that contains the a coma seperated string
#of the assigned sub-categories
$prdSQL = "SELECT pname, maincategorytest, subcategorytest FROM e2o_products WHERE psku = '{$_GET['psku']}'";
$prdRLT = mysql_query($prdSQL) or die("Error: ".mysql_error()); # output error message
$prdROW = mysql_fetch_array($prdRLT);

#Here is where all goes wonkie. I am trying to break the coma separated string into individual words and than compare
#each word against the list of my checkboxes. If 1 word matches, it should set the checkbox into checked stage.
$selected = str_replace(',', ' ', $prdROW['subcategorytest']);
$selected = explode(' ', $selected);	
for ($i=0; $i<count($selected); $i++){
$selected = $selected[$i];
if(strcasecmp($selected,$subROW['catseoname'])==0){$ischecked = 'checked="checked"';}
}		

echo '<li><input type="checkbox" name="selectedsubcat[]" '.$ischecked.' value="'.$subROW['catseoname'].'" /> '.$subROW['catname'].'</li>
';

}
echo '</ul>';
echo '<div style="padding-bottom:10px;"></div>';
echo '</div>';
}
echo '<div class="clearall"></div>';
echo '</li>';
echo'</ul>';
?>
<input type="submit" class="globalokbutton" value="Update Details" name="assigncats" style="float:left; margin-left:0;margin-top:20px;"/>
</form>

Open in new window

Would anyone know on how to accomplish this. My explanation might be not as good, but I could provide more info or data if needed.
Please feel free to ask.

Thank you again for your time!

Regards.
slickipAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

rinfoCommented:
Why you are using this
$selected = str_replace(',', ' ', $prdROW['subcategorytest']);
$selected = explode(' ', $selected);      

you should be using
$selected = explode(', ', $selected) in the first place and no need to change comma to space.
because , is a valid delimiter.
Besides your sub cat list also contains in their name so that may work against your code .
If subcatlist did not contained spaces then your code was valid.
0
Ray PaseurCommented:
If this were my task, the informational tables might be like this...
Products
MainCategories
SubCategories

The relational tables would be like this...
Products_MainCategories
Products_SubCategories
MainCategories_SubCategories

This design would enable you to eliminate hierarchies and create relationships in a variety of flexible ways, by simply adding a row to the relational tables.

Not sure where the comma-separated lists would fit in here.  I might want to think about using XML or a JSON string to pass the information, but in any case the first task of the action script would be to tease apart the request array.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
slickipAuthor Commented:
Hi Ray!

I decided to use your method by creating relational tables. Looks much cleaner & will cause no headaches in to long time, plus it will be easier to manage. It must have slipped my minds that I didn't think about products and multiple cat/subcategories before I started coding this project. Nothing that a few hours of recoding and plenty of coffee can fix.

Thank you again for your time and everyone else involved with this problem.

Best Regards .... =D
0
Ray PaseurCommented:
Thanks for the points -- I am sure you will find that "junction table" structure to offer many good ways of looking at the data!  All the best, ~Ray
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.