Help debug Random property script.

Thread Solved

Join Date: May 2004
Posts: 102
Reputation: kained is an unknown quantity at this point 
Solved Threads: 0
kained kained is offline Offline
Junior Poster

Help debug Random property script.

 
0
  #1
Sep 19th, 2009
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.

  1. <?php
  2. if (time() - filemtime('todaysQuote.txt') > 86400)
  3.  
  4. {
  5. $file = "todaysQuote.txt";
  6. // read the file into an array
  7. $quoteArray = file("quoteData.txt");
  8.  
  9. //get the random number
  10. $qrnd = rand(0,sizeof($quoteArray)-1);
  11.  
  12. // pick quote name from random numbers
  13. $quote = "$quoteArray[$qrnd]";
  14.  
  15.  
  16. //write the string to the file
  17.  
  18. // Note: mode 'w' opens for writing only; place the file pointer at
  19. // the beginning of the file and truncate the file to zero length.
  20. // If the file does not exist, attempt to create it.
  21.  
  22. $fh = fopen($file, "w");
  23. fputs($fh, $quote);
  24. fclose($fh);
  25. }
  26. }
  27. ?>

I am then calling the quote on the webpage

by using the following script.

  1. <?php
  2. // display quote of the day
  3. $file = "todaysQuote.txt";
  4.  
  5. $fh = fopen($file, "r");
  6. $string = fread($fh, filesize($file));
  7. fclose($fh);
  8.  
  9. echo "$string";
  10. ?>

what have i done wrong? any help very very appreciated.
Last edited by kained; Sep 19th, 2009 at 7:26 am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 982
Reputation: ardav will become famous soon enough ardav will become famous soon enough 
Solved Threads: 128
ardav's Avatar
ardav ardav is online now Online
Posting Shark

Re: Help debug Random property script.

 
-1
  #2
Sep 19th, 2009
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.

  1. SELECT * FROM properties ORDER BY RAND();

Pick the first record.

Poncing about with files is a pain.
Last edited by ardav; Sep 19th, 2009 at 8:07 am.
"...the woods would be a very silent place if no birds sang except for the best"
All opinions count - unless you're a serial downvoter.
F'enw i yw Mr. Blaidd. Byddwch yn ofalus - dwi'n cnoi.
Reply With Quote Quick reply to this message  
Join Date: May 2004
Posts: 102
Reputation: kained is an unknown quantity at this point 
Solved Threads: 0
kained kained is offline Offline
Junior Poster

Re: Help debug Random property script.

 
0
  #3
Sep 19th, 2009
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!
Reply With Quote Quick reply to this message  
Join Date: May 2004
Posts: 102
Reputation: kained is an unknown quantity at this point 
Solved Threads: 0
kained kained is offline Offline
Junior Poster

Re: Help debug Random property script.

 
0
  #4
Sep 19th, 2009
I have got the webpage bit working now using:

  1. <?php
  2.  
  3. define(TODAYFILE, 'todaysQuote.txt');
  4. define(QUOTEFILE, 'quotelist.txt');
  5.  
  6.  
  7. if ((time() - filemtime(TODAYFILE)) > 5) {
  8. $quotes = file(QUOTEFILE);
  9. $new_quote = $quotes[rand(0, sizeof($quotes) - 1)];
  10. $fh = fopen(TODAYFILE, 'w');
  11. fwrite($fh, $new_quote);
  12. fclose($fh);
  13. }
  14.  
  15. echo file_get_contents(TODAYFILE);
  16.  
  17. ?>

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

problem solved!
Last edited by kained; Sep 19th, 2009 at 10:43 am.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC