PDA

View Full Version : saving to a list?



darco9x2
02-24-2006, 04:33 AM
i want to set up a page, that will allow me to add somthing to a list and that list will be displayed on another page. i can probably figure out how to display the list, but how can i get it to save? i plan on using a form to save it, so can anybody help me out?

Pixelcode
02-24-2006, 01:59 PM
You could add to the list in a text file and have every new entry on a new line e.g.
LIST ITEM 1
LIST ITEM 2

Then read the text file line by line to display the list.

<?
if($_POST['Submit']){
$open = fopen("textlist.txt","w+");
$text = $_POST['list']."\r\n";
fwrite($open, $text);
fclose($open);
echo "File updated.";
}else{
echo "<form action=\"".$PHP_SELF."\" method=\"post\">";
echo "<textarea Name=\"list\"></textarea>";
echo "<input name=\"Submit\" type=\"submit\" value=\"Update\" />\n
</form>";
}
?>
View file line by line:

$file = file("textlist.txt");
echo "<ul>";
foreach($file as $text) {
echo "<li>".$text."</li>";
}
echo "</ul>";
:)

darco9x2
02-24-2006, 02:35 PM
thankyou so much.
:)

edit: i tried it out, and it overwrites it now instead of adding to it. is it supposed to do that?

Twey
02-24-2006, 03:38 PM
If you want it to append, replace
$open = fopen("textlist.txt","w+"); with
$open = fopen("textlist.txt","a+");Note that you'll have to seek to the beginning of the file before reading from it:
fseek($open, 0);and if you then want to write to it again, you'll need to seek back to the end:
fseek($open, 0, SEEK_END);

darco9x2
02-24-2006, 09:39 PM
ok thanks that clears it up.