foxhill Posted September 19, 2008 Share Posted September 19, 2008 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]); Quote Link to comment https://forums.phpfreaks.com/topic/124987-display-multiple-quotes-randomly/ Share on other sites More sharing options...
Maq Posted September 19, 2008 Share Posted September 19, 2008 Could you please provide an example, I don't think I fully understand your problem. Quote Link to comment https://forums.phpfreaks.com/topic/124987-display-multiple-quotes-randomly/#findComment-645810 Share on other sites More sharing options...
genericnumber1 Posted September 19, 2008 Share Posted September 19, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/124987-display-multiple-quotes-randomly/#findComment-645821 Share on other sites More sharing options...
The Little Guy Posted September 19, 2008 Share Posted September 19, 2008 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++; } Quote Link to comment https://forums.phpfreaks.com/topic/124987-display-multiple-quotes-randomly/#findComment-645827 Share on other sites More sharing options...
discomatt Posted September 19, 2008 Share Posted September 19, 2008 genericnumber1 has the ideal solution to this issue. Quote Link to comment https://forums.phpfreaks.com/topic/124987-display-multiple-quotes-randomly/#findComment-645832 Share on other sites More sharing options...
AV1611 Posted September 19, 2008 Share Posted September 19, 2008 isn't there a possibility of the same random number quote being produced in the code you suggest? Quote Link to comment https://forums.phpfreaks.com/topic/124987-display-multiple-quotes-randomly/#findComment-645841 Share on other sites More sharing options...
foxhill Posted September 19, 2008 Author Share Posted September 19, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/124987-display-multiple-quotes-randomly/#findComment-645848 Share on other sites More sharing options...
genericnumber1 Posted September 19, 2008 Share Posted September 19, 2008 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); Quote Link to comment https://forums.phpfreaks.com/topic/124987-display-multiple-quotes-randomly/#findComment-645870 Share on other sites More sharing options...
foxhill Posted September 19, 2008 Author Share Posted September 19, 2008 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); Quote Link to comment https://forums.phpfreaks.com/topic/124987-display-multiple-quotes-randomly/#findComment-645879 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.