seadonkey Posted March 9, 2008 Share Posted March 9, 2008 More or less I am unable to figure out the proper code for using an array to create a guest book. What I need to do is not allow the same name to be used again and that sorts the book by name and removes duplicate names. I went to http://us.php.net/array_unique and did read up on it and tried to put it to the test. A sample of what I have come with so far with an array for this is // Collect information $FirstName = addslashes($_GET['first_name']); $LastName = addslashes($_GET['last_name']); $Email = addslashes($_GET['email_address']); // Load file into Array $GuestFile = file('guest_book.txt'); // Remove duplicates $GuestFile = array_unique($GuestFile); // Sort the collect information area sort($GuestFile, SORT_STRING); // Open the guest book to write too $GuestBook =fopen ("guest_book.txt","a"); The whole script does work but the problem is that it is not removing the dup and is sorting my the last name, not the first. Any sugguestions would be great. Link to comment https://forums.phpfreaks.com/topic/95150-basic-array-for-a-guest-book/ Share on other sites More sharing options...
uniflare Posted March 9, 2008 Share Posted March 9, 2008 hmm not removing dupes, the lines returned from file must be different values somehow.... try debugging by using "print_r($GuestFile);" before array_unique() tell us what it prints as HTML Source code Link to comment https://forums.phpfreaks.com/topic/95150-basic-array-for-a-guest-book/#findComment-487398 Share on other sites More sharing options...
seadonkey Posted March 9, 2008 Author Share Posted March 9, 2008 Array ( [0] => dfd,dfd,dfdd [1] => rudolph,michelle,[email protected] [2] => day,amy,sdfds [3] => as,day,ere [4] => as,day,ere [5] => test,test,test [6] => test,test,test [7] => cc,cc,cc [8] => cc,cc,cc ) so its still seeing the repeats Link to comment https://forums.phpfreaks.com/topic/95150-basic-array-for-a-guest-book/#findComment-487418 Share on other sites More sharing options...
uniflare Posted March 9, 2008 Share Posted March 9, 2008 that is very strange, im not sure... try this instead of array_unique: $GuestFile = array_keys(array_flip($GuestFile)); Link to comment https://forums.phpfreaks.com/topic/95150-basic-array-for-a-guest-book/#findComment-487432 Share on other sites More sharing options...
seadonkey Posted March 9, 2008 Author Share Posted March 9, 2008 I am getting 4 warning messages about this Warning: Wrong parameter count for fwrite() in /home/academyw/public_html/rudolph/SignTheGuestBook.php on line 33 Warning: sort() expects parameter 1 to be array, resource given in /home/academyw/public_html/rudolph/SignTheGuestBook.php on line 38 Warning: fwrite(): 3 is not a valid stream resource in /home/academyw/public_html/rudolph/SignTheGuestBook.php on line 40 Warning: fclose(): 3 is not a valid stream resource in /home/academyw/public_html/rudolph/SignTheGuestBook.php on line 43 <?php $NameExists =FALSE; if (file_exists("guest_book.txt") && filesize("guest_book.txt")> 0) { $GuestArray =file("guest_book.txt"); for($i=0; $i<count($GuestArray); ++$i){ $CurMessage = explode("~", $GuestArray[$i]); if(in_array(addslashes($LastName), $CurMessage)){ $NameExists =true; break; } } } if($NameExists) echo "<p>That name currently exists, please try again!</p>"; else { $GuestBook =fopen ("guest_book.txt" , "a"); fwrite($GuestBook); fclose ($GuestBook); stripslashes($Name) . " <br />"; stripslashes($LastName) . " <br />"; stripslashes($Email) . " <br />"; sort($GuestBook, SORT_STRING); if (is_writable("guest_book.txt")) { if (fwrite( $GuestBook, $Name . "," . $LastName . ",". $Email . "\n")) echo "<p>Thank you for signing our guest book! </p>"; fclose($GuestBook); } } ?> What I am trying to do is check to see if the user already enter in his/her name, to delete dup names and to sort names. Any suggestions on what is wrong would be great Link to comment https://forums.phpfreaks.com/topic/95150-basic-array-for-a-guest-book/#findComment-487932 Share on other sites More sharing options...
uniflare Posted March 9, 2008 Share Posted March 9, 2008 try: <?php // Collect information $FirstName = addslashes($_GET['first_name']); $LastName = addslashes($_GET['last_name']); $Email = addslashes($_GET['email_address']); // Load file into Array $GuestFile = file('guest_book.txt'); // Remove duplicates $GuestFile = array_keys(array_flip($GuestFile)); // Sort the collect information area sort($GuestFile, SORT_STRING); // Open the guest book to write too $GuestBook =fopen ("guest_book.txt","a"); ?> see php.net/function for more information on php functions eg: www.php.net/fwrite ------- first error is fwrite($GuestBook); should be: fwrite($GuestBook,"contents"); // but this is not necessary, to read from the file use "fread()" ---------- you are passing a file point ($GuestBook) to sort(), use fread to to read thge contents to a variable and explode the variable by "\n", then sort the array returned by the explode function. if you fix the sort() error you should also fix the last 2 errors at the same time, as i think sort is messing with the resource variable which is in turn messing the fwrite/fclose functions. i would use "file_get_contents" and "file_put_contents" personally. hope this helps, Link to comment https://forums.phpfreaks.com/topic/95150-basic-array-for-a-guest-book/#findComment-487943 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.