Solved

PHP select checkbox stage from coma separated string

Posted on 2012-03-28
4
504 Views
Last Modified: 2012-08-13
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.
0
Comment
Question by:slickip
  • 2
4 Comments
 
LVL 9

Expert Comment

by:rinfo
ID: 37781254
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
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 37781262
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
 

Author Closing Comment

by:slickip
ID: 37785814
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
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 37786851
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

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This article discusses four methods for overlaying images in a container on a web page
In this tutorial viewers will learn how to code links for mobile sites that, once clicked, send a call or text to a specified number. For a telephone link (once clicked, calls a number), begin with a normal "<a href=" link tag. For the href, specify…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now