Jump to content

basic array for a guest book


seadonkey

Recommended Posts

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
Share on other sites

Array

(

    [0] => dfd,dfd,dfdd

 

    [1] => rudolph,michelle,testemail!@yahoo.com

 

    [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
Share on other sites

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

Link to comment
Share on other sites

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