Page 1 of 1

Rotating Content

Posted: Tue Nov 23, 2004 6:21 am
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???

Posted: Tue Nov 23, 2004 6:37 am
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.

Posted: Tue Nov 23, 2004 7:18 am
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");

Posted: Tue Nov 23, 2004 9:56 am
by hairyjim
Phenom wrote:

Code: Select all

SELECT `website`,`websitedescription` WHERE `id` = rand(`ID`) LIMIT 1
Oh...I learn something new everyday!

Posted: Tue Nov 23, 2004 12:48 pm
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)