Filtering a list an "or" facility

5 posts by 3 authors in: Forums > CMS Builder
Last Post: July 30, 2014   (RSS)

By willydoit - July 28, 2014

Hi All,

I have a business directory listing businesses of different types, from this list I want to populate a places to eat page which shows only businesses which have a business type which equals either "Cafe" "Public House" Restaurant" or Take Away Food" 

I do already have a "where" clause operating due to needing to display listings in a certain order, ie featured first, then standard listings and finally complimentary listings, so there are three arrays being created and I need to apply the parameters to each array.

I have the following code and I have added in red what I want to achieve but do not know the correct syntax or procedure to achieve it. I did see a post indicating that future releases (post ver 2.53) would include support for OR using square brackets but am afraid it went over my head and dont know even if it was relevant to what I want to achieve. If someone could advise on the correct procedure/syntax for achieving this it would be appreciated.

<?php
/* STEP 1: LOAD RECORDS - Copy this PHP code block near the TOP of your page */
require_once "/home/sites/bridlington.net/public_html/cmsAdmin/lib/viewer_functions.php";
list($business_directoryRecordsFeatured, $business_directoryMetaDataFeatured) = getRecords(array( 'tableName' => 'business_directory', 'orderBy' => 'RAND()', 'where' => 'featured_listing = 1',and where business type = Cafe or Restaurant or Public House or Take Away Food ));

list($business_directoryRecordsstandard, $business_directoryMetaDatastandard) = getRecords(array( 'tableName' => 'business_directory', 'orderBy' => 'RAND()', 'where' => 'standard_listing = 1', and where business type = Cafe or Restaurant or Public House or Take Away Food)); 

list($business_directoryRecordscomp, $business_directoryMetaData) = getRecords(array(
'tableName' => 'business_directory',
'orderBy' => 'RAND()', 'where' => 'comp_listing = 1', and where business type = Cafe or Restaurant or Public House or Take Away Food
));

?>

As always thanks in advance for any help provided.

By claire - July 28, 2014

Sure, here's how the code should be formatted:

<?php
/* STEP 1: LOAD RECORDS - Copy this PHP code block near the TOP of your page */
require_once "/home/sites/bridlington.net/public_html/cmsAdmin/lib/viewer_functions.php";
list($business_directoryRecordsFeatured, $business_directoryMetaDataFeatured) = getRecords(array( 'tableName' => 'business_directory', 'orderBy' => 'RAND()', 'where' => 'featured_listing = 1 and business_type IN ("Cafe","Restaurant","Public House","Take Away Food")')); 

list($business_directoryRecordsstandard, $business_directoryMetaDatastandard) = getRecords(array( 'tableName' => 'business_directory', 'orderBy' => 'RAND()', 'where' => 'standard_listing = 1 and business_type IN ("Cafe","Restaurant","Public House","Take Away Food")')); 


list($business_directoryRecordscomp, $business_directoryMetaData) = getRecords(array(
'tableName' => 'business_directory',
'orderBy' => 'RAND()', 'where' => 'comp_listing = 1 and business_type IN ("Cafe","Restaurant","Public House","Take Away Food")'
));

?>

--------------------

Claire Ryan
interactivetools.com

Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

By gregThomas - July 29, 2014

Hey willdoit,

Thanks for attaching your code. You're on the right track thinking the issue is that the business type is multi checkbox list.

I think you need to change the getRecord function to this:

  list($business_directoryRecordscomp, $business_directoryMetaData) = getRecords(array(
    'tableName'   => 'business_directory',
    'orderBy'     => 'RAND()',
    'where' => 'comp_listing = 1 AND (`business_type` LIKE "%Cafe%" OR `business_type` LIKE "%Restaurant%" OR `business_type` LIKE "%Public House%" OR `business_type` LIKE "%Take Away Food%")',
  ));

So a multi list field type stores the values as a tab separated string. So you need to search that string for each value as opposed to matching it. The LIKE operator will search the business type string for the matching variables.

Thanks!

Greg

Greg Thomas







PHP Programmer - interactivetools.com

By willydoit - July 30, 2014

Hi Greg,

that sorted the issue, thank you very much.