Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Mar 2012
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    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>

  • #2
    Codeasaurus Rex
    Join Date
    Jun 2008
    Location
    Redmond, WA
    Posts
    659
    Thanks
    31
    Thanked 100 Times in 94 Posts
    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

  • Users who have thanked ShaneC for this post:

    rose34ll2 (04-02-2012)

  • #3
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,133
    Thanks
    12
    Thanked 332 Times in 328 Posts
    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.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •