Rotating Content

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sabian
Forum Newbie
Posts: 6
Joined: Thu Oct 07, 2004 3:07 am

Rotating Content

Post by sabian »

I have started learning php and would like to create some dynamic rotating text on the main page. An example of this is shown at http://www.webmaster-toolkit.com where a different site is shown each time in "Site of the Moment".

I basically want to know how to do a really simple version of this as i take it this version is done using some form of datebase.

Quite simply i want to have a directory containing a number of different files (for example text files) each text file contains the site description and the link to the site. Each time the index page is viewed and/or refreashed a different/random text file from the directory in question is selected.

The content of which is then shown in a specified area on the index page.

Any help for a newbie???
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Code: Select all

SELECT `website`,`websitedescription` WHERE `id` = rand(`ID`) LIMIT 1
I don't recommend using flatfiles to store your data. Databases are so much cleaner. This will pull a random website from your database.
peni
Forum Commoner
Posts: 34
Joined: Thu Nov 18, 2004 1:15 pm

Post by peni »

i support phenom's advice about using databases. anyway if it's inevitable, you can store (or dynamically fill) the list of the file names in an array and generate a random index in order to access a random name and include it

or

having named the files systematically (file0001.txt, file0002.txt, etc), generate a random index and compose the included file name with it ("file".$randomIndex.".txt");
hairyjim
Forum Contributor
Posts: 219
Joined: Wed Nov 13, 2002 9:04 am
Location: Warwickshire, UK

Post by hairyjim »

Phenom wrote:

Code: Select all

SELECT `website`,`websitedescription` WHERE `id` = rand(`ID`) LIMIT 1
Oh...I learn something new everyday!
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

If you want to use text files use a loop to go if (file_exists($file))

that will get a list of files in that directory, then use the rand() function to get a random #


for instance:
file1
file2
file3


then in php go

while(file_exists(file))

or something like that then

$file = "file" . rand(1,$x);

Where $x is the # for the last file there is


(A database would be alot better)
Post Reply