Filter with multiple checkboxes
Posted: Fri Apr 15, 2011 10:57 am
Since I'm a total PHP noob, I've googled and tried almost everyting i could find, but I can't seem to get the following to work:
I'm trying to filter a portfolio with multiple checkboxes. If a subcategory has an item in it, it gets displayed by default. At this point no checkboxes should be checked.
When a user checks a checkbox, only the items in that category should be displayed. It should also be possible to check multiple checkboxes to show items from different subcategories together.
What happens with the code below is that all items get displayed by default, and all checkboxes are unchecked(which is good!)
When I click a checkbox, only the items in that subcategory get displayed, but the checkbox doesn't stay checked(which is bad!), and when I click another checkbox only the items from that subcategory get displayed instead of the combined checkboxes.
So whats wrong with my code which should do the above,
(Please note it's a school assignment (for absolute php noobs), there's no need to remove inline css, or to take precautions for sql injections and other security measures)
If you could help me with this it would be greatly appreciated 
I'm trying to filter a portfolio with multiple checkboxes. If a subcategory has an item in it, it gets displayed by default. At this point no checkboxes should be checked.
When a user checks a checkbox, only the items in that category should be displayed. It should also be possible to check multiple checkboxes to show items from different subcategories together.
What happens with the code below is that all items get displayed by default, and all checkboxes are unchecked(which is good!)
When I click a checkbox, only the items in that subcategory get displayed, but the checkbox doesn't stay checked(which is bad!), and when I click another checkbox only the items from that subcategory get displayed instead of the combined checkboxes.
So whats wrong with my code which should do the above,
(Please note it's a school assignment (for absolute php noobs), there's no need to remove inline css, or to take precautions for sql injections and other security measures)
Code: Select all
<div id="portfolio">
<h1>Portfolio</h1>
<div id="gallery">
<?php
include('beheer/includes/config.inc.php');
$stage_width=600;
$stage_height=600;
$i=1;
if(is_array($_POST['cat'])){
$toon = implode(',', $_POST['cat']);
$query = "SELECT * FROM foto WHERE subcategorieID IN(".$toon.")";
}else{
$query = "SELECT * FROM foto";
}
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
$left=rand(0,$stage_width);
$top=rand(0,300);
$rot = rand(-40,40);
echo '
<div id="pic-'.($i++).'" class="pic" style="top:'.$top.'px;left:'.$left.'px;background:url(photos/thumbs/'.$row['fotoID'].'.jpg) no-repeat 50% 50%; -moz-transform:rotate('.$rot.'deg); -webkit-transform:rotate('.$rot.'deg);">
<a class="fancybox" rel="fncbx" href="photos/original/'.$row['fotoID'].'.jpg" target="_blank">'.$row['fotoTitel'].'</a>
</div>';
}
?>
</div>
</div>
<div id="filter">
<h1>Categorieën</h1>
<form method="POST">
<?php
$i=1;
$query = 'SELECT
categorie.categorieID as categorieID,
categorie.categorieNaam as categorieNaam,
subcategorie.subcategorieID as subcategorieID,
subcategorie.subcategorieNaam as subcategorieNaam
FROM categorie, subcategorie
WHERE categorie.categorieID = subcategorie.categorieID';
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
if($row['categorieID'] != $oudID){
if($oudID != ''){
echo '</p>
';
}
echo '<h3>'.$row['categorieNaam'].'</h3>
';
echo '<p class="over">
';
$oudID = $row['categorieID'];
}
if(!is_array($_POST['cat']) || in_array($row['subcategorieID'],$_POST['cat'])){
$selected = 'selected="selected"';
}else{
$selected = '';
}
echo '<input name="cat[]" type="checkbox" value="'.$row['subcategorieID'].'" '.$selected.' onclick="this.form.submit();" /> '.$row['subcategorieNaam'].'<br />
';
}
echo '</p>
';
?>
</form>
</div>