Image Rotator Question

5 posts by 2 authors in: Forums > CMS Builder
Last Post: January 6, 2015   (RSS)

Hi All,

I’m using an upload field in my accounts table to hold multiple images. These images are to be used in a single image rotator which  displays a single random image from a random account.

In the displayed image I’m using the filename to display the title of the image (removing the .jpg and other extraneous information with preg_replace).

I’m also displaying the name of the artist from the name fields in the account record.

This first code block, which I cobbled together from another implementation, works, but it since it loops through all of the account record images and then displays one image, I’m concerned about server loads as the number of records and images increases. I'm also not sure how to get around the  <?php if (@$alreadySeen[ $record[''] ]++) { continue; } ?> code which I don't think is valid code but seems to work.

<?php  
    list($accountsRecords, $accountsMetaData) = getRecords(array(
    'tableName'   => 'accounts',
    'loadUploads' => true,
    'allowSearch' => false,
     'orderBy'     => 'RAND()',
  ));
  ?>

<table align="center" width="80%" border="0" cellpadding="5">
  <tr>
    <?php foreach ($accountsRecords as $record): ?>
    <?php shuffle($record['images']) ?>
    <?php foreach ($record['images'] as
$upload): ?>
    <?PHP $upload['filename'] = preg_replace('/\.\w+$/', '', $upload['filename']); ?>
    <?PHP $upload['filename'] = preg_replace("/[-_]/", " ", $upload['filename'] ); ?>
    <?PHP $upload['filename'] = ucwords($upload['filename'] ); ?>
    <?php if (@$alreadySeen[ $record[''] ]++) { continue; } ?>
    <td align="center"><div onclick='window.location="<?php echo $record['url'] ?>"' class="round-corner" style="position: relative; background: url(<?php echo
$upload['thumbUrlPath2'] ?>); width: <?php echo $upload['thumbWidth2'] ?>px; height: <?php echo $upload['thumbHeight2']
?>px;">
        <div style="position: absolute; top: .9em; left: 1.2em; width: 200px; font-family:Verdana, Tahoma, Arial, Sans-Serif;
font-size:1.2em; font-weight:bold; text-align:left; font-style: normal; color: #00F;" class="shadow1"><?php echo $upload['filename']
?></div>
        <div style="position: absolute; bottom: 1.2em; left: 1.2em; width: 200px; font-family:Verdana, Tahoma, Arial, Sans-Serif;
font-size:1.2em; font-weight:bold; text-align:left; font-style: normal; color: #00F;" class="shadow1"><?php echo $record['first_name']
?> <?php echo $record['last_name']
?></div>
      </div></td>
    <?php endforeach ?>
    <?php endforeach ?>
  </tr>
</table>

In this second block, I’m using a mysql_select to limit the loaded records to those that contain images.

<?php $uploadRecords = mysql_select('uploads', " tableName = 'accounts' AND fieldName = 'images'"); ?>

<table align="center" width="80%" border="0" cellpadding="5">
  <tr>
    <?php shuffle($uploadRecords) ?>
    <?php foreach ($uploadRecords as
$upload): ?>
    <td align="center"><div onclick='window.location="#"' class="round-corner" style="position: relative; background: url(<?php echo $masterurl ?>/cmsAdmin/uploads/<?php echo
$upload['thumbUrlPath2'] ?>); width: <?php echo $upload['thumbWidth2'] ?>px; height: <?php echo $upload['thumbHeight2']
?>px;">
        <?PHP $upload['urlPath'] = preg_replace('/\.\w+$/', '', $upload['urlPath']); ?>
        <?PHP $upload['urlPath'] = preg_replace("/[-_]/", " ", $upload['urlPath'] ); ?>
        <?PHP $upload['urlPath'] = ucwords($upload['urlPath'] ); ?>
        <div style="position: absolute; top: .9em; left: 1.2em; width: 200px; font-family:Verdana, Tahoma, Arial, Sans-Serif;
