![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Jul 2004
Location: Location
Posts: 138
Rep Power: 5
![]() |
Php Tutorial 3
Before starting this tutorial, I highly recommend looking over tutorial #1 and #2 created by SyringeX. All of these steps lead up to one another, and if you haven't read those tutorials, I highly doubt that much of this will make sense.
~SyringeX What will we be covering in this tutorial? In this tutorial I aim to teach you some quick things, they don't relate to eachother specifically, but this is pretty much the last general php code that I want to show you before we start moving into more advanced scripts, ones that use mysql in them. We will be looking at arrays, php and forms, and cookies/sessions. So, let's get to Arrays I probably should've mentioned arrays in an earlier tutorial, but I think that they fit fine here. Let's start with this, php variables can only store one piece of information, an array can store more than one pice of information at a time, which can help in various aspects of php. So basically, an array is a variable that can store multiple pieces of information. Each piece of information inside of our arrays is know as an element. So, enough with the explaining and let's take a look at an example script, which will create us our array: php Syntax (Toggle Plain Text)
Now, I don't think there is a point to save and open this one YET, as nothing will be displayed, so let's take a look at what we have: In case you didn't read my comments in the code, here you are: $coolarray is the name of our array, and = array() tells the server that this particular variable will be an array. All information within the parentheses are the elements, and must be enclosed in quotes. After each element, if more elements follow, you must include a comma. You can have as many elements that you want within an array. Now let's look at creating an associative array. The difference in creating an associative array is that you need to define the key, and the value of the element. It can be created the easiest by using the array() function, which we used above. Let's take a look at creating and displaying an associative array: php Syntax (Toggle Plain Text)
Now is the time to save it, and load it as told in the first tutorial. Now, once again if you didn't read my comments in the code... => is also known as equal to or greater than, it's basically the same, but the first is the key, and the second is the value of the element, let's take a look at displaying it. As you see, we use our echo to print out our words, then we declare the variable array $ranks, and within the brackets is the key that we want to use. So the output to our browser would say, SyringeX is the Best. Try changing the key and see what you come up with, and try making some of your own arrays. Well, that concludes the introduction to arrays, so on comes our next stop... PHP and Forms! Now, forms and PHP are probably more of the fun parts. It allows users to interact with your site, whether it is a tagboard script, a generator, or a contact script. They can be used to retrieve information about a user, or other ways. When working with forms and php you will need two separate files, one with the forms, and another that uses php code to process that information. Let's take a look at our basic form set-up that we will be practicing with, this page can be simple HTML, and not a php file: php Syntax (Toggle Plain Text)
That's our basic form that we will be using. I would suggest naming this form1.html or something similar. The names in the <input> tags will be the variable that the processor uses to check the data. So in our text input, the server will see the information as the variable $textfield1. This is basically how I created those generators you might have seen, they're actually pretty simple. The action="" is what the submit button will take you to when it is clicked, it will process the information into how we want it. Now that we have our forms down, create a new file called formprocessor.php. In this file, we'll write a few things that will differently display what the user types in our forms from form1.html. First, let's just write a script using echo to display what they wrote: php Syntax (Toggle Plain Text)
Now, save this is formprocessor.php. Open up your form1.html file in your browser, and fill out your text field, then click submit. If all went well, what you wrote in the text field should be displayed on the next page. If not, check to make sure you named your field the same as your variable in the formprocessor.php file. You can use as many text fields as you want and name them exactly as you want. The variable on the processing page will always be the name of the text field that you created. Try adding more forms, and try making more complicated scripts in your script processor and see what you can come up with. This is exactly how I made some of those generators. Well, forms and php are easy to use aren't they? So after your done experimenting, let's move on to.... Cookies Now, of course as I always say, these are also a very important part of PHP. Cookies store information on a text file in a user's computer. Cookies are generally small, because the information that they carry isn't to big itself. Usually a cookie can be used in login scripts, so the user doesn't have to keep logging on. It also can store a user's username and password so that you can verify if the user is logged in or not (Generally used in portals). Cookies should be used when you need to store the information for long periods of time. Let's take a look at how our cookie is set: php Syntax (Toggle Plain Text)
Save it, and now open it. You'll notice nothing has been displayed, but now a cookie has been stored on your computer, telling it that you have accessed this page before. Now let's look at a script that a user will see if they access this page again. php Syntax (Toggle Plain Text)
Cool, now when you access this page again, you get a message saying that you have accessed it before, and it declares the value that you put into the cookie. In fact, you know how you don't have to log in repeatedly to IFskinzone? That is because there is a cookie set when you are logged in.This is just a simple cookie (The one that we used, not the forum cookie used here), and later on, you will learn how to store variables into cookies when forms are used (We will go over this in a section with MySQL and forms that I plan to write in another tutorial) Also, as in the comments we saw output buffering, let me explain output buffering a bit... An output buffer is used when you need a large amount of output sorted through, I'll get into output buffering into depth later on when we work on the MySQL and forms section in another tutorial, where you will attempt to write a MySQL powered user login script, but for now, I think it's time to look at deleting cookies. Now, say if the user wanted to log out, we would delete the cookie. Let's look at how we do it: php Syntax (Toggle Plain Text)
And that is basically it for simple cookies in this tutorial. You might not know it, but we just covered tons of useful information, this will definitley help you with writing your own personal scripts. So, look for the fourth tutorial by SyringeX, where we might be getting into MySQL! This concludes tutorial 3 of php by SyringeX Created by Syringex Last edited by big_k105; Nov 16th, 2007 at 9:28 AM. |
|
|
|
|
|
#2 |
|
Newbie
Join Date: Mar 2005
Posts: 13
Rep Power: 0
![]() |
Thanks for another great tutorial, I found arrays one of the most confusing things when I started PHP. The online documentation at PHP.org, or it PHP.net, was most confusing and went well above my head when I just wanted to know the basics about arrays it was banging on about keys and complex stuff.
|
|
|
|
|
|
#3 |
|
Expert Programmer
|
PHP and Forms problems
When ever i click on the button it opens the file and does not display anything.
![]() |
|
|
|
|
|
#4 |
|
Newbie
Join Date: May 2006
Posts: 6
Rep Power: 0
![]() |
A good tutorial, very useful.
Crawford, have you downloaded PHP, so you can view it on your browser? |
|
|
|
|
|
#5 |
|
Expert Programmer
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 3
![]() |
Err... he posted that post like over a year ago.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Assembly tutorial, part one. | Mad_guy | Software Design and Algorithms | 21 | Apr 15th, 2006 7:02 PM |
| Basic HTML Tutorial - Reuben Keeney | ReubenK | HTML / XHTML / CSS | 14 | Mar 26th, 2006 5:50 AM |
| Tutorial - Using MySQL in C# | Darkhack | C# | 12 | Jan 17th, 2006 9:28 AM |
| [tutorial] Simple G++ compiler tutorial | coldDeath | C++ | 7 | Nov 27th, 2005 12:33 PM |
| Character creation tutorial | jayme | C++ | 17 | Nov 14th, 2005 3:17 PM |