joey_jj Posted February 9, 2013 Share Posted February 9, 2013 (edited) Hello!, I'm still quite new to php but I'm enjoying it a lot. I'm trying to create an enhancement of a pagination script I found on phpfreaks here. The code works great, and so far I've added two forms into the mix. I have a set of images to paginate and I want to filter the images using just two (for now) parameters 'Colour' and 'Pattern'. I've managed to filter the images using each variable separately but I can't work out how to filter them simultaneously. I would like to filter 'Red' images, then filter again on 'Stripes' (the pattern) - thus ending up with Red/Striped images (and keeping it filter order independent i.e. filter by Stripes, then by Red to get the same results). At the moment I filter my Red images, but if I click 'Stripes' I lose the filter on colour. Therefore I would like me code to remember the previous parameter, assuming that it was of a different filter type - e.g. I don't want to filter by 'Red', then try filtering by 'Blue' and for it to filter both Red and Blue images (giving zero results). I've tried and failed a few ways, so below is my working non-simultaneously filtering code. (Note: I know that I would be prone to SQL injection - for now I'm just trying to cover basics. Though I would of course welcome suggestions). Thanks in advance! Joey: Edit: This perhaps should belong in a new thread, but I'd like the form to also remember the last filter so that the user knows which filter has just been selected - at the moment the 'Colour' form refreshes back to the select name 'Colours'. The form code: <form name="Colour" action="../materials/M1.php" method="get"> <select name="Colours" onchange="this.form.submit();"> <option>Colour</option> <option value="%">All</option> <option value="Red">Red</option> <option value="Blue">Blue</option> <option value="Green">Green</option> <option value="Pink">Pink</option> <option value="Yellow">Yellow</option> </select> </form> <form name="Pattern" action="../materials/M1.php" method="get"> <select name="Pattern" onchange="this.form.submit();"> <option>Pattern</option> <option value="%">All</option> <option value="Solid">Solid</option> <option value="Striped">Striped</option> <option value="Checked">Checked</option> <option value="Flower">Flower</option> </select> </form> The php: // select colour - if set, else default if (isset($_GET['Colours']) && (strlen($_GET['Colours'])<10)) { $colourselect = $_GET['Colours']; } else { $colourselect = '%'; } // end if // select colour - if set, else default Code Please paste inside the following box using the keyboard (Ctrl/Cmd+V) and hit OK if (isset($_GET['Pattern']) && (strlen($_GET['Pattern'])<10)) { $patternselect = $_GET['Pattern']; } else { $patternselect = '%'; } // end if // get the info from the db $sql = "SELECT * FROM $tbl_name WHERE (Colour LIKE '$colourselect') AND (Pattern LIKE '$patternselect') LIMIT $offset, $rowsperpage"; // The LIMIT is for pagination Edited February 9, 2013 by joey_jj Quote Link to comment Share on other sites More sharing options...
kicken Posted February 9, 2013 Share Posted February 9, 2013 Make both select boxes part of the same <form> </form> tag, that way they both get sent when you submit the form. Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 9, 2013 Share Posted February 9, 2013 (edited) Also, don't use LIKE in your query unless it is a search within a string - just use the equal operator. $sql = "SELECT * FROM $tbl_name WHERE (Colour = '$colourselect') AND (Pattern = '$patternselect') LIMIT $offset, $rowsperpage"; Edited February 9, 2013 by Psycho Quote Link to comment Share on other sites More sharing options...
joey_jj Posted February 9, 2013 Author Share Posted February 9, 2013 I can't combine forms as this fails due to the onchange="this.form.submit(); I want my users to have the option to do something like 1. Filter Red - get all red images 2. Filter by Stripes - get red stripe images 3. Change filter to blue - get blue stripe images 4. Filter by solids - get solid blue images 5. Turn off Pattern filter - get All blue images And so forth Quote Link to comment Share on other sites More sharing options...
joey_jj Posted February 9, 2013 Author Share Posted February 9, 2013 I used the LIKE so that when selecting 'All' I get the % wild card operator - I suppose I could use an if statement to turn off the particular WHERE clause Quote Link to comment Share on other sites More sharing options...
kicken Posted February 9, 2013 Share Posted February 9, 2013 You can do what you want and still have them in the same form. Just use a bit of PHP to pre-select whatever the currently selected option was for each select box. <option value="%" <?=($colorselect=='%')?'selected="selected"':''?> >All</option> <option value="Red" <?=($colorselect=='Red')?'selected="selected"':''?> >Red</option> ... Quote Link to comment Share on other sites More sharing options...
joey_jj Posted February 10, 2013 Author Share Posted February 10, 2013 (edited) That sounds to be exactly what I want, I just can't make it work - it's still using the filters independently of each other. Perhaps something is wrong with the order of my code?: The form is saved in M2.php: <form name="Colour" action="../materials/M1.php" method="get"> <select name="Colours" onchange="this.form.submit();"> <option value="%" <?php=($colorselect=='%')?'selected="selected"':''?> >All</option> <option value="Red" <?=($colorselect=='Red')?'selected="selected"':''?> >Red</option> <option value="Blue" <?=($colorselect=='Blue')?'selected="selected"':''?> >Blue</option> <option value="Yellow" <?=($colorselect=='Yellow')?'selected="selected"':''?> >Yellow</option> </select> <select name="Pattern" onchange="this.form.submit();"> <option value="%" <?php=($colorselect=='%')?'selected="selected"':''?> >All</option> <option value="Solid" <?=($colorselect=='Solid')?'selected="selected"':''?> >Solid</option> <option value="Striped" <?=($colorselect=='Striped')?'selected="selected"':''?> >Striped</option> </select> </form> Which is then obtained/required in the first line before the if statements: require("../materials/M2.php"); // obtains form and other params to filter // select colour - if set, else default if (isset($_GET['Colours']) && (strlen($_GET['Colours'])<10)) { $colourselect = $_GET['Colours']; } else { $colourselect = '%'; } // end if // select colour - if set, else default if (isset($_GET['Pattern']) && (strlen($_GET['Pattern'])<10)) { $patternselect = $_GET['Pattern']; } else { $patternselect = '%'; } // end if // get the info from the db $sql = "SELECT * FROM $tbl_name WHERE (Colour LIKE '$colourselect') AND (Pattern LIKE '$patternselect') LIMIT $offset, $rowsperpage"; // The LIMIT is for pagination Edited February 10, 2013 by joey_jj Quote Link to comment Share on other sites More sharing options...
Barand Posted February 10, 2013 Share Posted February 10, 2013 <?php session_start(); if (isset($_GET['colour'])) { $colour = $_GET['colour']; } elseif (isset($_SESSION['colour'])) { $colour = $_SESSION['colour']; } else $colour = ''; $_SESSION['colour']= $colour; // put colours in array - makes it easier to set selected colour // by looping through the values $colours = array ('All', 'Red', 'Blue', 'Green', 'Pink', 'Yellow'); $colourOptions = ''; foreach ($colours as $c) { // leave blank value for All option $val = $c == 'All' ? '' : $c; // set current selected colour value $chk = $c == $colour ? "checked='checked'" : ''; $colourOptions .= "<option $chk value='$val'>$c</option>\n"; } ?> <select name="colour" onchange="this.form.submit();"> <?php echo $colourOptions ?> </select> Quote Link to comment Share on other sites More sharing options...
joey_jj Posted February 11, 2013 Author Share Posted February 11, 2013 Thanks Barand! I tweaked it a bit but I actually learnt more during the process! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.