Jump to content

Simultaneous use of multiple forms


joey_jj

Recommended Posts

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 by joey_jj
Link to comment
Share on other sites

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 by Psycho
Link to comment
Share on other sites

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

Link to comment
Share on other sites

 

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>
...

Link to comment
Share on other sites

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 by joey_jj
Link to comment
Share on other sites

<?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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.