Jump to content

Homework Help, please


Joe Sai

Recommended Posts

Ok, so I asked for help last week and am grateful for what I did get, but I still couldn't figure it out, and this assignment is a few days late. I'm trying to get it done today so I can work on my new assignment tomorrow, and I'd really, really appreciate the help.

 

I think the problem I had was that I tried to make the file open into a table without reading it as an array first, so after researching some guides, I was able to do much better. So here is what I have so far:

 

Telephone Directory HTML Form:

 

<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>

 

Telephone Directory PHP:

 

<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 finally, the part I need help with, opening the information from the form:

 

<body>

<?php
$lines = file('telephonedirectory.txt');


foreach ($lines as $line_num => $line) {
    echo "Line #<b>{$line_num}</b> : " . htmlspecialchars($line) . "<br />\n";
}

?>
</body>
</html>

 

So far so good with it opening it into an array; however, I can't get it to do some other things. It's displaying all of the information from the form, but I only want it to show the last name, first name, and phone number. Also, I need it to be alphabetical as well, according to last name, and the hardest part is that I also need all this information to be displayed into a two column table instead of just listed. I'm still looking up guides, and I think I have a good start, but I'm really having some trouble, and i would greatly appreciate some help, even if just a little. Thank you so much.

Link to comment
Share on other sites

Okay, $lines is an array and $line is an item from that array

$line will be a string but will have the 7 items you want, as these are comma delimited you can use explode() to convert the string to an array (the 7 items) that will help you pull out the data, ie $item[0] would be LastName

 

Now for sorting see the PHP User Contributed Notes for uasort "php arobase kochira period com"

 

 

3. Users will not post their homework questions expecting to get their homework coded for them. If you have a question about part of the assignment that you do not understand' date=' please post asking for an explanation rather than the code.[/quote']
Link to comment
Share on other sites

Okay, $lines is an array and $line is an item from that array

$line will be a string but will have the 7 items you want, as these are comma delimited you can use explode() to convert the string to an array (the 7 items) that will help you pull out the data, ie $item[0] would be LastName

 

Now for sorting see the PHP User Contributed Notes for uasort "php arobase kochira period com"

 

 

3. Users will not post their homework questions expecting to get their homework coded for them. If you have a question about part of the assignment that you do not understand' date=' please post asking for an explanation rather than the code.[/quote']

 

Thanks so much for your help!

 

I really had a hard time understanding it, but it's making more sense now. I do, however, have another question.

 

You said that I could make $item[0] the Last Name, but how? If I just put $item[0], it won't know that I'm talking about the last name, and I'm not sure how to tell it.

Link to comment
Share on other sites

You'd want to add the following line within you foreach loop after the echo.

$item = explode(', ', $line);

 

Now you can do this within your loop

echo $item[0];

To display the lastName from the current line, echo $item[1]; will return the firstName, echo $item[2]; will return the address etc.

Link to comment
Share on other sites

Thank you soooo much! I've spent like 20 hours on this, if not more, and it's making me want to pull my hair out, I really am having a hard time, especially since this is my first php class and I know nothing about it.

 

Ok, so the code worked for the most part, but I guess I'm not putting the echo in the right place. I put it after my other echo, but should I have two echos? I keep getting an error that says undefined variable, I'm probably putting it in the wrong place.

 

This is what I did

<?php
$lines = file('telephonedirectory.txt');


foreach ($lines as $line_num => $line) {
echo "Line #<b>{$line_num}</b> : " . htmlspecialchars($line) . "<br />\n";
echo $item[0];
$item = explode(', ', $line);

}

?>

Link to comment
Share on other sites

Like this?

 

<?php
$lines = file('telephonedirectory.txt');

foreach ($lines as $line_num => $line) {
echo $item[0];
echo "Line #<b>{$line_num}</b> : " . htmlspecialchars($line) . "<br />\n";
$item = explode(', ', $line);

}

?>

 

It's giving me a lot of problems. I made up entries for the directory, and it's duplicating them and going all crazy! This is what happens when I open it:

 

Notice: Undefined variable: item in C:\wamp\www\ShowDirectory.php on line 15

Line #0 : anne,alison,123958 Reed Ridge,loopppp,va,31258,315-287-5635

anne,alison,123958 Reed Ridge,loopppp,va,31258,315-287-5635 Line #1 : anne,alison,123958 Reed Ridge,loopppp,va,31258,315-287-5635

anne,alison,123958 Reed Ridge,loopppp,va,31258,315-287-5635 Line #2 : a,a,123958 Reed Ridge,loopppp,va,31258,315-287-5635

a,a,123958 Reed Ridge,loopppp,va,31258,315-287-5635 Line #3 : zee,zaaa,123958 Reed Ridge,loopppp,va,31258,315-287-5635

zee,zaaa,123958 Reed Ridge,loopppp,va,31258,315-287-5635 Line #4 : maa,moo,123958 Reed Ridge,loopppp,va,31258,315-287-5635

 

Line 15 is echo $item[0];

Link to comment
Share on other sites

Ok, I'm an idiot. One big problem was relabeling everything, which I've done here:

 

<?php
$lines = file('telephonedirectory.txt');

foreach ($lines as $line_num => $line) {
echo "Line #<b>{$line_num}</b> : " . htmlspecialchars($line) . "<br />\n";
$lines = explode(', ', $line);
echo $line[0];
}
?>

 

Now my problem is still getting just the last name, first name, and phone number to appear. I have line[0] on there, but it still displays everything instead of just one part. It's kind of confusing me.

 

This is what shows up when I run the directory with made up entries:

 

Line #0 : Smith,Bob,123 Brooks lane,Brookland,NY,13642,315-287-5635

SLine #1 : Jones,Samantha,543 Larry Road,Brookland,NY,13642,315-287-5635

JLine #2 : Croft,Lara,543 Wilonia Cir,Brookland,NY,13642,315-287-5635

C

 

I don't know where the S, J, and C came from, and I still can't get it just to display the names and phone numbers. :/

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.