Paginate Upload field on Detail Page?

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

Hi Avrom,

The easiest option might be to have a separate multi record section that contains an upload field where a user can upload images, and assign them to a record if required.  This way you can easily use the pagination system that is built into CMS Builder. 

There is no pagination system built into the uploads field for a record. But you could create your own using something like this:

// load record from 'blog'
list($blogRecords, $blogMetaData) = getRecords(array(
'tableName' => 'news',
'where' => "`num` = '1'",
'loadUploads' => false,
'allowSearch' => false,
'limit' => '1',
));

$blogRecord = @$blogRecords[0]; // get first record
if (!$blogRecord) { dieWith404("Record not found!"); } // show error message if no record found

//Number of results that you want to appear on a page.
$resultsPerPage = 3;

//Get the current page number from the URL, or set it to one if no page number has been selected.
$pageNum = (@$_REQUEST['downloadPage'])? (intval($_REQUEST['downloadPage'])) : '1';

//Calculate the first record that should be loaded in the MySQL string.
$startRecord = ($pageNum-1) * $resultsPerPage;

//Get the images from the uploads section. TableName should be the table the image are stored in, and the recordNum will be the record entry for the item in that table
$images = mysql_select('uploads', "tableName = 'news' AND recordNum = '1' LIMIT ".($startRecord).", ".$resultsPerPage);

//Count the number of applicable images.
$totalEntries = mysql_count('uploads', "tableName = 'news' AND recordNum = '1'");

//Calculate the number of image pages.
$totalPages = $totalEntries/$resultsPerPage;

?><!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" />
<style type="text/css">
body { font-family: arial; }
.instructions { border: 3px solid #000; background-color: #EEE; padding: 10px; text-align: left; margin: 25px}
</style>
</head>
<body>

<?php foreach ($images as $index => $upload): ?>

<img src="uploads/<?php echo $upload['urlPath'] ?>" width="<?php echo $upload['width'] ?>" height="<?php echo $upload['height'] ?>" alt="" />

<?php endforeach ?>
<!-- pageination -->

<!-- If the downloadPage number is greater than 1, show a previous button. -->
<?php if($pageNum > 1): ?>
<a href="?downloadPage=<?php echo $pageNum -1; ?>" ><?php echo $pageNum -1; ?></a>
<?php endif; ?>

<!-- If the page number is less than the total number of pages, show a next button. -->
<?php if($pageNum < $totalPages): ?>
<a href="?downloadPage=<?php echo $pageNum +1; ?>" ><?php echo $pageNum +1; ?></a>
<?php endif; ?>

</body>
</html>

Thanks

Greg

Greg Thomas







PHP Programmer - interactivetools.com

By avrom33 - January 16, 2013 - edited: January 16, 2013

Thanx Greg, I figured it needed to be a multi-record of some type to work. This is a little over my head, but I'll follow through and see if I can make it work. :)

Actually, the "list page" already has pagination built in. I can just use that:

  // load records from 'pictures'
  list($picturesRecords, $picturesMetaData) = getRecords(array(
    'tableName'   => 'pictures',
    'perPage'     => '9',
    'loadUploads' => true,
    'allowSearch' => false,
  ));

But your right, you need to use a multiple list record instead of one uplaod field to make it work. Thanx! :)

Cheers