Joe Sai Posted August 5, 2009 Share Posted August 5, 2009 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 https://forums.phpfreaks.com/topic/168894-help-with-arrays/ Share on other sites More sharing options...
abazoskib Posted August 5, 2009 Share Posted August 5, 2009 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 https://forums.phpfreaks.com/topic/168894-help-with-arrays/#findComment-891094 Share on other sites More sharing options...
Joe Sai Posted August 5, 2009 Author Share Posted August 5, 2009 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 https://forums.phpfreaks.com/topic/168894-help-with-arrays/#findComment-891099 Share on other sites More sharing options...
Joe Sai Posted August 5, 2009 Author Share Posted August 5, 2009 Anybody have any idea what's wrong? I'm confused and I desperately need help, it's 2AM and I have work in the morning, I really need help. :/ Link to comment https://forums.phpfreaks.com/topic/168894-help-with-arrays/#findComment-891103 Share on other sites More sharing options...
Joe Sai Posted August 5, 2009 Author Share Posted August 5, 2009 Any comments at all? I feel like I'm being ignored here and I am very confused, I really, really need some help please. Link to comment https://forums.phpfreaks.com/topic/168894-help-with-arrays/#findComment-891111 Share on other sites More sharing options...
watye Posted August 5, 2009 Share Posted August 5, 2009 You should read the file line by line and then use the function "explode" to fetch those columns you need. Link to comment https://forums.phpfreaks.com/topic/168894-help-with-arrays/#findComment-891114 Share on other sites More sharing options...
Joe Sai Posted August 5, 2009 Author Share Posted August 5, 2009 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 https://forums.phpfreaks.com/topic/168894-help-with-arrays/#findComment-891116 Share on other sites More sharing options...
Joe Sai Posted August 5, 2009 Author Share Posted August 5, 2009 Anyone? Link to comment https://forums.phpfreaks.com/topic/168894-help-with-arrays/#findComment-891128 Share on other sites More sharing options...
Joe Sai Posted August 5, 2009 Author Share Posted August 5, 2009 Come on, please. Link to comment https://forums.phpfreaks.com/topic/168894-help-with-arrays/#findComment-891134 Share on other sites More sharing options...
Joe Sai Posted August 5, 2009 Author Share Posted August 5, 2009 Ok, well I'm about to leave for work, I hope someone can help me before I get back. I am really stumped. :/ Link to comment https://forums.phpfreaks.com/topic/168894-help-with-arrays/#findComment-891294 Share on other sites More sharing options...
abazoskib Posted August 5, 2009 Share Posted August 5, 2009 the manual is your friend. a lot of times they have sample code that will help out a lot. http://us.php.net/fgets Link to comment https://forums.phpfreaks.com/topic/168894-help-with-arrays/#findComment-891321 Share on other sites More sharing options...
Joe Sai Posted August 5, 2009 Author Share Posted August 5, 2009 the manual is your friend. a lot of times they have sample code that will help out a lot. http://us.php.net/fgets Thanks for responding. I used one of their guides to help me with the table, which I posted above, but it's giving me those errors and I don't understand why. Link to comment https://forums.phpfreaks.com/topic/168894-help-with-arrays/#findComment-891697 Share on other sites More sharing options...
abazoskib Posted August 5, 2009 Share Posted August 5, 2009 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 https://forums.phpfreaks.com/topic/168894-help-with-arrays/#findComment-891714 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.