I appreciate your trust in my PHP abilities, but I assure you, they are unfounded. 🙂 Yes, I use strings (a lot) and includes (a lot) but that's literally the totality of my PHP knowledge. I wouldn't even know where to begin to do this. By @Under S. Start by producing an array of php objects, see sample php code below. You can store this array of objects in a seperate php include file, name it 'quotes.php' $quotes = [
[
'quote' => 'A rose by any other name would smell as sweet.' ,
'author' => 'William Shakespeare'
],
[
'quote' => 'Tis better to have loved and lost than never to have loved at all.' ,
'author' => 'Alfred Lord Tennyson'
],
[
'quote' => 'He travels the fastest who travels alone.' ,
'author' => "Rudyard Kipling"
],
[
'quote' => 'No one can make you feel inferior without your consent.' ,
'author' => 'Eleanor Roosevelt'
]
]; At the end of the quotes array of objects, after the closing array bracket ]; and still in the 'quotes.php' includes file, add the following: shuffle($quotes);
$selectedQuote = array_slice($quotes , 1); The above shuffles the $quotes array of objects, like a pack of cards, on each page load. The next line selects the 1st object in the array of objects, after the array has been shuffled, so the quote is random. The more quotes obviously the more random the quote will be on each page load. Once the 'quotes.php' includes file has been included/linked to your main page where you want the random quote to appear then in the main page add the following where you require the random quote to appear: <blockquote>
<?php echo $selectedQuote[0]['quote']."<br><span>".$selectedQuote[0]['author']."</span>"; ?>
</blockquote> Style the quote with some css, how you require it to look: blockquote {
font-size: 20px;
}
blockquote span {
font-size: 15px;
font-weight: bold;
} That's it! You should now have a random quote on each page load. As a bonus, but not necessary, you can fade in the quote by adding the <script></script> below to your main page: <script>
const blockquote = document.querySelector('blockquote');
setTimeout(function() {
blockquote.classList.add('fadeIn');
}, 500)
</script> Then updating the css: blockquote { font-size: 20px; opacity: 0; transition: opacity 1s ease-in-out; } blockquote span { font-size: 15px; font-weight: bold; } .fadeIn { opacity: 1; }
... View more