You are here:  » Replicating Existing Filters Issue


Replicating Existing Filters Issue

Submitted by ChrisNBC on Wed, 2014-02-05 15:53 in

Hi David,

Hope all is going well.

I wondered if you might be able to help me with creating an additional filter for the site I have been working on. I'm trying to add a colour filter to an existing page (prices.php). I created the filter by taking another similar filter and changing the variables so it applied to colour. I have already added an additional field to the PT dB with the name 'phone_colour' and have populated it with data.

I must have either missed something because when the pages displayed the only option is the 'All' option. I wondered if you might be able to spot anything obviously a miss in the code below which I'm using:

<td><?php
      // print "<select name='phone_colourFilter' onchange='JavaScript:this.form.submit();'>";
      print "<select name='phone_colourFilter'>";
      print "<option value=''>All</option>";
      foreach($phone_colour as $phone_colour => $v)
      {
        $selected = ($phone_colourFilter==$phone_colour?"selected='selected'":"");
        print "<option value='".htmlspecialchars($phone_colour)."' ".$selected.">".$phone_colour."</option>";
      }
      print "</select> ";
      ?></td>

I have also added the code below to the section of prices.php which is commented "// apply filters if set"

 if ($phone_colour)
    {
      if ($product["phone_colour"] != $phone_colourFilter) $drop = TRUE;
    }

Thanks in advance.

Regards

Chris

Submitted by support on Wed, 2014-02-05 16:13

Hi Chris,

In the first section of code, this line is using the same variable name twice:

      foreach($phone_colour as $phone_colour => $v)

The first instance of $phone_colour should be an array of all possible colours - maybe $phone_colours for example. Check how it's implemented for the filter that you copied, and that should populate the drop down correctly.

In the second section of code, I think the IF condition should be checking the filter variable - e.g.

  if ($phone_colourFilter)

...but in addiotion, there should be equivalent code nearer to the top of search.php where the variable is populated from the $_GET superglobal - e.g.

  $phone_colourFilter = (isset($_GET["phone_colourFilter"])?$_GET["phone_colourFilter"]:"");

Hope this helps! If still no joy, email me the files that you've modified and I'll check them out for you...

Cheers,
David.
--
PriceTapestry.com