-
☆ A M B ☆
- 24,524 Posts
Give it a try. Nothing is written in stone around here. Just make sure to test for corner conditions, like if there is only one entry.
I gave it a try but still no luck.
Quote from: 2wistd at Jan 21, 2008, 08:56 PM
upon inspection...this line doesn’t seem right to me.
$random = (rand(1, $limit) - 1);
seems like it would need to be $random = (rand(1, $limit - 1));
because the other line seems like it is subtracting the 1 from the whole statement (1, $limit), rather then $limit?
Am I right in this assumption?
I don’t think so. $limit should be the number of items. $limit<1 doesn’t change it, it’s just a test. If you have 4 items, $limit will be 4. rand(1,$limit) will return a number from 1-4 and subtracting 1 will give a number from 0-3 -- as it should. The test Sottwell suggests should confirm that.
Another useful test might be to add:
It will be a little hard to read, but you can see if the first and last quotes are there and whether any blank ones are coming back.
Bob
-
☆ A M B ☆
- 24,524 Posts
I always use
echo "<pre>";
print_r($resource);
echo "</pre>";
in fact I often add a function "checkArray($resource)" that contains those lines to anything I’m doing so all I need to do is call it with the name of whatever I want to display. That might make a nice utility function to add to the MODx api, actually.
This may not be the right thread in which to post, and apologies in advance.
I have seen a few of the random* snippets in the repository, however what I am looking for perhaps extends a little beyond what is currently available.
I am wanting to randomly (or in some sort of order) display a set of sponsiored ad links. (Gif, jpg, png etc image linked to the persons site).
Now I thought that random ad would do this, but when I read through the description it mentioned only linking to other documents within the modx system, which is off course not what I am wishing to achieve.
Has anyone got any thoughts or ideas if this can be done and how? Perhaps it is being planned already?
Regards,
Tom.
How could I show content AND the image in an image TV connected to each document in the random folder?
I think, thererfor I am! But what I am, and why...?
Quote from: goldeagle at Mar 01, 2008, 01:10 AM
This may not be the right thread in which to post, and apologies in advance.
I have seen a few of the random* snippets in the repository, however what I am looking for perhaps extends a little beyond what is currently available.
I am wanting to randomly (or in some sort of order) display a set of sponsiored ad links. (Gif, jpg, png etc image linked to the persons site).
Now I thought that random ad would do this, but when I read through the description it mentioned only linking to other documents within the modx system, which is off course not what I am wishing to achieve.
The snippet returns the content of a random document in the folder. If you put the URL of an ad in the content field of each document there, you can show them on your page by putting the snippet call in the img tag:
<img src="[!RandomQuotes? "eid=`123` !]" alt ="random advertisement">
Quote from: Sylvaticus at Mar 16, 2009, 07:43 AM
How could I show content AND the image in an image TV connected to each document in the random folder?
You’d have to either create a tpl chunk for the snippet, or just hard-code both the image and text into the snippet code, something like this:
// RandomQuote
// Snippet to produce by sottwell.
// Posted by web_designer.
// Dated 25th Sepember 2006.
$resourceparent = $quoteid; // the folder that contains quote documents
$output = ''; // initialise the quote variable
$resource = $modx->getActiveChildren($resourceparent,'createdon', 'DESC', $fields='content');
$limit=count($resource);
if($limit<1) {
$output .= "No entries found.<br />";
return $output;
}
$random = (rand(1, $limit) - 1);
$output .= '<img src="' . $resource[$random][['imageTVName'] . '">';
$output .= '<p>' . $resource[$random]['content'] . '</p>'; // get a random quote
return $output;
This assumes that the "image TV" contains the URL of the image, not the image itself.
BTW,
I haven’t tested it, but I think array_rand() would be a better choice for selecting the random array member:
Replacing this:
$
random = (rand(1, $limit) - 1);
with
$random = array_rand($resource);
And, while I’m thinking about it, what is probably better all around is a snippet that would rotate through the ads instead of picking one at random. I think there is such a snippet but, if not, it should be simple to write.