Page 1 of 1

reading and writing to php file

Posted: Tue Dec 13, 2011 2:48 pm
by fuzznut
i have this script where the user enters their name and their favorite quotes. It adds their favorite quotes and their names to quotes.txt and prints out their name and their quotes on view_quote.php. I need to use a foreach() loop to print out the name and the quotes, but I'm confused about how to set it up. Here's my files.
add_quote.php

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
	<meta http-equiv="content-type" content="text/html; charset=utf-8" />
	<title>Add A Quotation</title>
</head>
<body>
<?php // add_quote.php
/* This script displays and handles an HTML form. This script takes text input and stores it in a text file. */

// Identify the file to use:
$file = 'quotes.txt';

// Check for a form submission:
if ($_SERVER['REQUEST_METHOD'] == 'POST') { // Handle the form.

	if ( !empty($_POST['quote']) && ($_POST['quote'] != 'Enter your quotation here.'
		 ) ){ // Need some thing to write.
	
		if (is_writable($file)) { // Confirm that the file is writable.
			
			file_put_contents($file, $_POST['quote'] . PHP_EOL, FILE_APPEND | LOCK_EX); // Write the data.
			file_put_contents($file, $_POST['name'] . PHP_EOL, FILE_APPEND | LOCK_EX);
			// Print a message:
			print '<p>Your quotation has been stored.</p>';
		
		} else { // Could not open the file.
			print '<p style="color: red;">Your quotation could not be stored due to a system error.</p>';
		}
		
	} else { // Failed to enter a quotation.
		print '<p style="color: red;">Please enter a quotation!</p>';
	}
	
} // End of submitted IF.

// Leave PHP and display the form:
?>

<form action="add_quote.php" method="post">
	<p>Name:<input type="text" name="name"/><br />
	<textarea name="quote" rows="5" cols="30">Enter your quotation here.</textarea><br />
	<input type="submit" name="submit" value="Add This Quote!" />
</form>

</body>
</html>
view_quote.php

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
	<meta http-equiv="content-type" content="text/html; charset=utf-8" />
	<title>View A Quotation</title>
</head>
<body>
<h1>View My Quotes</h1>
<?php // view_quote.php
/* This script displays and handles an HTML form. This script reads in a file and prints a random line from it. */

// Read the file's contents into an array:
$data = file('quotes.txt');

// Count the number of items in the array:
$n = count($data);

// Pick a random item:
$rand = rand(0, ($n - 1));

// Print the quotation:
print '<p>' . trim($data[$rand]) . '</p>';


?>
</body>
</html>
and the quotes.txt file is blank until the user enters their names and quotes. Im trying to get this done kind of quick so any help would be awesome.

Re: reading and writing to php file

Posted: Wed Dec 14, 2011 12:42 am
by social_experiment
Once you have the array you can use foreach() after this part

Code: Select all

<?php
// do some checking on the files, see if it exists, also check if $data is 
// an array
$data = file('quotes.txt');
// use foreach here
foreach ($data as $line) {
    echo $data . '<br />';
}
?>
You could also modify the foreach() statement to display the line number of the quote if you really wanted to

Code: Select all

<?php foreach ($data as $line_num => $line) ?>