Splitting lists into two divs on one page

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

Hi,

There are several ways  you could do this, but I think this is the simplest solution:

<?php
  /* STEP 1: LOAD RECORDS - Copy this PHP code block near the TOP of your page */
  
  // load viewer library
  $libraryPath = 'cmsAdmin/lib/viewer_functions.php';
  $dirsToCheck = array('C:/wamp/www/','','../','../../','../../../');
  foreach ($dirsToCheck as $dir) { if (@include_once("$dir$libraryPath")) { break; }}
  if (!function_exists('getRecords')) { die("Couldn't load viewer library, check filepath in sourcecode."); }

  //Number of articles to be displayed per page.
  $perPage = 8; // This should be an even number
  
  //Calculate number of articles per div.
  $perDiv  = $perPage / 2; 

  // load records from 'blog'
  list($blogs, $blogMetaData) = getRecords(array(
    'tableName'   => 'blog',
    'loadUploads' => true,
    'allowSearch' => false,
    'perPage'     => $perPage
  ));

  //Cut the first records to be displayed from the getRecords array
  $firstDiv  = array_slice($blogs, 0,$perDiv);
  
  //Cut the second records to be displayed from the getRecords array
  $secondDiv = array_slice($blogs,$perDiv, count($blogs));

?>
<!-- Display data in two seperate divs -->
  <div id="1" >
    <ul>
      <?php foreach($firstDiv as $article): ?>
        <li><?php echo $article['title']; ?></li>
      <?php endforeach; ?>
    </ul>
  </div>
  <div id="2" >
    <ul>
      <?php foreach($secondDiv as $article): ?>
        <li><?php echo $article['title']; ?></li>
      <?php endforeach; ?>
    </ul>
  </div>

<!--  Display Page Links (Paste anywhere below "Load Record List") -->
  <?php if ($blogMetaData['prevPage']): ?>
    <a href="<?php echo $blogMetaData['prevPageLink'] ?>">&lt;&lt;  prev</a>
  <?php else: ?>
    &lt;&lt; prev
  <?php endif ?>

  - page <?php echo $blogMetaData['page'] ?> of <?php echo $blogMetaData['totalPages'] ?> -

  <?php if ($blogMetaData['nextPage']): ?>
    <a href="<?php echo $blogMetaData['nextPageLink'] ?>">next &gt;&gt;</a>
  <?php else: ?>
    next &gt;&gt;
  <?php endif ?>
  <!-- /Display Page Links -->

This is just example code, so you'll need to make a few changes to get it working. 

So you set the number of articles you want to display using the perPage variable, this is then used to work out the key values that need to be cut from the getRecords $blog array for each div using the array_slice function (http://php.net/manual/en/function.array-slice.php). Then you just need to cycle through the $firstDiv and $secondDiv arrays and display their data. 

Let me know if you have any questions.

Thanks!

Greg

Greg Thomas







PHP Programmer - interactivetools.com

By JeffC - March 26, 2013

Thanks Greg

That does exactly what I need it to do.

Jeff