115 pts.
 Parsing error: Declare and initialize an associative array
Used an HTML form to create a table.  Saved the data using
<html>
    <!--Patricia Oswalt - IT250 Unit 4 Assignment-->
    
    <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($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 = 'contactlist.txt';
        $Contact = fopen($NewContact, 'a+');
        $output = "$LastName, $FirstName, $StreetAdd, $City, $State, $ZipCode, $PhoneNum rn";
        echo "New Contact Saved Successfully!";
        fwrite($Contact, $output);
        fclose($Contact);

        ?>

    <p><a href="InputTelephone.html">Add New Listing</a></p>
    <p><a href="contactlist.txt">Retrieve Contact List</a></p>

    </body>

</html>
The above works fine.  Now I have to be able to sort the data in a table. This is my code so far.
<html>
    <!--Patricia Oswalt - IT250 Unit 5 Assignment-->
    
    <head>
        <title>Contact Table</title>
    </head>
     
    <body>

    <?php
    //opening the file for reading
    $fp = fopen('contactlist.txt', 'r');
    if(!$fp) die ("Cannot open the file");
    
    //Declare and initialize an associative array

    $Listing = array();
    $Listing[$Name] = $LastName, $FirstName;
    $Listing[$Number] = $PhoneNum;

//Is the above syntax correct to create an associative array named $Listing?

  echo "<table border='1'>";
  echo "<tr>";
  echo "<th>Name</th>";
  echo "<th>Phone Number</th>";
  echo "</tr>";
  
      //Next we want to sort $Listing by the $LastName
    ksort($Listing);

    //Then to print the sorted table. (using the example in Unit 5’s slide show)
        foreach($Listing as $Name => $Number)
        {
            echo "<tr>";
            echo "<td>$Name</td>";
            echo "<td>$Number</td><br>";
            echo "</tr>";
        }
        echo "</table>";
    ?>

    <p><a href="InputTelephone.html">Add New Listing</a></p>
    <p><a href="contactlist.txt">Retrieve Contact List</a></p>
    
    </body>

</html>
I think my only problem is creating the associative array.  Can anyone look it over and tell me if they see any problems?  This project is already a week behind and I can't figure out where I'm going wrong. Looking over it right now, I think I made need to put an fclose statement somewhere as well. Thanks in advance!  Patricia

Software/Hardware used:
CodeLobster PHP version 3.2.2; WAMP server; PHP 5.3.0; Windows XP Pro
ASKED: March 17, 2010  1:24 AM
UPDATED: July 13, 2010  5:43 AM

Answer Wiki:
First, I think you have an error here: <pre>$Listing[$Name] = $LastName, $FirstName;</pre> If you want to assign the last and first names to one position of the array, you have to concatenate them. <pre>$Listing[$Name] = $LastName.",". $FirstName;</pre> But, using the array that way you are doing something like this (for example): <pre>$Listing["Doe, John" ] = "Doe, John"; $Listing["5551234567" ] = "5551234567";</pre> Which doesn't make much sense, and furthermore, you would be using one array position for the name, and another position for the number, so you will need to use a <a href="http://www.webcheatsheet.com/PHP/multidimensional_arrays.php">multidimensional array</a> to store all your contacts. If you just want to store the name and phone number, you could do something like this: <pre>$Listing[$LastName.",". $FirstName] = $PhoneNum;</pre> P.s. If you get errors, please include the complete error message and specify the line of code where you get the error. -CarlosDL ------------------
Last Wiki Answer Submitted:  March 17, 2010  2:59 pm  by  carlosdl   63,580 pts.
All Answer Wiki Contributors:  carlosdl   63,580 pts.
To see all answers submitted to the Answer Wiki: View Answer History.


Discuss This Question:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _


 

Sorry. Had to edit the answer many times as some code was not showing properly.

 63,580 pts.

 

Using the first suggestion:

	$Listing = array();
	$Listing[$Name] = $LastName.",". $FirstName;
	$Listing[$Number] = $PhoneNum;

I received the following error:

Notice: Undefined variable: LastName in C:wampwwwOswaltPatricia_Unit5AssignmentDisplayDirectory.php on line 21

Notice: Undefined variable: FirstName in C:wampwwwOswaltPatricia_Unit5AssignmentDisplayDirectory.php on line 21

Notice: Undefined variable: Name in C:wampwwwOswaltPatricia_Unit5AssignmentDisplayDirectory.php on line 21

Notice: Undefined variable: Number in C:wampwwwOswaltPatricia_Unit5AssignmentDisplayDirectory.php on line 22

Notice: Undefined variable: PhoneNum in C:wampwwwOswaltPatricia_Unit5AssignmentDisplayDirectory.php on line 22

Using your second suggestion:

	$Listing = array();
	$Listing[$LastName.",". $FirstName] = $PhoneNum;

I received this error
Notice: Undefined variable: LastName in C:wampwwwOswaltPatricia_Unit5AssignmentDisplayDirectory.php on line 21

Notice: Undefined variable: FirstName in C:wampwwwOswaltPatricia_Unit5AssignmentDisplayDirectory.php on line 21

Notice: Undefined variable: PhoneNum in C:wampwwwOswaltPatricia_Unit5AssignmentDisplayDirectory.php on line 21

Name Phone Number
,

Add New Listing

Retrieve Contact List

Thank you so much for the help. Patricia

 115 pts.

 

In PHP, variables are created when you assign them a value for the first time. As the error messages say, the variables you are using are undefined (they don’t exist in that scope).

 63,580 pts.

 

Okay, I’m going to go back to my PHP book to read up about scopes again. But if I’m trying to pull the information from a text file, wouldn’t the variables be defined once I opened the text file for reading?

	$fp = fopen('contactlist.txt', 'r+');
	if(!$fp) die ("Cannot open the file");

And if not, would I declare them in the same manner as I did in the initial php file that created the text file?

Just in case we need clarification:
There’s an InputTelephone.html form for the user to fill out, then a SaveDirectory.php file to save the data to the contactlist.txt. Lastly, there’s the DisplayDirectory.php that I’m trying to figure out.

I really appreciate the time you’re giving me to help me figure this out. Would it be easier if I zipped the files and sent them to you? Whatever would be easier for you.

Patricia

 115 pts.

 

Hi Patricia,

First of all, I must say that I’m not a PHP expert. My PHP knowledge is very limited, but similar concepts apply to different languages, so…

Regarding this:

…But if I’m trying to pull the information from a text file, wouldn’t the variables be defined once I opened the text file for reading?

No, you used those variables to write to the file, but that information (variable names) is not stored in the file. The file contains just plain text, so you will have read the text, parse it, and assign the appropriate values to your variables.

 63,580 pts.

 

Okay. Thank you. You gave me another way to look at it. I think I can figure this out. Hopefully, I’ll be able to pay this forward some day.
Patricia

 115 pts.

 

I’m sure you will.

Take a look at this:

Working With Text Files in PHP

 63,580 pts.

 

I am doing the same project now and I am stuck on it as well. I hate that, I hope you got yours figured out better than I have. Wished I had thought sooner to ask a tech help person.

 20 pts.

 

I am on same one now as well and yes it is a KILLER!!

 10 pts.

 

Pat,

Did you ever get the code figured out for this? I have been working my tail off and other than the advice from a few student who pay to get a professional coder to do theirs I am no closer to finishing mine. Is there any chance you have your Unit 4 on projects for IT250 or at least the unit 4 & 5 and would either post the code or upload and post a download link? I hate to ask but I just do not know what else to do short of paying $30 bucks to have a PHP coder to do it.

 20 pts.