Skip to main content
Inspiring
April 24, 2024
Answered

How do I randomize quotes on a page (without using databases)

  • April 24, 2024
  • 1 reply
  • 2225 views

Is there a way for a coding fraud like myself to easily implement a "random quote" for my site's front page that does not involve databases? I don't need it to rotate on a timer, I just need the page to display a different quote each time it's loaded. No frills.

 

I'm pretty comfortable with HTML/CSS at this point, but only know PHP includes & strings (so I can't really say I know PHP). Don't know any Javascript at all, beyond just placing existing code where I'm told to place it.

 

In a perfect world, the front page would pull from a sheet located where the web pages are; where every 2 lines is a new quote. So the odd lines would hold the quote, and the even lines the authors of those quotes. (Unless someone has a better idea, but this seemed the most obvious using only 1 sheet.)

 

I assume it would be preferable to do the quote-pulling in PHP since it can do it locally, right? (It's my understanding that this would allow the downloading of only the quote being used, instead of the full list, every time the front page is loaded). There should be between 50 and 200 quotes in all.

 

Could someone help me out with the code, or point me to a tutorial that will walk me through it?

 

Thanks!

 

 

This topic has been closed for replies.
Correct answer L e n a

You have two approaches among many others :

so as not to flood this thread with code, you'll find links that mirror the pages (just explore the browser page code).

 

 

Personally, I much prefer the second approach, which makes it easier to refresh the data and, above all, separates the structure from the injection using AJAX.

https://demo.puce-et-media.com/UnderS/quote-3.html 

1 reply

L e n aCommunity ExpertCorrect answer
Community Expert
April 24, 2024

You have two approaches among many others :

so as not to flood this thread with code, you'll find links that mirror the pages (just explore the browser page code).

 

 

Personally, I much prefer the second approach, which makes it easier to refresh the data and, above all, separates the structure from the injection using AJAX.

https://demo.puce-et-media.com/UnderS/quote-3.html 

Under S.Author
Inspiring
April 25, 2024

@L e n a Thank you for this, it's very appreciated. I especially like that you can style the quote and author differently in both examples. Obviously I am leaning towards the first answer, as I don't even know what JSON is.

 

However, I get the impression your first example requires that the end-user download ALL 200 quotes every time, rather than just the one they will be seeing on that specific page refresh. Do I have that wrong? If so, ignore what follows; but if not, is there a way to accomplish the same thing without downloading all the quotes on every page load? For example, by storing them in a server-side file that would act as a makeshift database (that a PHP file could then pull 1 random quote from BEFORE sending it [and only it] to the end-user).

 

PS: Off-topic, but how did this thread get marked as "solved" within 24h of it going up, when the OP (me) didn't have a chance to look at the answers yet? I'm not saying I wouldn't have made the same choice myself, only that this is the first chance I got to look at these replies and I wonder if prematurely marking the thread solved discouraged some from chiming in.

Community Expert
April 25, 2024

I understand your concern, but for my part, if I had to process 200 quotes on a simple TXT file (which I wouldn't do, I'd use a database), I wouldn't ask myself about the weight, as @osgood_ said, a file containing, say, 200 quotes in the previous format (text, author, date) wouldn't wait, or just about 30 kb.

let's push purity to the limit, by limiting this gargantuan loading...

two schools of thought  :

  • the first would be to use a localstorage, so that once the X quotations have been loaded once, JS would dip into the browser's memory to extract a quotation at random,

  • the second, which seems to be what you want to do, would be to upload a TXT file to the server, containing on each line a quotation, its author and the date, all separated by a character, or series of characters, acting as a separator (of course, we could just as easily use XLS or CSV, but that would require libraries to read these formats, so let's stick with pure TXT).
    PHP could then read this TXT file, count the total number of lines, position its cursor at start of one of these lines at random (as far as I know, there is no way to natively retrieve it), and return the result in JSON format to the client page.

 

What would you like to do, so that we can help you in the best possible way? I must have this type of PHP routine somewhere in my development notes, which would only need basic adaptations to your needs.