954,164 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Help debug Random property script.

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.

house 1

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.

Read More

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.

kained
Junior Poster
126 posts since May 2004
Reputation Points: 10
Solved Threads: 0
 

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.

diafol
Rhod Gilbert Fan (ardav)
Moderator
7,755 posts since Oct 2006
Reputation Points: 1,168
Solved Threads: 1,074
 

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!

kained
Junior Poster
126 posts since May 2004
Reputation Points: 10
Solved Threads: 0
 

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!

kained
Junior Poster
126 posts since May 2004
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You