4 posts by 4 authors in: Forums > CMS Builder
Last Post: November 30, 2017   (RSS)

Anyone have any suggestions on how to sort records to show those records with uploads first, followed by other records without uploads? I can't use "drag sort" within the CMS.

Is it possible to sort upon // Load records from menu ? Similar to what's shown below?

  // load records from 'menu'
  list($menuRecords, $menuMetaData) = getRecords(array(
    'tableName'   => 'menu',
    'sort'        => 'uploads',
    'loadUploads' => true,
    'allowSearch' => false,
  ));

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

  // load records from 'menu'
  list($menuRecords, $menuMetaData) = getRecords(array(
    'tableName'   => 'menu',
    'loadUploads' => true,
    'allowSearch' => false,
  ));

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 <head>
  <title></title>
  <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
 </head>
<body>

    <h1>Menu</h1>
<?php foreach ($menuRecords as $record): ?>
<?php if ($record['photo']): ?>
      <h1><a href="<?php echo $record['_link'] ?>"><?php echo htmlencode($record['name']) ?></a></h1>
      <?php echo $record['content']; ?><br/>
      Price: $<?php echo htmlencode($record['price']) ?><br/>
      
        <?php foreach ($record['photo'] as $index => $upload): ?>
          <a href="<?php echo $record['_link'] ?>">
              <img src="<?php echo htmlencode($upload['thumbUrlPath3']) ?>" alt="<?php echo htmlencode($upload['info1']) ?>" />
          </a>
        <?php endforeach ?>
        
<?php else: ?>

      <h1><a href="<?php echo $record['_link'] ?>"><?php echo htmlencode($record['name']) ?></a></h1>
      <?php echo $record['content']; ?><br/>
      Price: $<?php echo htmlencode($record['price']) ?><br/>
      
<?php endif; ?>
        
        <hr/>

<?php endforeach ?>

<?php echo poweredByHTML(); ?>

</body>
</html>

Thanks Zicky

Hi, Zicky.

There may be a more elegant method, but this is what I would do.

// load records WITH a photo
list($menuRecordsPHOTO, $menuMetaData) = getRecords(array(
'tableName' => 'menu',
'loadUploads' => true,
'allowSearch' => false,
));
// load records WITHOUT a photo
list($menuRecordsNOPHOTO, $menuMetaData) = getRecords(array(
'tableName' => 'menu',
'loadUploads' => true,
'allowSearch' => false,
));

<?php foreach ($menuRecordsPHOTO as $record): ?>
<?php if ($record['photo']): ?>
<h1><a href="<?php echo $record['_link'] ?>"><?php echo htmlencode($record['name']) ?></a></h1>
<?php echo $record['content']; ?><br/>
Price: $<?php echo htmlencode($record['price']) ?><br/>
<?php foreach ($record['photo'] as $index => $upload): ?>
<a href="<?php echo $record['_link'] ?>">
<img src="<?php echo htmlencode($upload['thumbUrlPath3']) ?>" alt="<?php echo htmlencode($upload['info1']) ?>" /></a>
<?php endforeach ?>
<hr>
<?php endif; ?>
<?php endforeach ?>

<?php foreach ($menuRecordsNOPHOTO as $record): ?>
<?php if (!$record['photo']): ?>
<h1><a href="<?php echo $record['_link'] ?>"><?php echo htmlencode($record['name']) ?></a></h1>
<?php echo $record['content']; ?><br/>
Price: $<?php echo htmlencode($record['price']) ?><br/>
<hr>
<?php endif; ?>
<?php endforeach ?>

Hope that helps. :)

~ Deborah

By Toledoh - November 30, 2017 - edited: November 30, 2017

Could you just do 2 loops?

<?php foreach ($menuRecords as $record): ?>
<?php if ($record['photo']): ?>

Show records with images in the order set via CMSB

<?php endif; ?>
<?php endforeach ?>

<?php foreach ($menuRecords as $record): ?>
<?php if (!$record['photo']): ?>

Show records with NO images in the order set via CMSB

<?php endif; ?>
<?php endforeach ?>

Cheers,

Tim Forrest
Toledoh Enterprises
www.toledoh.com.au

Hi Zicky,

An easy solution will be using usort() to specify how you want the array to be sorted. You can check the manual here for more information: http://php.net/manual/en/function.usort.php

But basically the code might be something like this:

// sort array
usort($menuRecords, 'sortByUploads');

// sort function
function sortByUploads($prev, $next){
  if(!empty($prev['photo']) && empty($next['photo'])){
    // $prev has higher priority then $next
    return -1;
  }
  else if(empty($prev['photo']) && !empty($next['photo'])){
    // $next has higher priority then $prev
    return 1;
  }
  else{
    // $prev and $next have the same priority
    return 0;
  }
}

Hope this helps and let me know if you have any questions!

Leo - PHP Programmer (in training)
interactivetools.com