'Where' to find an answer!

18 posts by 4 authors in: Forums > CMS Builder
Last Post: October 23, 2008   (RSS)

By Perchpole - July 1, 2008

I'm trying to develop a random image display for my client who makes and sells military models. I want to pad out his home page by loading a new image every time a visitor opens the page. The data is drawn straight from the product catalogue (so no need to enter the info twice). The only problem is that some of the products don't have images. That means every once in a while the rotator will load a blank page - which won't look good.

So, I'd like to use a 'where' clause to ensure the page only displays records that do have images. Trouble is, I can't work out what code to use...

$options['where'] = 'xxx';




(NB: This site still uses the old CMS Builder code format)

The upload field is called "images". Currently the page is configured to load the thumbnail of the image.

Any help would be much appreciated.

:0)

Perch

Re: [Perchpole] 'Where' to find an answer!

By Dave - July 2, 2008

Hi Perch,

Do you need any information from the record or just the image? I have two ideas in mind.

1) You could just load 10 records (sorted randomly), display them in a loop and stop after we hit the first image. We'd have to count on the fact that one of those 10 records had images defined though.

2) All the uploads are stored in a separate table called 'uploads'. We could query that table directly for a random upload for the table and field. If you could let me know the name of your table and upload field I can write up some code.

Hope that helps!
Dave Edis - Senior Developer
interactivetools.com

Re: [Dave] 'Where' to find an answer!

By Perchpole - July 2, 2008

Hi, Dave -

Given a choice I suspect I'd prefer the second option.

The tablename is: products_classic

The upload field is: images

Thanks,

:o)

Perch

Re: [Perchpole] 'Where' to find an answer!

By Jake - July 2, 2008

Hi Perch,

Thanks for your reply!

Here's the code you'll want to use for this:

<?php

list($uploadRecords, $uploadMetaData) = getRecords(array(
'tableName' => 'uploads',
'limit' => '1',
'orderBy' => 'RAND()',
'where' => 'tableName = "classic" && fieldName = "images"',
));

?>

<?php foreach ($uploadRecords as $record): ?>
<?php echo $record['urlPath'] ?>
<?php endforeach ?>


All that code does is randomly return the URL for one image upload in your database. If this doesn't work out for you, drop me an email at jake@interactivetools.com with your FTP information so I can take a look at the code being used on this page directly.

Keep us updated with how this is working out. [:)]
-----------------------------------------------------------
Cheers,
Jake Marvin - Product Specialist
support@interactivetools.com

Hire me!
Save time by getting our experts to help with your project. Template changes, advanced features, full integration, whatever you need. Whether you need one hour or fifty, get it done fast with Priority Consulting.

Re: [Jake] 'Where' to find an answer!

By Perchpole - July 2, 2008

Hi, Jake -



Thanks for the code. I've just tested it on one of my newer CMSB sites and it works perfectly.

However, as I mentioned in my original, the site that I revisiting with this image rotator is an older CMSB installation which uses the old code format. As a result, your code (in its current format) does not work. I've tried to adapt it, purely by following my nose, and this is the result:

<?php
require_once "web/viewer_functions.php";
$options = array();
$options['tableName'] = 'uploads';
$options['titleField'] = '';
$options['viewerUrl'] = '/classic/products_classicPage.php';
$options['orderBy'] = 'RAND()';
$options['pageNum'] = '';
$options['perPage'] = '1';
$options['where'] = 'tableName = "products_classic" && fieldName = "images"';
$options['useSeoUrls'] = '';

list($uploadRecords, $uploadMetaData) = getRecords($options);[/#ff0000]
?>


Unfortunately, it doesn't seem to work. All I get is an error message indicating a "Call to undefined function: getrecords()... " in the line hi-lighted in red above.

Any tips?

:0)

Perch

Re: [Perchpole] 'Where' to find an answer!

By Jake - July 2, 2008

Hi Perch,

Sorry about that!

Try this code instead:

<?php
require_once "web/viewer_functions.php";
$options = array();
$options['tableName'] = 'uploads';
$options['titleField'] = '';
$options['viewerUrl'] = '/classic/products_classicPage.php';
$options['orderBy'] = 'RAND()';
$options['pageNum'] = '';
$options['perPage'] = '1';
$options['where'] = 'tableName = "products_classic" && fieldName = "images"';
$options['useSeoUrls'] = '';

list($uploadRecords, $uploadMetaData) = getListRows($options);
?>

<?php foreach ($uploadRecords as $record): ?>
<?php echo $record['urlPath'] ?>
<?php endforeach ?>


Let us know if that does the trick. [:)]
-----------------------------------------------------------
Cheers,
Jake Marvin - Product Specialist
support@interactivetools.com

Hire me!
Save time by getting our experts to help with your project. Template changes, advanced features, full integration, whatever you need. Whether you need one hour or fifty, get it done fast with Priority Consulting.

Re: [Jake] 'Where' to find an answer!

By Perchpole - July 3, 2008

Jake -

Excellent. Once again it works perfectly.

Now, taking things on a step - towards their natural conclusion...! Is there any way I can pull more details from the upload's main data?

In a perfect world what you'd want is the ['title'] of the record to appear below the upload which also acts as a ['_link'] to the page viewer.

So, the visitor is presented with a random image under which appears the title of the item. The visitor then clicks on the title to view the full page of information.

Can you see where I'm going with this? Is it do-able?

:0)

Perch

Re: [Jake] 'Where' to find an answer!

By Perchpole - July 3, 2008

Hi, Jake -

That works fine. However, if you're up for a challenge, I'd still like to see if you could think your way around my earlier post - regarding the use of the other data associated with the image/upload.

Most records have ['title'] and ['content'] fields and I thought it might be useful to learn if there was a way to show this information beneath the images called by the rotation code. Being able to show the ['title'] of the record would be particularly helpful.

I appreciate I could use the ['info1'] code - but this doesn't always tell the full story!

Just a thought.

:0)

Perchpole

Re: [Perchpole] 'Where' to find an answer!

By Dave - July 4, 2008

Hi Perch,

We're getting pretty advanced here, and it requires a few queries to get all the data, but one way is to use a viewer to load the record based on the record number that is attached to the upload:

<?php
require_once "web/viewer_functions.php";

// load upload
$options = array();
$options['tableName'] = 'uploads';
$options['titleField'] = '';
$options['viewerUrl'] = '/classic/products_classicPage.php';
$options['orderBy'] = 'RAND()';
$options['pageNum'] = '1';
$options['perPage'] = '1';
$options['where'] = 'tableName = "products_classic" && fieldName = "images"';
$options['useSeoUrls'] = '';
list($uploadRecords, $uploadMetaData) = getListRows($options);
$upload = @$uploadRecords[0]; // get first upload



// load record
$recordNum = @$upload['recordNum'];
$options = array();
$options['tableName'] = 'products_classic';
$options['titleField'] = '';
$options['viewerUrl'] = '/classic/products_classicPage.php';
$options['pageNum'] = '1';
$options['perPage'] = '1';
$options['where'] = "num = '$recordNum'";
$options['useSeoUrls'] = '';
list($productRecords, $productMetaData) = getListRows($options);
$product = @$productRecords[0]; // get first product
?>


Then you should have your upload in $upload and the record it came from in $record. Note, I wrote this code in the forum, it should be fine but may require some tweaks. Let me know if you have any problems with it.

Hope that helps!
Dave Edis - Senior Developer
interactivetools.com