943,568 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 414
  • PHP RSS
Sep 19th, 2009
0

Help debug Random property script.

Expand Post »
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.

Quote ...
<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 Syntax (Toggle Plain Text)
  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.

php Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster
kained is offline Offline
126 posts
since May 2004
Sep 19th, 2009
-1

Re: Help debug Random property script.

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.

PHP Syntax (Toggle Plain Text)
  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.
Sponsor
Featured Poster
Reputation Points: 1046
Solved Threads: 942
Sarcastic Poster
ardav is offline Offline
6,661 posts
since Oct 2006
Sep 19th, 2009
0

Re: Help debug Random property script.

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!
Reputation Points: 10
Solved Threads: 0
Junior Poster
kained is offline Offline
126 posts
since May 2004
Sep 19th, 2009
0

Re: Help debug Random property script.

I have got the webpage bit working now using:

php Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Junior Poster
kained is offline Offline
126 posts
since May 2004

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Search with multiple search terms used
Next Thread in PHP Forum Timeline: How to use large pagination in a limited area





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC