First time here? First time here? Newsletter Signup Newsletter
Login | Register | 651326 Members and growing!
Search tutorials
Author: Jeffrey F. Hill | Website: http://www.flash-db.com

Part II - Setting up the script

The below script is the only one that is needed.  The script included in the download has extra comments and other helpful information.  After reading over the script, read over the line by line comments located below.  That combined with the extra comments in the actual script should give you a good idea on how this script functions.  What it basically does is writes to a text file if the variable Submit from the Flash movie is equal to yes, separates the comments into an array then loads that array according to what the values for NumLow and NumHigh are.  In this manner we load 10 entries at a time from the text file.

	$Name
Email: $Email
Website: $Website
Comments: $Comments
Date: $Today

.:::."; 17) $New = "$Input$OldData"; 18) $fp = fopen( $filename,"w");  19) fwrite($fp, $New, 80000);  20) fclose( $fp );  21) } 22) $filename = "GuestBook.txt"; 23) $fp = fopen( $filename,"r");  24) $Data = fread($fp, 80000);  25) fclose( $fp ); 26) $DataArray = split (".:::.", $Data); 27) $NumEntries = count($DataArray) - 1; 28) print "&TotalEntries=$NumEntries&NumLow=$NumLow&NumHigh=$NumHigh&Guest Book="; 29) for ($n = $NumLow; $n < $NumHigh; $n++) { 30) print $DataArray[$n]; 31) if (!$DataArray[$n]) { 32) Print "

No More entries"; 33) exit; 34) } 35) } ?>

Lines 1-3,5:  These lines include some regular expressions that remove unwanted characters from the variables.  The most important character that we are removing  is the colon (:). (If the colon was not removed someone could potentially enter the same pattern that we are using to separate the text file into an array with and cause results to become mixed up).    These lines are however optional, you can remove them and the script will still work. 

Line 4: This replaces the string 'http://' when it is encountered in a submitted URL.  This part is removed so that when we add the 'http://' part back later we do not have two http://'s.

Lines 6-9:  These  lines are optional.  Basically what the stripslashes() function does is to remove slashes that where added to the data when posted from the Flash movie. 

Line 10: This line checks to see if the variable 'Submit' is equal to 'Yes'.  If it is the script will create a new entry in the database/textfile.  If not it will skip this step and move on to the second part of the script.  Remember that 'Submit' was set to 'Yes' when the user filled out the entry form and submitted it.

Line 11: The variable $filename is given the value of 'GuestBook.txt' which is the text file that we will be writing the new guest book entry to.

Line 12: Opens the file 'GuestBook.txt' for reading.

Line 13: Reads in the contents of 'GuestBook.txt' and assigns that value to the new variable '$OldData'.

Line 14: Closes the file that we previously opened.

Line 15: Gets the current date and time from the server and assigns it to the variable $Today.

Line 16: Formats all of the values entered by the user into a string.  Notice how we add the html formatting at this point.  Also notice the last part of this string that contains the pattern .:::. - we will be using that pattern to separate the text files into an array that we can split up and work with later.  That is  the most important part of this script

Line 17: This line takes the New Input and puts it in front of the old data from the text file.  In this way the newest results are always first.

Line 18: Opens the file 'GuestBook.txt' for writing.

Line 19: Writes the new data to that file.  Notice the limit of '80000'.  This is a fairly large number and you may want to reduce it.

Line 20: Closes the file for writing.

Line 21: Ends the if Statement that we started on Line 10.

Line 22: The variable $filename is given the value of 'GuestBook.txt' which is the text file that we will be writing the new guest book entry to. (Basically the same as on Line 11 although it's needed again because this script has a duel purpose).

Line 23:  Opens the file 'GuestBook.txt' for reading.  As above this line is basically the same as Line 12.  Because this script is not always used for adding a new entry we need to use it again.

Line 24: Reads in the contents of 'GuestBook.txt' and assigns that value to the new variable '$Data'.  

Line 25: Closes the file that we previously opened. 

Line 26: Splits the data that we just retrieved ('$Data') from the text file into an array.  The string is split whenever the script encounters the pattern '.:::.' - remember we added this after every new entry on line 16.  Now we have an array of strings split by the specified pattern in the variable $DataArray.  We can access any of these substrings in the array by specifying $DataArray[i]; where i is the specific Guest book entry number.

Line 27: This line just counts the number of times we matched the pattern '.:::.' in the previous line.  This corresponds to the current number of Guest Book entries that are in the Guest Book.   We send this value back to the flash movie so we know how many people have signed the Guest Book.

Line 28: This formats the first part of the string we send back to the flash movie. Notice how this line ends with 'GuestBook=' we will be adding the Guest Book entries with the next couple of lines.

Line 29: This sets up a For Loop.  We specify the starting value to be equal to NumLow (remember back to the flash movie).  Then we keep looping (increasing $n by 1) while $n is less then NumHigh.  

Line 30: Prints the corresponding Guest Book entry from the array to the string we are returning back to the Flash movie.  By limiting it to only returning 10 at a time we increase it's speed and manageability.

Line 31: Checks to see if their is a value for that Guest Book entry.

Line 32: If the Guest Book entry is empty we print out a message telling the user -  'No More Entries'.

Line 33: At which point we exit the loop.

Line 34: Closes the If loop - (The one that checks to see if their are any more entries).

Line 35: Closes the For Loop.

And that's it.

» Level Advanced

Added: 2001-12-21
Rating: 8.92 Votes: 200
(10 being the highest)
» Author
Jeffrey Hill is a freelance web developer from Boulder, Colorado. He specializes in creating and developing dynamic database driven Flash content and applications. Specialty's include SQL, PHP, Perl, and XML.
» Download
Download the files used in this tutorial.
» Forums
More help? Search our boards for quick answers!

Comments

  • There are no comments yet. Be the first to comment!

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.
Featured Flash FLA
» Author: Inocreato
» Title: RaiseTheBlocks
» Description: Raise all the blocks to win the game
Featured Sound Loops
Image for equalize

Audio Player

» Author: TomCat Carty
» Title: The Wood
» Description: Just a little game ending or it can maybe be looped. Recorders with music box and percussion to give the feel of well, I don't know, the woods? Free to use, just credit me. thank you
Latest Font
» Author: Fábio FAFERS
» Description: I created this font for free use. Everyone can apply it in personal or business texts. Its free, but I want to be communicated in case of business use. Donations are accepted to keep the project of free fonts alive! Thank you all
Featured Sound Fx
Image for equalize

Audio Player

» Author: Davisigner
» Description: Hmm... what to say about this one? It's reminiscent of the closing notes of the opening music from the Three Stooges done in a church organ style with a closing cymbal crash. I'll give this one away gratis, but feel free to check out my free loops and potential upcoming license-mandated ones over in the respective part of Flashkit.