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 Mikey - July 7, 2023

Hey Dave,

In CMS Builder for each section I have a checkbox named "Hidden" so some records can be changed to hidden to remove them from the website without the need to erase the record. Below is the full code I am using to get the list of sections.

<?php header('Content-type: text/html; charset=utf-8'); ?>
<?php
  
  // load viewer library
  $libraryPath = 'cmsb/lib/viewer_functions.php';
  $dirsToCheck = ['','../','../../','../../../','../../../../'];
  foreach ($dirsToCheck as $dir) { if (@include_once("$dir$libraryPath")) { break; }}
  if (!function_exists('getRecords')) { die("Couldn't load viewer library, check filepath in sourcecode."); }
?>

<!--BEGIN TAG CLOUD-->
<?php // tag cloud
$tagsToCount = array();

$sectionsToField = array(
	'floor_plans'			=> 'title',
	'homes_for_sale'		=> 'title',
	'news'					=> 'title' and NOT hidden,
    'media'                 => 'name' and NOT hidden,
	//'homes_for_sale'		=> 'bedrooms:label',
	//'homes_for_sale'		=> 'bathrooms:label',
	//'news'				=> 'categories:label',
);

//use additional if joining tags from multiple sections
foreach($sectionsToField as $sectionName => $fieldName) {

//get records
$records = mysql_select($sectionName);

foreach ($records as $record) {
//turn field into an array of values
$tags = explode("\t", @$record[$fieldName]); // Modified for multiple tags associated with record.

// add tags to the count array
foreach ($tags as $tag) {
$tag = trim($tag);
if (!$tag) { continue; } // skip empty array values

   if (array_key_exists($tag, $tagsToCount)) {
   $tagsToCount[$tag]++; }
   else {
   $tagsToCount[$tag] = 1; 
      }
  }
}
}
//PHP doesn't have a shuffle function built in that maintains keys, but this version does.
function shuffle_assoc($list) { 
  if (!is_array($list)) return $list; 

  $keys = array_keys($list); 
  //shuffle($keys);  // Disable to show largest quantity at the top and smallest at the bottom.
  $random = array(); 
  foreach ($keys as $key) { 
    $random[$key] = $list[$key]; 
  }
  return $random; 
} 
?>

<!-- DISPLAY TAGS -->
<?php $tagsToCount = shuffle_assoc($tagsToCount); ?>
 <?php if ($tagsToCount): ?>
         <?php  $maximumResults = 32;
$resultCount = 0;
?>
 
<?php $totalTagCount = array_sum($tagsToCount); ?>

<?php
$min_size = 13; //smallest font size
$max_size = 22; //largest font size
$minimum_count = min(array_values($tagsToCount));
$maximum_count = max(array_values($tagsToCount));
$spread = $maximum_count - $minimum_count;
if($spread == 0) {
$spread = 1;
}
    arsort($tagsToCount);
            $tagsToCount = array_slice($tagsToCount, 0, 99);
            $tagsToCount = shuffle_assoc($tagsToCount);
foreach ($tagsToCount as $key => $value): ?>

<?php $size = $min_size + ($value - $minimum_count) * ($max_size - $min_size) / $spread; ?>

<a href="search.php?q=<?php echo $key;?>" class="button small hollow" style="font-size: <?php echo floor($size)?>px;" title='<?php echo $key; ?>'><?php echo $key; ?></a>
<?php if (++$resultCount == $maximumResults) { break;}?>

  <?php endforeach ?>
<?php endif ?>
<div class="clearfix"></div>
<!-- END TAG CLOUD -->

Thank you, Mikey

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,

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