I haven't touched my site since the class meeting on Tuesday, but I've done some research into my idea about fetching a random poem whenever a page is refreshed. I knew that I'd have to write or adapt some kind of script for it, and I needed to find out whether I could do it without teaching myself PHP from scratch at the same time that I'm learning HTML, Dreamweaver, and the contents of two other LIS courses. Happily, it looks like I can. I started my search by using View Source on the page of Neil Gaiman's that inspired this idea in the first place, but I wasn't able to identify what part of the code he uses to randomize the page's content. So I turned to Google.
A little cursory Googling around revealed a number of useful script-support sites, and fortunately, there are a number of people out there who for various reasons want to do the kind of thing I'm doing. It seems that the best-practices solution is that I should create an array -- not a full-fledged database, to my relief -- and use PHP for my scripting. I found good code on StackOverflow.com, and by looking it over I understand the principles I'll be using. I'll have an array called $poems, or something similar, and I'll populate it from a text file using a command like "$poems = file('poems.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES)". Then I'll use the command "echo $poems[mt_rand(0,count($poems)-1)]" to have the script select a random number and pick the array entry corresponding to that number. I understand every part of that from my days coding in BASIC except the command "echo," which I assume outputs the picked entry to the webpage.
This is still going to require a lot of experimenting. I don't even know whether I need to explicitly install PHP support on my site somehow, or whether browsers and/or servers all already know what [?php] means. But if I can pull this off, I'll feel really happy about that piece of my site.
No comments:
Post a Comment