Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-02-2012, 01:04 AM   PM User | #1
rose34ll2
New Coder

 
Join Date: Mar 2012
Posts: 12
Thanks: 1
Thanked 0 Times in 0 Posts
rose34ll2 is an unknown quantity at this point
I need help with this coding please keep getting errors

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($Contact8192);
}
 
// 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>
rose34ll2 is offline   Reply With Quote
Old 04-02-2012, 03:09 AM   PM User | #2
ShaneC
Codeasaurus Rex


 
Join Date: Jun 2008
Location: Redmond, WA
Posts: 659
Thanks: 31
Thanked 100 Times in 94 Posts
ShaneC is on a distinguished road
Please be sure to post PHP questions in the PHP category in the future. "Post a PHP Snippet" is for already created code for others to use. No need to worry this time around, though, as I've moved your post.
__________________
Unless otherwise stated, any code posted is most likely untested and may contain syntax errors.
My posts, comments, code, and suggestions reflect only my personal views.
Web Portfolio and Code Snippets: http://shanechism.com
ShaneC is offline   Reply With Quote
Users who have thanked ShaneC for this post:
rose34ll2 (04-02-2012)
Old 04-02-2012, 10:38 AM   PM User | #3
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,856
Thanks: 9
Thanked 288 Times in 284 Posts
Dormilich is on a distinguished road
when if() and else are used without curly braces, only the next statement/line is used. in your case, line #37 does not belong to the if/else condition and because only in the else statement you define that variable, it is not initialized. hence the errors.
__________________
please post your code wrapped in [CODE] [/CODE] tags
Dormilich is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:25 PM.


Advertisement
Log in to turn off these ads.