font-size:1.2em; font-weight:bold; text-align:left; font-style: normal; color: #00F;" class="shadow1"><?php echo $upload['urlPath']
?></div>
        <?php  
    list($accountsRecords, $accountsMetaData) = getRecords(array(
    'tableName'   => 'accounts',
    'loadUploads' => true,
    'allowSearch' => false,
    
  ));
  ?>
        <?php foreach ($accountsRecords as $record2): ?>
        <div style="position: absolute; bottom: 1.2em; left: 1.2em; width: 200px; font-family:Verdana, Tahoma, Arial, Sans-Serif;
font-size:1.2em; font-weight:bold; text-align:left; font-style: normal; color: #00F;" class="shadow1">
          <?php  echo $record2['first_name']
?>
          <?php echo $record2['last_name']
?></div>
      </div></td>
    <?php break?>
    <?php endforeach ?>
    <?php break?>
    <?php endforeach ?>
  </tr>
</table>

My Problem is that I’m not sure how to pull the record name fields from the image’s parent record. The existing code currently shows the name of the first record each time.

Any help would be great.

Thanks,

Jerry Kornbluth  

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

Hey Jerry,

I just want to make sure I understand. So in the second block you'd like to modify the $uploadRecords that are loaded, so that they only retrieve records that are loaded in the first block?

Does the first block display more than one record? It looks like this code:

 if (@$alreadySeen[ $record[''] ]++) { continue; }

would return true or false every time, so I'm guessing the for each loop through all of the returned records each time?

Thanks,

Greg

Greg Thomas







PHP Programmer - interactivetools.com

Hi Jerry,

Happy new year! I think I see what you're trying to do, so the first name and last name aren't matching the retrieved image? I've modified the second code block to retrieve the user details of the image uploader:

<?php $uploadRecords = mysql_select('uploads', " tableName = 'accounts' AND fieldName = 'images'"); ?>
<table align="center" width="80%" border="0" cellpadding="5">
  <tr>
    <?php shuffle($uploadRecords) ?>
    <?php foreach ($uploadRecords as $upload): ?>
    <td align="center">
      <div onclick='window.location="#"' class="round-corner" style="position: relative; background: url(<?php echo $masterurl ?>/cmsAdmin/uploads/<?php echo
$upload['thumbUrlPath2'] ?>); width: <?php echo $upload['thumbWidth2'] ?>px; height: <?php echo $upload['thumbHeight2']
?>px;">
      <?php
      
        $upload['urlPath'] = preg_replace('/\.\w+$/', '', $upload['urlPath']); 
        $upload['urlPath'] = preg_replace("/[-_]/", " ", $upload['urlPath'] ); 
        $upload['urlPath'] = ucwords($upload['urlPath'] ); 

      ?>
        <div style="position: absolute; top: .9em; left: 1.2em; width: 200px; font-family:Verdana, Tahoma, Arial, Sans-Serif; font-size:1.2em; font-weight:bold; text-align:left; font-style: normal; color: #00F;" class="shadow1">
          <?php echo $upload['urlPath']; ?>
        </div>
        <?php  
          list($accountsRecords, $accountsMetaData) = getRecords(array(
          'tableName'   => 'accounts',
          'where'       => "`num` = '{$upload['recordNum']}'"
          'loadUploads' => false,
          'allowSearch' => false,
          'limit'       => '1',
        ));
        ?>
        <?php foreach ($accountsRecords as $record2): ?>
          <div style="position: absolute; bottom: 1.2em; left: 1.2em; width: 200px; font-family:Verdana, Tahoma, Arial, Sans-Serif; font-size:1.2em; font-weight:bold; text-align:left; font-style: normal; color: #00F;" class="shadow1">
            <?php  echo $record2['first_name']; ?> <?php echo $record2['last_name']; ?>
          </div>
        <?php endforeach ?>
      </div>
    </td>
  </tr>
</table>
<?php break?>
<?php endforeach ?>

So the code above will only retrieve one record, which should speed up the page load times as not all of the records are being retrieved. I've also added a where statement that will retrieve the account record based on the upload record chosen, as the upload section stores the record number value in a field called recordNum. 

Cheers,

Greg

Greg Thomas







PHP Programmer - interactivetools.com

Thanks Greg,

I'd forgotten about the $upload['recordNum']

It now works extremely well and I'll add this recipe to the CMSBCookbook.

Best,

Jerry Kornbluth

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php