Member Avatar for kained

Right i'm in the process of designing a web page for a property company and the owner has requested a random property be displayed each day.

I have modified a quote of the day script to work with a filemtime method but its not working and im stumped, fairly sure i'm being an idiot though!
All php & txt files are located in the root directory.

I have 10 quotes(for now) in a file called Quotedata.txt All these quote occupy one line.

I then have a file called TodaysQuote.txt which at the moment contains the following on ONE line.

<img src="images/house_1.jpg" width="85" height="64" alt="house 1" class="left" /><p>This is a demo text. It will be replaced by the original. This is a demo text. It will be replaced by the original. This is a demo text.</p><p class="readmore"><a href="">Read More</a></p>

I then have this file called quoteGenerate.php which i suspect is where the problem lies.

<?php if (time() - filemtime('todaysQuote.txt') > 86400) { $file = "todaysQuote.txt"; // read the file into an array $quoteArray = file("quoteData.txt"); //get the random number $qrnd = rand(0,sizeof($quoteArray)-1); // pick quote name from random numbers $quote = "$quoteArray[$qrnd]"; //write the string to the file // Note: mode 'w' opens for writing only; place the file pointer at // the beginning of the file and truncate the file to zero length. // If the file does not exist, attempt to create it. $fh = fopen($file, "w"); fputs($fh, $quote); fclose($fh); } } ?>

I am then calling the quote on the webpage

by using the following script.

<?php // display quote of the day $file = "todaysQuote.txt"; $fh = fopen($file, "r"); $string = fread($fh, filesize($file)); fclose($fh); echo "$string"; ?>

what have i done wrong? any help very very appreciated.

Member Avatar for diafol

You'd be far better off using a database to store your info and then using a random number generator (RAND() in mysql) to get a record to display.

SELECT * FROM properties ORDER BY RAND();

Pick the first record.

Poncing about with files is a pain.

Member Avatar for kained

don't really want to do a database seems unnecessary as there is no other need for a database and the person who will be maintaining the site has any database experience.

It would be easier for me to code though obviously!

Member Avatar for kained

I have got the webpage bit working now using:

<?php define(TODAYFILE, 'todaysQuote.txt'); define(QUOTEFILE, 'quotelist.txt'); if ((time() - filemtime(TODAYFILE)) > 5) { $quotes = file(QUOTEFILE); $new_quote = $quotes[rand(0, sizeof($quotes) - 1)]; $fh = fopen(TODAYFILE, 'w'); fwrite($fh, $new_quote); fclose($fh); } echo file_get_contents(TODAYFILE); ?>

im an idiot! quotelist.txt should have been quoteData.txt.

problem solved!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.