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
  • 2204 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 

Abambo
Community Expert
Community Expert
April 24, 2024

"Database" means probably a SQL database system, and not a flat file. Many people think that a database needs to be a complex structure with a lot of overhead. But with 10 quotes or something there around, a flat file would do also the affair. 

 

My guess would be: 

  • create an XML file with the quote.
  • write code to read that XML file - that should be easy to find on the web. 
  • display a random quote at a predifined place on your page.
ABAMBO | Hard- and Software Engineer | Photographer
Community Expert
April 24, 2024

You're right but JSON is almost a database some how, look MongoDB, FRD, Couchbase and so on... 

 

Using XML is an other way to go, but except if @Under S. use an XSLT (or other processing integration), XML should be parsed too... and so JSON is for what I think, more accurate for such a purpose, at least that's what seams more customary and contemporary. 

https://demo.puce-et-media.com/UnderS/quote-3.html  produce a complete and functionnal sample using an external JSON file.