if statement within an array and record not hidden

8 posts by 2 authors in: Forums > CMS Builder
Last Post: July 7, 2023   (RSS)

By Mikey - July 6, 2023

Anyone have suggestions on how to get an if statement to work within an array, so that only records that are not hidden are shown in the results?

Below is a snippet of code for my search engine's tag cloud showing what I'm trying to accomplish... thought what I have fails to work, but I think you'll get the idea.

    $sectionsToField = array(
        'news'                => 'title' and NOT hidden,
        'media'                => 'name' and NOT hidden,
        'about'                => 'name',
        'products'            => 'name',
        'services'            => 'name',
    );

Thanks Mickey

By Dave - July 7, 2023

Hi Mikey, 

Yes, there's many ways to do that.  It just depends on what your source data is.  

How are the getting the list of sections and fields and how are you determining if they're hidden? 

Dave Edis - Senior Developer
interactivetools.com

By Dave - July 7, 2023

Hi Mikey, 

What about something like this: 

foreach ($records as $record) {
  if (isset($record['hidden']) && $record['hidden']) { continue; } // skip hidden records

Let me know if that works for you.

Dave Edis - Senior Developer
interactivetools.com

By Mikey - July 7, 2023

Hey Dave,That worked like a charm! 

Thank you.

By Mikey - July 7, 2023

Hey Dave,

One other question. Any suggestions on how to display the labels of a dropdown list instead of the list num?

For example when I use the code below, I get the numbers of the list item selected within the record.

'homes_for_sale'		=> 'bedrooms',

However, I would like to show the label of the list items selected. So I tried the code below, but it produces zero results.

'homes_for_sale'		=> 'bedrooms:label',

Thanks, Mikey

By Dave - July 7, 2023

Hi Mikey, 

Instead of this:

$records = mysql_select($sectionName);

You can use getRecords which will look up those label values for you.

// get records
// $records = mysql_select($sectionName);
  list($records, $recordsMetaData) = getRecords(array(
    'tableName'     => sectionName,
    'loadUploads'   => false,
    'loadCreatedBy' => false,
    'allowSearch'   => false,
  ));

Hope that helps!

Dave Edis - Senior Developer
interactivetools.com

By Mikey - July 7, 2023

Hey Dave,

That worked great!!! I now have a much more robust search engine.

Thank you so much for the help. 

Mikey