Looks like a nice snippet. Thanks for your contribution.
I see that the snippet call didn’t show up in your instructions in the repository, most likely because MODx thinks it’s a real snippet and can’t find it. Let me mention a little trick I learned from RThrash. Put an empty comment between the brackets of your snippet call to keep MODx from interpreting it as a snippet:
[<!-- -->[Snippet call here ]<!-- -->]
Bob
Hey Bob. Thanks for the tip! I will be sure to keep that in mind in the future.
Nice snippet, thanks. Seems to work fine, except for the occasional blank line -- formatting items display fine, but only blanks are lifted from the database -- looks like this, when that happens:
" " --
instead of how it’s supposed to look:
"Quote" -- Reference
How can I fix that?
-
☆ A M B ☆
- 24,524 Posts
Sounds like an array indexing error; arrays are indexed from 0 to count - 1. If you have six items, you can get items 0 through 5; if you use the count() function then try to use 6, you’ll get a blank since there is no item #6 in the array.
Hmmm, the code seems to be okay in that regard. Early on it counts the number of quotes with:
$numQuotes = count ($lines);
and then it uses that result to select a random number with:
if ($quoteType == 0)
{
$ranNum = rand(1, $numQuotes); // Quote changes on browser reload
}
and then outputs the quote and reference with
$line = $lines[$ranNum-1];
$element = explode("::",$line);
$theQuote = $element[0];
$theRef = $element[1];
$output = outputLine($theQuote, $theRef);
@tcochran:
I’m having the same issue. Did you have any luck resolving it?
I changed the parameters on the rand function to the following:
$ranNum = rand(0, $numQuotes-1);
I’d seen an old comment online that the "rand() max value is always 1 greater than it should be." I don’t know if that’s true or not, but so far, it seems to be working okay.
Of course, the results ARE random, so it’s hard to say for sure. :~)
Correction to earlier post: Change to code should be:
$ranNum = rand(1, $numQuotes) -1;
If you change
$line = $lines[$ranNum-1];
to
$line = array_rand($lines);
I think it would be faster and you wouldn’t need $count or any random number.