php help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
emetrack123
Forum Newbie
Posts: 4
Joined: Thu Mar 06, 2014 7:10 am

php help

Post by emetrack123 »

Here is what i am trying to do
- i am suppose to open the file put it into an array
- delete something in the array
- rewrite the array back into the file

and spit it back out

i dont know what i am doing wrong
please help me

Code: Select all

<?php
$visitor = fopen("guestbook.txt", "ab");
$countvisit = count($visitor);
	for ($i = 0; $i < $countvisit; ++$i){
		$visitor = explode(",", $visitor[$i]);
fclose($visitor);
	}
$del = ($_POST['visitor']-1);
$redit = array_splice($visitors, $del, $del, NULL);
	echo $redit;

$visitor1 = fopen("guestbook.txt", "w");
	fwrite($visitor1, $redit);
	fclose($visitor);
	
	$viewgb = file("guestbook.txt");
	echo "<table style=\"background-color:gray\" border=\"1\" width=\"100%\">\n";


	$countvisit = count($viewgb);
	for ($i = 0; $i < $countvisit; ++$i){
		$visitor = explode(",", $viewgb[$i]);
	 echo "<tr>\n";

	echo "<td width=\"5%\" style=\"text-align:center; font-weight:bold\">",($i + 1),"</td>\n";
	echo "<td width=\"95%\"><span style=\"font-weight:bold\">Name:</span>",htmlentities($visitor[0]), "<br />\n";
	echo "<span style=\"font-weight:bold\">Email:</span>". htmlentities($visitor[1])."</td>\n";
	echo " </tr>\n ";
			
}

echo "</table>\n";
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: php help

Post by Christopher »

You're not actually reading in the file. The variable $visitor is the file handle, not the contents of the file. I'd recommend using file() to read the file into an array, then loop through the array, then implode() with newlines, then file_put_contents() that buffer back to the file.
(#10850)
emetrack123
Forum Newbie
Posts: 4
Joined: Thu Mar 06, 2014 7:10 am

Re: php help

Post by emetrack123 »

Code: Select all

<?php
	$viewgb = file("guestbook.txt");
	$countvisit = count($viewgb);
	for ($i = 0; $i < $countvisit; ++$i) {
		$visitor = explode("\n", $viewgb[$i]);
		$visitor[0];
	}

	$required = array('visit');
	$error = false;
	foreach ($required as $field) {
		if (empty($_POST[$field])) {
 			$error = true;
		}
	}

	if ($error) {
		echo "All fields are required.";
		echo "<p><a href ='viewguestbook.php'>Click here to go back</a></p>";
	} else {
		$undo = ($_POST['visit])
		$redit = array_splice($visitor, $undo, $undo, NULL);
		echo $redit; 
	}
This is what i have so far.. could you help me out..
Post Reply