Uploads Per Page

14 posts by 3 authors in: Forums > CMS Builder
Last Post: August 21, 2018   (RSS)

By KennyH - August 20, 2018

Is there a way to get uploads limited per per page?

For example, a client is using CMSB as a gallery subscription website. Each album can have 500 to 1000 pictures. I would like get the pictures to display about 50-100 per page instead of trying to load all of them at the same time.  I don't think I have ever done this, so I am needing some help.

Thanks in advance

Kenny

By Dave - August 20, 2018

Hi Kenny, 

Ok, sure.  Is an album a single record in the CMS?  So there's a single record in the CMS with an upload field that has 500-1000 uploads? Is that correct? 

Dave Edis - Senior Developer
interactivetools.com

By KennyH - August 20, 2018

It's a multi-record section and each record is a separate gallery. Each gallery will have 500+ uploads

By daniel - August 21, 2018

Hi Kenny,

Yes, this is quite possible. You can use the following getRecords() call to fetch the uploads limited by the perPage value. It will need to be placed below the getRecords() call that returns the gallery record:

list($uploadRecords, $uploadMetaData) = getRecords(array(
  'tableName' => 'uploads',
  'perPage' => 50,
  'where' => 'tableName = "gallery" AND recordNum = "' . $galleryRecord['num'] . '"',
  'allowSearch' => false,
  'loadUploads' => false,
));

Note that the above is an example and may need to be modified to suit your case; particularly the "gallery" table name and the $galleryRecord variable name.

The created variables are:

  • $uploadRecords will contain the array of records for the current page. You can likely substitute this into the existing loop you are using to output the images.
  • $uploadMetaData will contain data related to the pagination. The following values can be used to provide basic pagination links:
    • $uploadMetaData['prevPageLink']
    • $uploadMetaData['nextPageLink']
    • $uploadMetaData['firstPageLink']
    • $uploadMetaData['lastPageLink']

Let me know if this suits your needs, or if you have any further questions.

Thanks,

Daniel
Technical Lead
interactivetools.com

By KennyH - August 21, 2018

Hi Daniel - I might not have it down just right because I am getting an error

Error adding symbol table to error log num 367!
MySQL Error: Got a packet bigger than 'max_allowed_packet' bytes

mysql_count() MySQL Error: MySQL server has gone away
- in errorlog_functions.php on line 300 by mysql_count()

Here's what I have

list($chs_galleriesRecords, $chs_galleriesMetaData) = getRecords(array(
'tableName' => 'chs_galleries',
'where' => whereRecordNumberInUrl(0),
'loadUploads' => true,
'allowSearch' => false,
'limit' => '1',
));
$chs_galleriesRecord = @$chs_galleriesRecords[0]; // get first record

list($uploadRecords, $uploadMetaData) = getRecords(array(
'tableName' => 'uploads',
'perPage' => 50,
'where' => 'tableName = "chs_galleries" AND recordNum = "' . $gallery_imagesRecord['num'] . '"',
'allowSearch' => false,
'loadUploads' => false,
));

<?php foreach ($uploadRecords['gallery_images'] as $index => $upload): ?>
<a href="<?php echo htmlencode($upload['urlPath']) ?>" class="lightbox-portfolio">
<img src="<?php echo htmlencode($upload['thumbUrlPath']) ?>" class="img-fluid" alt="<?php echo htmlencode($chs_galleriesRecord['gallery_title']) ?>">
</a>
<?php endforeach ?>

By daniel - August 21, 2018

Hi Kenny,

I think that this line:

'where' => 'tableName = "chs_galleries" AND recordNum = "' . $gallery_imagesRecord['num'] . '"', 

should be:

'where' => 'tableName = "chs_galleries" AND recordNum = "' . $chs_galleriesRecord['num'] . '"',

Give that a shot and see if that helps.

Thanks,

Daniel
Technical Lead
interactivetools.com

By KennyH - August 21, 2018

Unfortunately, that gives me the same error

By KennyH - August 21, 2018

That works except I have had to add to the link it generates. Where does this variable get defined?

<img src="/webadmin/uploads/<?php echo htmlencode($upload['thumbUrlPath']) ?>" class="img-fluid" alt="<?php echo htmlencode($chs_galleriesRecord['gallery_title']) ?>">

By daniel - August 21, 2018

Hi Kenny,

To dynamically generate the upload directory (the portion you bolded), you can add this code below your getRecords() calls:

$schema = loadSchema('chs_galleries');
list($uploadDir,$uploadUrl) = getUploadDirAndUrl($schema['gallery_images']);

$uploadUrl should now contain the directory the images are in. You can then change your image src to:  

src="<?php echo $uploadUrl . htmlencode($upload['thumbUrlPath']) ?>"

Let me know if that solves the issue, or if I can help with anything else.

Thanks,

Daniel
Technical Lead
interactivetools.com