(Do NOT copy and paste the code! You need to learn it and typing it will help you to focus on it.)
| PHP-Code: | |
|
When you are done typing in that code, save it as "chatscript.php" and upload (FTP) it to your web server. Before you go on, try to just read through the code and see if you can follow the logic. Then run it a couple of times and post different things and see if you can understand most of the code.
Now here is the break-down of the script:
1) We start by putting the layout, filename, and comment form into strings so that we can keep our code clean by just using a string instead of putting all of that code into our loops...
| PHP-Code: | |
|
2) Next we check to see if anything was POSTED to the page. This will evaluate to FALSE if nothing has been "Posted" to the page (and FALSE means that it won't run).
However, after something has BEEN posted then this will be TRUE and the loop will run:
| PHP-Code: |
|
After we determined that a message was posted we then clean the message of all of the PHP, HTML, and Cussing that could ruin our conversation or point to spam sites. Finally we add ":END:\n" to the end of the text so that we can tell each comment apart.
3) Now that we have the cleaned message ready, we open the file and write the new text to it:
| PHP-Code: |
|
For those of you who don't know loops (read the lessons on them) I am going to write the [if][/i] loop in English:
| PHP-Code: |
if (!fwrite($filehandle, $text)) {
|
4) Now that we just finished checking to see if a message was posted it is time to show all of the messages in the file:
| PHP-Code: |
|
In English the if loop would read:
| PHP-Code: |
if the(number of array elements in "$contents" is greater than or equal to 1) { do this:
|
5) After we have grabbed all of the comments out of the file we just need to make another "safety" loop that will print "There are no comments" if for some reason the file is empty:
(Note: This is just a nice thing to add)
| PHP-Code: |
|
6) Finally, We add some formatting to the comments so that they stay in a box. and print everything to the screen!
| PHP-Code: |
|
Wow, good job! You finished it! (It took me ALL DAY to write this lesson!) Doesn't it feel good to have made something cool?!
Now I hope you followed the code and can now understand all of the loops and functions in it (If you are having trouble review the past lessons). If you just take code in small pieces it is a LOT easier to understand, so don't let anything intimidate you!
(Note: You can download the script at the bottom.)
##########################################################
## Another Chat Script!
##########################################################
Well, for those of you who want to make a more advanced chat system here is another script that allows you to enter your name, email, message, and the date! Now that you have finished the above script you shouldn't have very much trouble understanding this one.
Also, there are a couple of NEW functions in this script - don't panic - just stop by php.net, type the name of the function (such as "list()")in the search box, and they will show you what it is and tell you about it. Have a great day!
| PHP-Code: | ||||||
| ||||||
You can download both scripts here:
Lesson 19 Files
No comments:
Post a Comment