Selection Box for Different Category Filtering (Public Output)

4 posts by 2 authors in: Forums > CMS Builder
Last Post: May 25, 2017   (RSS)

By mark99 - May 24, 2017 - edited: May 24, 2017

Is there a simple way to filter my public products list output via the 'where' call between two different fields, such as via a checkbox that readers could select / click?

At present I call the category for my products list like so:

    'where'         => " category LIKE '%Ultrafast Broadband%' ",

However sometimes I have to make a separate list in order to filter the category output by a second field, which I do by using a different 'where' call:

'where'         => " category LIKE '%Ultrafast Broadband%' AND (uk_network_availability LIKE '%Significant National Coverage%' OR uk_network_availability LIKE '%Patchy National Coverage%') ",

I find it wasteful to have to make two separate listing pages for this and wondered if there's a simpler way to do it on the same page via a selection box or something? So the list would default to one of the two' where' calls and then reload with the other when a visitor clicks a checkbox. Any ideas?

By gkornbluth - May 24, 2017

Hi Mark,

Here's a recipe from my CMSB Cookbook that might help. '

ALLOWING VISITOR TO SET WHERE VALUES FROM MASTER VALUES LIST

For this example I wanted my client to be able to filter the Exhibition records displayed by choosing possible
project_title 'where' values from a pre-determined list of Exhibition titles. This would insure that all the requests
were consistent when filtering the records to be shown, and would keep all the possible values in a CMSB table, instead
of needing to hard code them into the viewer code.

I also wanted to make sure that I was not creating a security risk as described by Dave Edis from Interactive Tools, who
said:

"By passing a letter (or word or code, it doesn't matter) and testing for that instead of just specifying the order by
in the option value directly, you won't allow users to pass MySQL directly into your program and create a security risk."

To accomplish this task, I created a multi-record editor called master_exhiibtion_list which has only one text field
called title. This way each allowed Title was in it's own record. 

Then in the head of my viewer, (with a lot of help from Jason Sauchuck from Interactive Tools), I inserted the following
code:



<?php
list($master_exhibition_listRecords$master_exhibition_listMetaData) = getRecords(array(
'tableName' => 'master_exhibition_list',

));
?>

<?php
$numToName 
= array(); 
foreach (
$master_exhibition_listRecords as $record){ 
  
$numToName[$record['num']] = $record['title']; 


}
?>

<?php 
 $where 
""
  
?>

<?php foreach ($master_exhibition_listRecords as $record): ?>

<?php if (@$FORM['where'] == $record['num']) { $where $record['title'];}?>
<?php 
endforeach; ?>
   
  
<?php
 
list($client_uploadsRecords$client_uploadsMetaData) = getRecords(array(  
    
'tableName'   => 'client_uploads',  
    
'where'       => " project_title = '$where'",  

  ));
?>

Then for the form that selects the values (again with a lot of help from Jason) I used:

<form method="POST" action="<?php echo $_SERVER['PHP_SELF'] ?>">

<select name="where"> 
<option value=""><span class="body-text-bold">Select</span></option> 
<?php foreach($numToName as $num => $name): ?> 
<option value="<?php echo $num;?>"><?php echo $name;?></option> 
<?php endforeach?> 
</select>

<input type="submit" name="submit" value="Select An Exhibition And Click To View">
</form>

Of course you'll have to modify the code to fit your needs.

There are other recipes about using where values as well.

If you're not a subscriber to the Cookbook, why not take advantage of my 3 month free subscription offer below.

Hope this helps,

Jerry Kornbluth

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

By gkornbluth - May 25, 2017

Hope it works for you.

The Cookbook has some tutorials also

Jerry

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php