Jump to content

Display *multiple* quotes randomly


foxhill

Recommended Posts

I came across the following php code to display a random quote (that I have modified only slightly).  How can I modify it to accept a variable to determine how many quotes to display without duplication?

 

$quote[] = 'blah blah blah html code';

$quote[] = 'blah blah blah more html code';

$quote[] = 'blah blah blah some other html code';

 

srand ((double) microtime() * 1000000);

$random_number = rand(0,count($quote)-1);

 

echo ($quote[$random_number]);

Link to comment
https://forums.phpfreaks.com/topic/124987-display-multiple-quotes-randomly/
Share on other sites

Do this.

 

$numQuotes = 2;
$quotes[] = 'blah blah blah html code';
$quotes[] = 'blah blah blah more html code';
$quotes[] = 'blah blah blah some other html code';
shuffle($quotes);
$randQuotes = array_slice($quotes, 0, $numQuotes); // Returns 2 random of the three quotes in array form.

You could give this a try:

 

$numQuotes = 2;
$i = 0;
$quotes = array();
$quotes[] = 'blah blah blah html code';
$quotes[] = 'blah blah blah more html code';
$quotes[] = 'blah blah blah some other html code';
while($i < $numQuotes){
     echo $quotes[array_rand($quotes)].'<br />';
     $i++;
}

I am trying genericnumber1's solution, but I cannot get it to actually print anything.  In the html, I have:

 

<?php @include("includes/testimonials.php"); ?>

 

When I removed the @ I did not have any errors, so I put it back in.  I notice there is no echo statement, so I tried adding one.  My html file is www.foxhilldesign.com/test.php and the script, obviously, is at www.foxhilldesign.com/includes/testimonials.php.  What am I missing?

You need to add an echo statement, however you want to handle the display... the simplest would be something like

 

$numQuotes = 2;
$quotes[] = 'blah blah blah html code';
$quotes[] = 'blah blah blah more html code';
$quotes[] = 'blah blah blah some other html code';
shuffle($quotes);
$randQuotes = array_slice($quotes, 0, $numQuotes); // Returns 2 random of the three quotes in array form.
echo implode('<br />', $randQuotes);

 

This prints both lines with a

<br />

between them. If you have them formatted already, you could just do

 

echo implode('', $randQuotes);

;D

That works exactly like what I wanted.  I've never seen the array_slice() or implode() functions before, but they look like two I should become familiar with.  This is awesome...Thanks!

 

You need to add an echo statement, however you want to handle the display... the simplest would be something like

 

$numQuotes = 2;
$quotes[] = 'blah blah blah html code';
$quotes[] = 'blah blah blah more html code';
$quotes[] = 'blah blah blah some other html code';
shuffle($quotes);
$randQuotes = array_slice($quotes, 0, $numQuotes); // Returns 2 random of the three quotes in array form.
echo implode('', $randQuotes);

 

This prints both lines with a

<br />

between them. If you have them formatted already, you could just do

 

echo implode('', $randQuotes);

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.