Page 1 of 1

php help

Posted: Tue Mar 11, 2014 10:28 pm
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";

Re: php help

Posted: Wed Mar 12, 2014 8:18 am
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.

Re: php help

Posted: Thu Mar 13, 2014 11:58 am
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..