PHP list viewer assistance please

5 posts by 3 authors in: Forums > CMS Builder
Last Post: February 4, 2015   (RSS)

By kkegans - January 30, 2015

I have a list page, blog page articles, and I would like to display only the most recent article in the webpage body, then display the date, title and author information of all of the remaining articles in a sidebar of the page.

Can some help me with the PHP code required to make that happen?

Or is it best to treat each part of the page separately and use separate PHP code for the main body and the sidebar?

Kurt

CMSB Rocks!
Thanks,
Kurt

By claire - February 2, 2015

Hi Kurt

It would be good practice to keep them separate if you're going to use the sidebar in places where the most recent article doesn't appear. It's also simpler to deal with overall.

For the most recent article, you'll want a getRecords call that looks like this:

list($articleRecords, $articleMeta) = getRecords(array(
  'tableName' => 'articles',
  'orderBy' => 'createdDate DESC',
  'allowSearch' => false,
  'loadUploads' => true,
  'limit' => 1
));
$mostRecent = $articleRecords[0];

Obviously, you need to put the right table name in there if yours is called something other than 'articles'! Then you can just echo whatever you need out of $mostRecent.

For the sidebar:

list($articleRecords, $articleMeta) = getRecords(array(
  'tableName' => 'articles',
  'orderBy' => 'createdDate DESC',
  'allowSearch' => false,
  'loadUploads' => true,
  'limit' => 6
));
unset($articleRecords[0]);

This will get the six most recent articles (change this number to whatever you like) and then exclude the first one. All you should need to do is run $articleRecords through a foreach loop (see the Code Generator entry for multi-record viewers if you're not sure how this works) and echo the date, title, and author info in the sidebar for each one left.

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

Claire Ryan
interactivetools.com

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

I use something like this to format the first news article or event differently using only one viewer:

<?php
    // load records from 'news'
    list($newsRecords, $newsMetaData) = getRecords(array(
      'tableName'   => 'news',
      'loadUploads' => true,
      'allowSearch' => false,
      'limit'                => 6 // Max number to display in sidebar 
    ));
    $firstNewsRecord = $newsRecords[0]; // Get the first record as $firstNewsRecord
?>          
<?php if($newsRecords): // If any news exists ?>
  <!-- For the first record -->
  <div class="big-news">
    <!-- Place first news story in large format on page -->
    <h1><?php echo $firstNewsRecord['title'] ?></h1>
    <?php echo $firstNewsRecord['date'] ?>
    <?php echo $firstNewsRecord['content'] ?>
  </div>
  <!-- For the Sidebar, the rest of the records -->
  <div class="side-news">
      <?php foreach $newsRecords as $sideNews: // or whatever you want to call it ?>
        <?php if($count++ < 1){continue;} // Skip the first record ?> 
            <a href="<?php echo $sideNews['_link'] ?>"><?php echo $sideNews['title'] ?></a>
      <?php endforeach ?>
  </div> 
<?php else: ?>
  <!-- Inform no news exists message -->
  No news exists  
<?php endif ?>

Ron Conring
Conring Automation Services
----------------------------------------
Software for Business and Industry Since 1987

By kkegans - February 3, 2015 - edited: February 3, 2015

Ron,  Thanks, that is exactly what I was looking for.  I was confused on how to use 2 different viewers on the same page, but your solution works beautifully!

CMSB Rocks!
Thanks,
Kurt