Hi I am trying to make this coding work where it reads and write to a txt file and then puts it into a table with two colums and I noticed it's not putting the names on separte lines. heres the errors i'm getting
Notice: Undefined variable: NewContact in C:\wamp\www\unit4.php on line 36
Warning: fopen() [function.fopen]: Filename cannot be empty in C:\wamp\www\unit4.php on line 36
Warning: fwrite() expects parameter 1 to be resource, boolean given in C:\wamp\www\unit4.php on line 39
it's also not putting the names and phone numbers in a table or the last name in alph order. and not showing the phone numbers its showing the rn.
PHP Code:
<head>
<title>Save Directory</title>
</head>
<body>
<?php
//initializing the variables
$LastName = @$_POST['LastName'];
$FirstName = @$_POST['FirstName'];
$StreetAdd = @$_POST['StreetAdd'];
$City = @$_POST['City'];
$State = @$_POST['State'];
$ZipCode = @$_POST['ZipCode'];
$PhoneNum = ("(" . @$_POST['AreaCode'] . ")" . @$_POST['Prefix'] . "-" . @$_POST['LineNum']);
//confirming that all variables will contain values
if (empty($LastName) ||
empty($FirstName) ||
empty($StreetAdd) ||
empty($City) ||
empty($State) ||
empty($ZipCode) ||
empty($AreaCode) ||
empty($PhoneNum))
echo "<p>You must completely fill out the form. Please use your browser's Back button to return to the form.</p>";
else
//creating, writing and saving to the text file.
$NewContact = 'file.txt';
$Contact = fopen($NewContact, 'a+');
$output = "$LastName, $FirstName, $StreetAdd, $City, $State, $ZipCode, $PhoneNum\r\n";
echo "New Contact Saved Successfully!";
fwrite($Contact, $output);
if ( @$_GET['source'] ){
show_source(__FILE__);
die();
}
// Open text file
$Contact = fopen("file.txt", "rb");
// read text file into $contents
$contents = "";
while (!feof($Contact)) {
$contents .= fread($Contact, 8192);
}
// close the file
fclose($Contact);
echo nl2br($contents);
echo "<hr>";
// Sepeate $contents
$contactLines = explode("\n", $contents);
echo "<pre>"; print_r($contactLines); echo "</pre>";
echo "<hr>";
// Sort the array
asort($contactLines);
echo "<pre>"; print_r($contactLines); echo "</pre>";
echo "<hr>";
?>
<table>
<tr>
<th>Name</th>
<th>Phone</th>
</tr>
<?
// output the data
foreach($contactLines as $line){
$info = explode(",", $line);
$lastname = $info[0];
$firstname = $info[1];
$phone = $info[2];
echo '<tr>';
echo '<td><input type="text" value="'.$LastName.', '.$FirstName.'"></td>';
echo '<td><input type="text" value="'.$PhoneNum.'"></td>';
echo '</tr>';
}
?>
</table>
<?
echo "<hr>";
echo nl2br("
fclose($Contact);
?>
<p><a href="unit4.html">Add New Listing</a></p>
<p><a href="file.txt">Retrieve Contact List</a></p>
</body>
</html>