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
Share on other sites

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?

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

;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);

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.