Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jul 9th, 2004, 1:09 PM   #1
bulio
Hobbyist Programmer
 
bulio's Avatar
 
Join Date: Jul 2004
Location: Location
Posts: 138
Rep Power: 5 bulio is on a distinguished road
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)
  1. <?php
  2.  
  3. $coolarray = array("SyringeX", "PHP", "MySQL"); /* $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. */
  4.  
  5. ?>

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)
  1. <?php
  2.  
  3. $ranks = array("SyringeX"=> "Best", "SyringeXJR"=> "Second_Best", "SyringeXthe3rd"=> "Worst");
  4. /* => 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. */
  5.  
  6. echo 'SyringeX is the ' . $ranks["SyringeX"];
  7. /* 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. */
  8.  
  9. ?>

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)
  1. <HTML>
  2. <HEAD><TITLE></TITLE></HEAD>
  3. <BODY>
  4. <form name="form" action="formprocessor.php" enctype="multipart/form-data">
  5. <input type="text" name="textfield1" value="Insert Text Here">
  6.  
  7. <input type="submit" action="formprocessor.php" value="Try It!">
  8. </form>
  9. </BODY>
  10. </HTML>

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)
  1. <?php
  2.  
  3. echo $textarea1; /* Does that name sound familiar? Yup, that's the name we named our text field in form1.html. All the names of your inputs will be turned into variables, and the value of each variable will contain what the user wrote in the text field. This will simply just display the text they wrote. */
  4.  
  5. ?>

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)
  1. <?php
  2.  
  3. setcookie("accessedbefore", "SyringeX") /* You need to specify at least two elements in your cookie, the first one is the name of your cookie, the second one is the value that it holds. */
  4.  
  5. ?>

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)
  1. <?php
  2.  
  3. if(isset($_COOKIE['accessedbefore'])) {
  4. /* This checks to see if the user has recieved this cookie or not. As you see it uses the IF statement, so everything after this bracer will be what the user sees if they have already recieved the cookie. */
  5.  
  6. echo 'Hi, when you last accessed this page, your username was ' . $_COOKIE["accessedbefore"]';
  7. } else {
  8.  
  9. ob_start(); /* Begins output buffering, look below for more information. */
  10. setcookie("accessbefore", "SyringeX");
  11. /* Sets our beloved cookie */
  12. ob_end_clean(); /* Ends output buffering */
  13. echo "You have not accessed this page before, next time you view this, you will have an automated message.";
  14. }
  15.  
  16. ?>
  17.  

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)
  1. <?php
  2.  
  3. if(isset($_COOKIE['accessedbefore'])) {
  4. ob_start();
  5. setcookie("accessedbefore", ""); /* When the page with this cookie is accessed, most likely a page that processes the log out, it will set the value of the cookie to null (nothing) and the cookie will be deleted */
  6. ob_end_clean();
  7. } else {
  8. echo "There is nothing set for this cookie.";
  9. }
  10. ?>

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.
bulio is offline   Reply With Quote
Old Apr 13th, 2005, 4:27 AM   #2
Jonnno
Newbie
 
Join Date: Mar 2005
Posts: 13
Rep Power: 0 Jonnno is on a distinguished road
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.
Jonnno is offline   Reply With Quote
Old Jun 29th, 2005, 10:44 PM   #3
crawforddavid2006
Expert Programmer
 
crawforddavid2006's Avatar
 
Join Date: Apr 2005
Location: Not sure yet
Posts: 568
Rep Power: 0 crawforddavid2006 is an unknown quantity at this point
Send a message via AIM to crawforddavid2006 Send a message via MSN to crawforddavid2006
PHP and Forms problems

When ever i click on the button it opens the file and does not display anything.
crawforddavid2006 is offline   Reply With Quote
Old Jun 1st, 2006, 3:29 PM   #4
Glastea
Newbie
 
Join Date: May 2006
Posts: 6
Rep Power: 0 Glastea is on a distinguished road
A good tutorial, very useful.

Crawford, have you downloaded PHP, so you can view it on your browser?
Glastea is offline   Reply With Quote
Old Jul 16th, 2006, 4:00 AM   #5
Polyphemus_
Expert Programmer
 
Polyphemus_'s Avatar
 
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 3 Polyphemus_ is on a distinguished road
Err... he posted that post like over a year ago.
Polyphemus_ is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:21 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC