Random display of entries

7 posts by 2 authors in: Forums > CMS Builder
Last Post: December 9, 2009   (RSS)

I am trying to get my web site to display quotes on pages based on the category of the quote.

For example, in the directory www.tsuluwerks.com/shamelessyou/, I would like for the quotes on the left of the page to show only if they have been categorized as "shameless".

I am using multiple checkboxes to categorize the quotes.

When I use the code below, it randomly cycles through the quotes. I notice that it's cycling through ALL of them, not just the ones marked "shameless". As a result, the page is often blank.

I have temporarily turned this off, but can anyone recommend why this code is not cycling through just the quotes that are categorized as Shameless? I tried checking capitalization and changing !== false to !== 0, == true, and == 1.

Thanks,
Tracy

<?php

require_once "E:/USERS/tsuluwerksinc/www/cmsAdmin/lib/viewer_functions.php";

list($quotesRecords, $quotesMetaData) = getRecords(array(
'tableName' => 'quotes',
'limit' => '1',
'orderBy' => 'RAND()',
'loadUploads' => '0',
'allowSearch' => '0',
));

?>


<!-- STEP2: Display Records (Paste this where you want your records to be listed) -->
<?php foreach ($quotesRecords as $record): ?>
<?php if (strpos($record['categories'], "\t" . 'Shameless' . "\t") !== false): ?>
<p class="quote"><?php echo $record['quote'] ?></p>
<p class="quoteAtt"> &#151; <?php echo $record['name'] ?>, <?php echo $record['company'] ?></p>
<?php endif; ?>
<?php endforeach; ?>

Re: [tsuluwerks] Random display of entries

By Dave - December 9, 2009

Hi tsuluwerks,

What's happening is that it's loading 1 random record (regardless of category), and then if it's not in the right category it's not showing it.

Try this where to only get records that are in the category Shameless:
'where' => " categories LIKE '%\tShameless\t%' ",

Let me know if that works for you.
Dave Edis - Senior Developer
interactivetools.com

Re: [Dave] Random display of entries

Hi, Dave:

Unfortunately, it did not work. A sample of the results can be found at http://www.tsuluwerks.com/shamelessyou/. If the page is refreshed, sometimes a quote shows up, sometimes it doesn't.

What do you think?

Re: [tsuluwerks] Random display of entries

By Dave - December 9, 2009

Try adding this line to see what the database query is returning:

<?php showme($quotesRecords); ?>

Let me know what that says.
Dave Edis - Senior Developer
interactivetools.com

Re: [Dave] Random display of entries

I received this reply

Fatal error: Call to undefined function: showme() in E:\USERS\tsuluwerksinc\www\common\quotes-shameless.php on line 26

Re: [tsuluwerks] Random display of entries

By Dave - December 9, 2009

Ahh, ok, you must have an older version. Try this:

<?php
print "<xmp>";
print_r($quotesRecords);
print "</xmp>";
?>

The idea is to see where the bad data is coming from. That will show you the contents of the data being loaded from the database. If it looks right then it must be something in the viewer code on the page.

If it doesn't look right then we need to look at the where being used to load the records.

Hope that helps! Let me know what that says.
Dave Edis - Senior Developer
interactivetools.com