Displaying from nth record

3 posts by 2 authors in: Forums > CMS Builder
Last Post: May 3, 2013   (RSS)

By kyle - May 2, 2013

Howsit guys

I realise this is a  fairly simple issue, but i cant seem to find a system that works ... 

Im trying to display a normal list page, 

but i want the top 3 items in the order to be displayed in the main div

and the remainder of the records to follow in a lower down div, without any records repeating themselves 

Please help, im at wits end :) 

By gregThomas - May 2, 2013

Morning,

This is probably the simplest solution:

  // load records from 'blog'
  list($blogs, $blogMetaData) = getRecords(array(
    'tableName'   => 'blog',
    'orderBy'     => '',   // use default database order
    'loadUploads' => false,
    'allowSearch' => false,
  ));

?>
<div id="firstThree">
  <ul>
  <?php foreach($blogs as $key => $blog): ?>
    <?php if($key >= 3){ break; } ?>
      <li><?php echo $blog['title']; ?></li>
    <?php endforeach; ?>
  </ul>
</div>
<br>
<div id="everythingElse">
  <ul>
    <?php foreach($blogs as $key => $blog): ?>
      <?php if($key >= 3): ?>
        <li><?php echo $blog['title']; ?></li>
      <?php endif; ?>
    <?php endforeach; ?>
  </ul>
</div>

So the break command (highlighted in orange) will exit the first foreach loop when it reaches the third record in the loop. In the second foreach loop, I've added a if statement (highlighted in blue) that will only display a record if the key value is greater than three.

Another more complicated but slightly more efficient system would be a for loop: http://php.net/manual/en/control-structures.for.php

Cheers!

Greg

Greg Thomas







PHP Programmer - interactivetools.com