Blog

2 posts by 2 authors in: Forums > CMS Builder
Last Post: September 25, 2014   (RSS)

Hey Tim,

I'd use the array_groupBy function to filter the list instead:


  $firstRec    = true;
  $searchMonth = false;

  // load records from 'blog'
  list($blogs, $blogsMeta) = getRecords(array(
    'tableName'   => 'blog',
    'loadUploads' => true,
    'allowSearch' => false,
    'orderBy'     => '`date` DESC'
  ));

  //Add the month the record was created to records.
  if($searchMonth){
    foreach($blogs as $key => $blog){
      $blogs[$key]['month'] = $blogMonth = date('m', $blog['date:unixtime']);
      $blogs[$key]['month_name'] = $blogMonth = date('F', $blog['date:unixtime']);
    }
  }

  $groupBy = ($searchMonth)? 'month' : 'category' ;

  //Group the blogs together
  $blogs = array_groupBy($blogs, $groupBy, true);

?>

  <?php foreach($blogs as $group): ?>

    <?php foreach($group as $blog): ?>
      <?php if($firstRec): ?>
        <h1><?php echo ($searchMonth)? $blog['month_name'] : $blog['category:label']; ?></h1>
        <?php $firstRec = false; ?>
      <?php endif; ?>

      <h3><?php echo $blog['title']; ?></h3>

    <?php endforeach; ?>

    <?php $firstRec = true; ?>
  <?php endforeach; ?>

If you change the $sarchMonth variable to true or false at the top of the page, you shouldfind that it switches between grouping the articles by category or month. 

The foreach loop will add variables for which month the article was published if the filter by month system is used. 

This is just example code, so you might have to make a few changes to get it working. 

Cheers,

Greg

Greg Thomas







PHP Programmer - interactivetools.com