Joe Sai Posted August 10, 2009 Share Posted August 10, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/169657-homework-help-please/ Share on other sites More sharing options...
MadTechie Posted August 10, 2009 Share Posted August 10, 2009 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'] Quote Link to comment https://forums.phpfreaks.com/topic/169657-homework-help-please/#findComment-895030 Share on other sites More sharing options...
Joe Sai Posted August 11, 2009 Author Share Posted August 11, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/169657-homework-help-please/#findComment-895793 Share on other sites More sharing options...
Joe Sai Posted August 11, 2009 Author Share Posted August 11, 2009 Is there anyone that can help me with that? Quote Link to comment https://forums.phpfreaks.com/topic/169657-homework-help-please/#findComment-895808 Share on other sites More sharing options...
wildteen88 Posted August 11, 2009 Share Posted August 11, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/169657-homework-help-please/#findComment-895812 Share on other sites More sharing options...
Joe Sai Posted August 11, 2009 Author Share Posted August 11, 2009 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); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/169657-homework-help-please/#findComment-895824 Share on other sites More sharing options...
wildteen88 Posted August 11, 2009 Share Posted August 11, 2009 Switch the last two lines in your foreach around. Quote Link to comment https://forums.phpfreaks.com/topic/169657-homework-help-please/#findComment-895826 Share on other sites More sharing options...
Joe Sai Posted August 11, 2009 Author Share Posted August 11, 2009 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]; Quote Link to comment https://forums.phpfreaks.com/topic/169657-homework-help-please/#findComment-895833 Share on other sites More sharing options...
Joe Sai Posted August 11, 2009 Author Share Posted August 11, 2009 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. :/ Quote Link to comment https://forums.phpfreaks.com/topic/169657-homework-help-please/#findComment-895864 Share on other sites More sharing options...
wildteen88 Posted August 11, 2009 Share Posted August 11, 2009 Umm, this $lines = explode(', ', $line); echo $line[0]; should be $item = explode(', ', $line); echo $item[0]; Quote Link to comment https://forums.phpfreaks.com/topic/169657-homework-help-please/#findComment-895887 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.