Show Random Items from Multiple Sections

9 posts by 2 authors in: Forums > CMS Builder
Last Post: September 26, 2008   (RSS)

By zip222 - September 26, 2008

I would like to show a random featured project on the home page of a site, but the projects are divided into five different sections in CMSB. I know exactly how to do this from one section, but not sure if there is a way to do this across multiple sections.

I tried using a random word generator pulling that pulled from a list of the 5 sections. I then tried to use that variable throughout the generated code, but I couldn't get that to work. Anyone have any ideas?

Re: [jdancisin] Show Random Items from Multiple Sections

By MisterJim - September 26, 2008

What you described having done seems like it would be one way to do it.

To recap my understanding of what you tried:

1. Pull one random section name from a list of sections names.
2. Use a variable to input that section name into your code that pulls the project to be displayed.

Would you mind posting some of the code you used when you tried this method?

An alternative might be to keep all the projects in one section, but use a drop down list (or other selection device) of 'project categories' to specify to which 'project category' the individual project belongs. Then use that variable in your single randomization statement. This would have the added benefit of making it easy to change a 'project category' for an individual project if you later decide to do so.

Jim
WebCamp One, LLC



Websites That Work

Re: [jdancisin] Show Random Items from Multiple Sections

By MisterJim - September 26, 2008

Try removing the single quote marks from around the variable, like this:

list($projectRecords, $projectMetaData) = getRecords(array(
'tableName' => $randomsection,
'allowSearch' => '0',
'where' => 'feature = 0',

since you're just pulling one section, you might want to also include:

'limit' => '1',

Hope this helps.

Jim
WebCamp One, LLC



Websites That Work

Re: [Mr Jim] Show Random Items from Multiple Sections

By zip222 - September 26, 2008

that almost works! here is the error message I am getting:

getRecords(interactive ): Couldn't load schema for 'interactive '!

The problem is that its inserting a space at the end of the section title. I think is happening because there is a return after each section name in the external file (words.txt). Is there a another method to provide the section names to the randomizer so that it doesn't add the space at the end of each? It doesn't need to come from an external file - that is just the way I found when I did a search.

Re: [jdancisin] Show Random Items from Multiple Sections

By MisterJim - September 26, 2008

Hmm. It worked for me when I duplicated your code on my test setup.

Before you drive yourself nuts, make sure that you are using a plain text editor when you create your external .txt file. There's probably a setting that will allow you to view invisible characters in the file. Make sure that the only thing at the end of each word is a UNIX line ending... I'm assuming that you're on a Unix server.

With most text editors you can set the type of line endings that they insert into the file.

Then it should work.

Jim
WebCamp One, LLC



Websites That Work

Re: [Mr Jim] Show Random Items from Multiple Sections

By zip222 - September 26, 2008

The problem is definitely the returns, because when the randomizer grabs the very last word, which doesn't have a return after it, then everything works perfectly.

I am using TextEdit on a Mac, saving in the TXT format. I am on a Unix server.

Re: [jdancisin] Show Random Items from Multiple Sections

By zip222 - September 26, 2008 - edited: October 29, 2008

Got it! Rather than grabbing the section titles from an external file, I defined them inline. Here is what I used instead:

$section[1] = "strategy";
$section[2] = "branding";
$section[3] = "interactive";
$section[4] = "design";
$section[5] = "investor";

$random_select = rand(1, 5);
$randomsection = $section[$random_select];


works now:
[removed url]


Thanks for your help!

Re: [jdancisin] Show Random Items from Multiple Sections

By MisterJim - September 26, 2008

Great. That's exactly what I was working on and was just ready to shoot you some code. Glad you worked it out!

Jim
WebCamp One, LLC



Websites That Work