Forum Moderators: coopster
What I'm trying to do is to display a random testimonial quote that would change on each HTML page. The quotes are located called randomTestimonial.php.
PHP code:
<?php
$testimonial[] = "copy1";
$testimonial[] = "copy2";
$testimonial[] = "copy3";
shuffle($testimonial);
?>
I'm trying to place these quotes in a formatted <div> container. Here's the HTML that I've tried to get working.
<div class="nav_testimonial">
<?
include "css/randomTestimonial.php"
print($testimonial);
?>
</div>
Makes sense to me, but doesn't work.
try:
<?php
$testimonial[1] = "copy1";
?>
or use array_push() to get strings into an array.
Since $testimonial is an array, you can't print() it without specifying which index to print.
<?
print($testimonial);
?>
<?
print($testimonial[1]);
?>
But:
You are asking it to print $testimonial but that is an array not a single part of an array.
print($testimonial[0]); would work but will be far ideal. So you really want is:
include('css/randomTestimonial.php');
$num = (random number between 0 and your number of testimonials)
print $testimonial[$num];
Hope that helps. Search how to get a random item;
For this particular purpose, shuffle is perfectly fine to use to get a random item from an array. And technically, there is no such thing as a perfect random number :)
You could also use rand [us3.php.net], or one of its brother functions, to generate a random array index.
For this particular purpose, shuffle is perfectly fine to use to get a random item from an array. And technically, there is no such thing as a perfect random number :)You could also use rand, or one of its brother functions, to generate a random array index.
The idea was to show a random testimonial. Using shuffle will show some testimonials far more often than others. Not random at all.
I know that 'technically, there is no such thing as a perfect random number' but you can get far closer to it than using shuffle.
shuffle
<?php
for($i = 1; $i < 25; $i++)
{
$nums = array('1','2','3','4','5','6','7','8','9','10');
shuffle($nums);
echo $i.": ".$nums[1]."<br>";
}
?>
rand
$nums = array('1','2','3','4','5','6','7','8','9','10');
for($i = 1; $i < 25; $i++)
{
$rand = rand(0,9)
echo $i.": ".$nums[$rand]."<br>";
}
My conclusion is, because of how rand() works, it is not good if you are calling it within a loop because it will frequently have the same value as the number before. Shuffle, however, seems to be random in the loop. Both overall display random results.
firstly
ran·dom
1. Having no specific pattern, purpose, or objective: random movements. See Synonyms at chance.
2. Mathematics & Statistics. Of or relating to a type of circumstance or event that is described by a probability distribution.
3. Of or relating to an event in which all outcomes are equally likely, as in the testing of a blood sample for the presence of a substance.
to have something be random just means patternless or "all outcomes are equally likely"
>> because it will frequently have the same value as the number before
not true, if it is random, then it is random, the same number may reoccur because for each iteration every number in your sequence has an equal probability of occuring
there are functions that are more truly random than others. You used to always have to seed rand functions because otherwise they would produce the same sequence over and over. There is an explanation as to why, you can look it up if you like. ;)
secondly there seems to be confusion as to how shuffle works
[php.net...]
This function shuffles (randomizes the order of the elements in) an array.
shuffle is also random and as far as I know the quality of randomness is equal for both functions
therefore
the argument is moot, the two are equal ;)
if we wanted to show the testimonials with an equal weighting then that would be something completely different