You are here:  » Filtering records for Multiple Prices.php output


Filtering records for Multiple Prices.php output

Submitted by ChrisNBC on Tue, 2013-08-27 11:29 in

Hi David,

Hope you had a good weekend.

I wonder if you might be able to help me.... I'm currently working on a site where I will have three slightly different versions of prices.php with each containing different fields. I have already created a version of prices.php which contains three tabs with each containing it's own version of the prices table. I have added a field to the pt products table which flags each record as either type1, type2 or type3. I would like to include in each of the three sets of prices.php code a preset filter that will ensure that the records returned will be displayed on the correct tab either type1, type2 or type3. I wondered if you could suggest the generic code to filter the records in this way and where it would need to be inserted in the prices.php code.

I hope I have explained what I am trying to achieve clearly but just in case I haven't the site I'm working on is {link saved}

Thanks in advance.

Regards
Chris

Submitted by support on Tue, 2013-08-27 11:42

Hi Chris,

Simplest way would be to skip records not of the appropriate type within the foreach() loop; so in your type1 version of prices.php, where you have:

  <?php foreach($prices["products"] as $product): ?>

...REPLACE with:

  <?php foreach($prices["products"] as $product): ?>
  <?php if ($product["type"] <> "1") continue; ?>

(assuming the type field is `type` on pt_products and value 1 for a type 1 result)

However, if it's possible that a product may have no results of a certain it might be tidier to separate the $prices["products"] array into separate arrays for each type, and then you can use isset() to determine whether or not to call in the tab / HTML for each type, and within the code for that tab use the custom variable instead of $product["prices"], so for example at the very top of your master prices.php:

<?php
  
foreach($prices["products"] as $product)
  {
    
$prices[$product["type"]][] = $product;
  }
?>

And then within your containing tab code:

  <?php if (isset($prices["1"])): ?>
    <!-- type 1 version of original prices.php code here -->
  <?php endif; ?>

...and in the type 1 version of the original prices.php code, instead of looping over the original version using:

  <?php foreach($prices["products"] as $product): ?>

...instead use:

  <?php foreach($prices["1"] as $product): ?>

Hope this helps!

Cheers,
David.
--
PriceTapestry.com

Submitted by ChrisNBC on Thu, 2013-09-12 14:09

Hi David,

Thanks for the above, I have realised since my original post that the data contained in the field I planned to use to set the 'type' would not be suitable, as in some instances it would result in an incorrect 'type' being set. However, after looking through all the feeds again, I can see three fields which are set to yes/no (for each record only one of the fields can be set to 'yes'). I wondered if you could suggest a way I could implement your solution above but using the contents of the three fields?

Ideally, I would prefer to implement the second version to only show the tab where there is a result.

Thanks in advance.

Regards
Chris

Submitted by support on Thu, 2013-09-12 14:49

Hi Chris,

Pretty close to the second version can still be used, but instead of deciding which index (1, 2 or 3) of $prices to used base on the value of a single field, you can use any combination of logic based on the value of other fields to make the determination.

So rather than the following code;

<?php
  
foreach($prices["products"] as $product)
  {
    
$prices[$product["type"]][] = $product;
  }
?>

If you instead had 3 separate fields `field1`, `field2` and `field3` that contain yes or no depending on whether or not the record is of type 1, 2 or 3, then you could use:

<?php
  
foreach($prices["products"] as $product)
  {
    if (
$product["field1"]=="yes")
    {
      
$type 1;
    }
    elseif (
$product["field2"]=="yes")
    {
      
$type 2;
    }
    elseif (
$product["field3"]=="yes")
    {
      
$type 3;
    }
    
$prices[$type][] = $product;
  }
?>

In this particular simplified example a switch statement would be more appropriate, but if / elseif / elseif means you can use whatever logic you need to inspect any fields in $product for the identifiers of type 1, 2 or 3 as required.

If you're not sure how to constructed the IF conditions, let me know an example of the condition and I'll work out the code for you...

Cheers,
David.
--
PriceTapestry.com

Submitted by ChrisNBC on Fri, 2013-09-13 16:42

Hi David,

Thanks as always for the super quick response and for the neat solution..I'm having a little trouble with the code:

<?php
 
if isset($prices["1"]): 
?>

Every time I add it around the tabs code (but inside the divs containing the tab code) I get a syntax error...I wondered if you could confirm the code in the solution is correct? Am I correct in my understanding that the code should be inserted around the tabs code but inside the divs containing the tabs code?

Thanks in advance.

Regards
Chris

Submitted by support on Fri, 2013-09-13 16:58

Hi Chris,

Sorry about that - brackets required surrounding the IF condition, corrected version:

<?php
 
if (isset($prices["1"])):
?>

(corrected above also)

Cheers,
David.
--
PriceTapestry.com

Submitted by ChrisNBC on Mon, 2013-09-16 11:24

Hi David,

No problem. Hope you had a good weekend.

Thanks for the revised code. I changed the code and the contents of each tab are now displaying perfectly. The only problem I'm facing is that the tabs are always displayed (even when they are empty). I can see that this is due to the fact that the tab headings are generated outside of the code changes above. The code sits in a block:

<div id="jQueryTabs">
  <ul>
    <li><a href="#tabs-1">Tab Heading 1</a> </li>
    <li><a href="#tabs-2">Tab Heading 2</a></li>
    <li><a href="#tabs-3">Tab Heading 3</a></li>
  </ul>
  <div id="tabs-1">

I have tried (unsuccessfully!) to place the tab headings within a test based on $type but I cannot get it to work. I wondered if you could suggest what the test code should look like?

Thanks in advance.

Regards
Chris

Submitted by support on Mon, 2013-09-16 11:45

Hi Chris,

Have a go with:

<div id="jQueryTabs">
  <ul>
    <?php if (isset($prices["1"])): ?> <li><a href="#tabs-1">Tab Heading 1</a></li><?php endif; ?>
    <?php if (isset($prices["2"])): ?> <li><a href="#tabs-2">Tab Heading 2</a></li><?php endif; ?>
    <?php if (isset($prices["3"])): ?> <li><a href="#tabs-3">Tab Heading 3</a></li><?php endif; ?>
  </ul>
  <div id="tabs-1">

Cheers,
David.
--
PriceTapestry.com

Submitted by ChrisNBC on Mon, 2013-09-16 13:02

Hi David,

Thanks for the quick response. I changed the code to the above and it works beautifully! ...my version was a set of brackets too light!

Thanks again.

Regards
Chris