Jump to content

Help with Arrays


Joe Sai

Recommended Posts

Ok guys, back yet again, sorry for bothering you.

 

Anyway, I want to thank you all soooo much for helping me last week with my telephone directory project. Everything worked out perfectly once you pointed out my errors.

 

Now I'm working with arrays. These are the directions for what I have to do from here:

 

In this assignment you will create an alphabetized list of your telephone directory. Put a link or button on the telephone directory entry page to execute this new program. 

 

Create a PHP program to open the file to read the data.  You will want to create an array that contains the name and the phone number. The name should be constructed so that the last name is first, followed by a comma, and then the first name. Example:  Smith, John

 

The phone number should contain the area code concatenated to the phone number. Example: 212-398-0987.

 

The array will then be sorted in (last) name order using an appropriate PHP function. 

 

The PHP program will present the data in an HTML table with two columns. Column headings will label the columns “name” and “phone number.” The data will be presented in alphabetical order.

 

Ok, so some of this I've already got down with no problems, it's just some others I need help with.

 

This is my Telephone Directory PHP code:

 

<body>

<?php
if (empty($_GET['first_name']) || empty($_GET['last_name']) || empty($_GET['street_address']) || empty($_GET['city']) || empty($_GET['state']) || empty($_GET['zip']) || empty($_GET['area_code']) || empty($_GET['phone_number']))
echo "<p>You must enter all information! Click your browser's Back button to return to the Telephone Directory form.</p>";
else {
$FirstName = addslashes($_GET['first_name']);
$LastName = addslashes($_GET['last_name']);
    $StreetAddress = addslashes($_GET['street_address']);
    $City = addslashes($_GET['city']);
    $State = addslashes($_GET['state']);
    $Zip = addslashes($_GET['zip']);
    $AreaCode = addslashes($_GET['area_code']);
$PhoneNumber = addslashes($_GET['phone_number']);
$TelephoneDirectory = fopen("telephonedirectory.txt", "a");
if (is_writable("telephonedirectory.txt")) {
	if (fwrite($TelephoneDirectory, $LastName . "," . $FirstName . "," . $StreetAddress . "," . $City . "," . $State . "," . $Zip . "," . $AreaCode . "-" . $PhoneNumber .  "\n"))
		echo "<p>Thank you for signing our Telephone Directory!</p>";
	else
		echo "<p>Cannot add your entry to the directory.</p>";
}
else
	echo "<p>Cannot write to the file.</p>";
fclose($TelephoneDirectory);
}
?>

</body>
</html>

 

And this is the html code for my telephone directory:

 

<body>
<h2>Enter your information into our telephone directory!</h2>
<form method="get" action="../TelephoneDirectory.php">
<p>Last Name <input type="text" name="last_name" /></p>
<p>First Name <input type="text" name="first_name" /></p>
<p>Street Address <input type="text" name="street_address" /></p>
<p>City <input type="text" name="city" /></p>
<p>State <input type="text" name="state" /></p>
<p>Zip <input type="text" name="zip" /></p>
<p>Area Code <input type="text" name="area_code" /></p>
<p>Phone Number <input type="text" name="phone_number" /></p>
<p><input type="submit" value=" Submit" /></p>
</form>
<p><a href="../ShowData.php">Show Data</a></p>
<p><a href="../ShowDirectory.php">Show Directory</a></p>
</body>
</html>

 

Notice how I have "Show Data" and "Show Directory" on the bottom, which really isn't necessary to have both, but I'm doing it incase my professor wanted one without the array and one with.

 

Anyway, I'm working on the PHP file for displaying the information in the directory, and I have this:

 

<body>
<?php
if (is_writable("telephonedirectory.txt")) {
echo "<p>The following visitors have entered our Telephone Directory:</p><pre>";
readfile("telephonedirectory.txt");
echo "</pre>";
}
else
echo "<p>Cannot write to the file.</p>";
?>

</body>
</html>

 

So at this point, it displays everything, and I only need it to display the last name, first name, and phone number. Moreover, I also need to make an html file as well to display the information in, but I'm not sure how to do that. I did one form for the genera directory where you input the information, but how would I go about making that form just to display the information in the two columns? I know this is probably something really easy and I'm being an idiot, but please bear with me, I'm trying really hard over here. Thanks for your help!

Link to comment
Share on other sites

instead of read file, why dont you read the file line by line using fgets and a while loop. remember to open the file for reading only.  you can now use explode to separate the line by a given delimiter(such as a comma) and you can assign variables for first name last name and number.  deal with them how you will after that. you can put them into a 3 dimensional array

Link to comment
Share on other sites

Ok, I've worked on the table a little to get it to display, and now I'm getting a little bit of problems. Here is my code:

 

<?php
require_once 'telephonedirectory.txt';

$attrs = array('width' => '600');
$table = new HTML_Table($attrs);
$table->setAutoGrow(true);
$table->setAutoFill('n/a');

for ($nr = 0; $nr < count($data); $nr++) {
  $table->setHeaderContents($nr+1, 0, (string)$nr);
  for ($i = 0; $i < 4; $i++) {
    if ('' != $data[$nr][$i]) {
      $table->setCellContents($nr+1, $i+1, $data[$nr][$i]);
    }
  }
}
$altRow = array('bgcolor' => 'red');
$table->altRowAttributes(1, null, $altRow);

$table->setHeaderContents(0, 0, '');
$table->setHeaderContents(0, 1, 'Name');
$table->setHeaderContents(0, 2, 'Phone Number');
$hrAttrs = array('bgcolor' => 'silver');
$table->setRowAttributes(0, $hrAttrs, true);
$table->setColAttributes(0, $hrAttrs);
?> 

 

I found this from a php help site regarding tables. When I put this in I got an error on line 13 saying:

 

Fatal error: Class 'HTML_Table' not found in C:\wamp\www\ShowDirectory.php on line 13

 

I tried changing it to 'telephonedirectory.txt' and then got this message:

 

Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `'$'' in C:\wamp\www\ShowDirectory.php on line 13

 

This is really throwing me for a loop.

 

Oh, and abaz, I'm not exactly sure how to do all that, I'm really new at this.

Link to comment
Share on other sites

You should read the file line by line and then use the function "explode" to fetch those columns you need.

 

Thank you for your response, but I'm not sure how to do that. Like I said, I'm new to PHP, so I need some more specifics.

 

So in the errors I posted before, do I need to type in "explode" instead of HTML_Table that it was not reading?

 

I'm sorry, but I don't understand what you're saying, I need things babied down a bit I guess. I'm trying my best here, so please try to be patient with me. I really appreciate your help.

Link to comment
Share on other sites

instead of read file, why dont you read the file line by line using fgets and a while loop. remember to open the file for reading only.  you can now use explode to separate the line by a given delimiter(such as a comma) and you can assign variables for first name last name and number.  deal with them how you will after that. you can put them into a 3 dimensional array

 

ill expand on this.

//create an empty array for later
$array = array();
//use fopen to open the file
$fh = fopen($file, 'r');
//have a while loop
while($line = fgets($fh)) {
    $pieces = explode(',',$line);
    //pieces is now an array i.e. $pieces[0], $pieces[1], etc
    $array[$i] = array($pieces[0],$pieces[1],$pieces[2]);
}

 

not tested, and bare bones, but that should get you started.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.