| | |
Help debug Random property script.
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
•
•
Join Date: May 2004
Posts: 102
Reputation:
Solved Threads: 0
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.
I then have this file called quoteGenerate.php which i suspect is where the problem lies.
I am then calling the quote on the webpage
by using the following script.
what have i done wrong? any help very very appreciated.
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>
php Syntax (Toggle Plain Text)
<?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 Syntax (Toggle Plain Text)
<?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.
Last edited by kained; Sep 19th, 2009 at 7:26 am.
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.
Pick the first record.
Poncing about with files is a pain.
PHP Syntax (Toggle Plain Text)
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.
It's full of stars...
•
•
Join Date: May 2004
Posts: 102
Reputation:
Solved Threads: 0
I have got the webpage bit working now using:
im an idiot! quotelist.txt should have been quoteData.txt.
problem solved!
php Syntax (Toggle Plain Text)
<?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!
Last edited by kained; Sep 19th, 2009 at 10:43 am.
![]() |
Similar Threads
- Random Images on Refresh (JavaScript / DHTML / AJAX)
- load-intensive shell script (Shell Scripting)
- Pass a 2D-array with AJAX (JavaScript / DHTML / AJAX)
- [DOM] Assigning width/height values in percent without eval()? (JavaScript / DHTML / AJAX)
- Word Search Puzzle Generator... (C)
- ASP/SQL - Problem (ASP)
- want to upload images to access db from ColdFusion (ColdFusion)
- Random Programming Script (Computer Science)
Other Threads in the PHP Forum
- Previous Thread: Search with multiple search terms used
- Next Thread: How to use large pagination in a limited area
| Thread Tools | Search this Thread |
.htaccess ajax apache api array beginner binary body broken cakephp checkbox class cms code cron curl database date date/time display dynamic echo email error file files folder form forms function functions global google href htaccess html image include insert integration ip java javascript joomla limit link list login loop mail memmory menu mlm mod_rewrite msqli_multi_query multiple mycodeisbad mysql navigation oop parameter paypal pdf php problem query radio random recourse recursion regex registrationform remote script search seo server sessions sms soap source space sql static syntax system table tutorial update upload url validation validator variable video web webdesign wordpress xml youtube






