Solved

PHP select checkbox stage from coma separated string

Posted on 2012-03-28
4
516 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 110

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 110

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

623 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