#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    112
    Rep Power
    7

    Multi dimensional array input.


    I am trying to adapt the following code. I am having trouble on line 89. I know that the in_array can only take 1 parameter so how do you modify the code so it takes the name and email and puts it on one line in the text file?

    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

    <head>
    <title>Guest Book</title>
    <meta http-equiv="Content-Type"
        content="text/html; charset=iso-8859-1"/>
    </head>



    <body>
    <h1>Guest Book Array Demonstration</h1>
    <?php
    //action is for the hyperlink -- like ?page
    if(isset($_GET['action'])) {
        
    //if the file guests.txt exists and is not blank
        
    if((file_exists("GuestOrganizer/guests.txt")) && (filesize("GuestOrganizer/guests.txt")!=0)) {

            
    //Gets the guests and puts them in an indexed array
            
    $GuestArray file("GuestOrganizer/guests.txt");

            
    //use troubleshooting technique to see if the array contains values
            //echo "<pre>";
                //print_r($GuestArray);
            //echo "</pre>";


            //the switch statment checks to see which of the actions has been clicked and performs those steps
            //NOTHING HAPPENS HERE UNLESS A ACTION HYPERLINK HAS BEEN CLICKED
            
    switch ($_GET['action']) {

            case 
    'Remove_Duplicates'//removes duplicates from the GuestOrganizer
                
    $GuestArray array_unique($GuestArray); //The array_unique() is used to remove duplicate values from an array
                
    $GuestArray array_values($GuestArray);//assigns all the values of the array to $GuestArray -- but no indexese
                
    break;

            case 
    'Sort_Ascending'://sorts GuestArray ascending
                
    sort($GuestArray);//sorts the array by values
                
    break;

            case 
    'Shuffle'://shuffles the guest names
                
    shuffle($GuestArray);//The shuffle() function randomizes the order of the elements
                
    break;
            }
    //end of switch statement


            //the contents of the guests.txt file are in an ndexed array
            
    if (count($GuestArray)>0) {

                
    //
                
    $NewGuests implode($GuestArray);//turns the indexed array into a string so it can be saved to the text file
                //Opens a handle to the guests.txt file in write binary mode
                
    $GuestStore fopen("GuestOrganizer/guests.txt""wb");

                if(
    $GuestStore ===false){
                    echo 
    "There was an error updating the guest file\n";
                }
    //end of inner if

                
    else{
                    
    fwrite($GuestStore$NewGuests);
                    
    fclose($GuestStore);
                }
    //end of inner else

            
    }//end of if (count($GuestArray)>0)
        
    else{
                
    unlink("GuestOrganizer/guests.txt");
            }
    //end of else
        
    }//end of if((file_exists))
    }//end of if(isset($_GET['action']))


    if(isset($_POST['submit'])) {//handling any data submitted from web form
        
    $GuestToAdd addslashes($_POST['GuestName']);
        
    $EmailToAdd addslashes($_POST['GuestEmail']);
        
        
    $ExistingGuests = array();

        if(
    file_exists("GuestOrganizer/guests.txt") && filesize("GuestOrganizer/guests.txt")>0) {
            
    $ExistingGuests file("GuestOrganizer/guests.txt");
        }
    //end of if file exists

        
    if (in_array($GuestToAdd$ExistingGuests)) {//checks to see if the guest name is already in list
            
    echo "<p>The guest you entered already exists! <br />\n";
            echo 
    "Your Guest was not added to the list.</p>";
        }
    //end of if in_array

        
    else{//adds new guest to guest list
            
            
    $GuestFile fopen("GuestOrganizer/guests.txt""ab");
            
         
            if (
    $GuestFile ===false) {
                
                echo 
    "There was an error saving your message!\n";
            }
    //end of if ($GuestFile ===false)

            
    else{
                
                
    fwrite($GuestFile$GuestToAdd);
                
                
    fclose($GuestFile);
                echo 
    "Your guest has been added to the list\n";
            }
    //end of else fwrite($GuestFile, $GuestToAdd);
        
    }//end of else $GuestFile = fopen("GuestOrganizer/guests.txt", "ab");
    }//end of if(isset($_POST['submit']

        
    if ((!file_exists("GuestOrganizer/guests.txt")) || (filesize("GuestOrganizer/guests.txt")== 0)) {
                echo 
    "<p>There are no guests in the list.</p>\n";
            }
    //end of if ! file_exists
            
    else{
                
    $GuestArray file("GuestOrganizer/guests.txt");
                echo 
    "<table border=\"1\"width=\"100%\" style=\"background-color: lightgray\">\n";

                foreach(
    $GuestArray as $Guest){
                    echo 
    "<tr>\n";
                    echo 
    "<td>" htmlentities($Guest) . "</td>";
                    echo 
    "</tr>\n";
                }
    //end of foreach loop

                
    echo "</table>\n";
    }
    //end of else


    ?>
    <!-- Displaying hyperlinks for the three functions in the switch statment -->
    <p>
    <a href = "GuestOrganizer.php?action=Sort_Ascending">Sort Guest Book</a><br />

    <a href = "GuestOrganizer.php?action=Remove_Duplicates" >Remove Duplicate Entries</a><br />

    <a href = "GuestOrganizer.php?action=Shuffle">Randomize Guest List</a><br />
    </p>

    <!--Web Form for entering song names into the song list  -->
    <form action= "GuestBookArray.php" method = "post">
    <p>Add New Guest Info to List:</p>

    <p>Guest Name: <input type = "text" name = "GuestName"/></p>
    <p>Guest E-mail: <input type = "text" name = "GuestEmail"/></

    <p>
    <input type = "submit" name = "submit" value = "Add Guest to List"/>
    <input type = "reset" name = "reset" value = "Reset Guest Name">
    </p>

    </form>
    </body>
    </html>
  2. #2
  3. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6352
    Stop using this book/tutorial/whatever, it's years out of date.

    The fwrite() line is what writes the line to the file. Instead of this:

    PHP Code:
                fwrite($GuestFile$GuestToAdd); 
    You want this:


    PHP Code:
                fwrite($GuestFile$GuestToAdd ' -- ' $emailToAdd); 
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  4. #3
  5. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,295
    Rep Power
    9400
    If you're putting both the name and email on one line in a file (which you aren't doing so far) then for the in_array() to work you have to combine the name and email into one string - just like you would have added.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    112
    Rep Power
    7
    Originally Posted by ManiacDan
    Stop using this book/tutorial/whatever, it's years out of date.

    The fwrite() line is what writes the line to the file. Instead of this:

    PHP Code:
                fwrite($GuestFile$GuestToAdd); 
    You want this:


    PHP Code:
                fwrite($GuestFile$GuestToAdd ' -- ' $emailToAdd); 
    I realize its a crappy book but I am forced to use it. It is riddled with errors and just a piece of crap. This is NOT a book for a novice PHP programmer. Thanks you guys for helping me out.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    112
    Rep Power
    7
    thanks, that works. however I want every name and email entered on a new line.
  10. #6
  11. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6352
    Fwrite writes a string to a file. If you'd like your string to have a newline in it, put one in:


    fwrite($GuestFile, $GuestToAdd . ' -- ' . $emailToAdd . "\n");
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.

IMN logo majestic logo threadwatch logo seochat tools logo