Archive for February, 2007

PHP Lesson 8

February 14th, 2007

Sunny Man’s TutorialsMy lessons are now into their second week and we’ve still got a way to go yet. This week we will be looking into IF and WHILE statements, $_GET, $_POST and we’ll also look into arrays near the end of the week.

Yesterday we did a simple lesson on $_GET and how to get values from a URL. Today another simple lesson on $_POST and the confusion that comes with it. I introduced you to $_POST in last Friday’s big special and today we are going into much more detail.

Lesson 8: $_POST and Forms

As I showed in Friday’s round up, we use $_POST to collect the values of submitted form data. Some confusion that I had for a while was whether or not to use the name or id attribute in the form fields to let $_POST work. I’ll settle this now – for $_POST to collect the data from the form fields, you need to assign a name attribute to them. The id can be used for labels, CSS classes and other things. Here’s a sample form form.php:

<form action="process.php" method="post">
First Name: <input type="text" name="fname" size="25" />
Last Name: <input type="text" name="lname" size="25" />
Over 16? <input type="radio" name="age" value="1" /> Yes <input type="radio" name="age" value="0" /> No
<input type="submit" value="submit form" />
</form>

In the above form we would get 3 values returned through $_POST. These would be the first name, last name and over 16 values. Here’s how we’d capture these values. This file would be process.php, as determined in the form’s first line:

<?php
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$age = $_POST['age']; //either 1 or 0

$name = $fname." ".$lname; //stitch the first and last names together
?>

Pretty simple to get the form fields’ values. On Friday we are going to combine everything and a lot of form fields to create a signup form which will include this, arrays and a bit of validation.

PHP Lesson 7

February 13th, 2007

Sunny Man’s TutorialsMy lessons are now into their second week and we’ve still got a way to go yet. This week we will be looking into IF and WHILE statements, $_GET, $_POST and we’ll also look into arrays near the end of the week.

In the previous lesson we went into detail about IF and WHILE statements and in Friday’s big round up I introduced you to $_POST and $_GET. Today we’re going to look further at $_GET and what it’s used for.

Lesson 7: $_GET and URLs

When we have a URL that includes the values of certain variables, we can use this to get the values. Say we had a URL like this:

http://www.eop.org.uk/index.php?name=sunny%20man&cool=yes

We use $_GET to grab the values of the name and cool variables. Obviously this isn’t a secure way to transport variables around your site – we’ll look at a more secure way later on into the tutorials – this is the most simple way.

<?php
$name = $_GET['name']; //$name = "sunny man"
$cool = $_GET['cool']; //$cool = "yes"
?>

Tomorrow I’ll be talking about $_POST and how it can be confusing when to use the name and id attributes on form field inputs.

PHP Lesson 6

February 12th, 2007

Sunny Man’s TutorialsMy lessons are now into their second week and we’ve still got a way to go yet. This week we will be looking into IF and WHILE statements, $_GET, $_POST and we’ll also look into arrays near the end of the week.

As always we will find a useful example we can apply everything to at the end of the week as well as introducing some things we are going to look at in the following week. I hope you’ve learnt something in the previous week but don’t worry we will get more advanced as time goes on.

Lesson 6: IF and WHILE

In last week’s final day lesson I introduced you to an IF statement. I’m going to go over them in more detail today as well as introducing you to a WHILE loop. Here’s the IF statement I used in Friday’s example:

if ($username == "username" && $password == "password") {
     ...
}

This says: if $username’s value is “username” and if $password’s value is “password” then do this… If the statement in the ( ) brackets are true then it executes the instructions within the { } brackets.

There are a variety of comparison operators that you can use. The one used in the example is == which means equal to. There is also != – not equal to, === – exactly equal and of the same type, !== not exactly equal and not of the same type, > – is bigger than, < – is smaller than, >= – is bigger than or equal to, <= – is smaller than or equal to.

If you want to check more than one thing in an IF statement then you need to use logical operators, such as in the example I’ve used && to indicate that the statement should be true if the username and the password are both correct. Here are some examples:

if ($username || $password) { ... } // OR
if (!$username) { ... } // NOT
if ($username XOR $password) { ... } // XOR (exclusive OR)

Now we have got the hard bit out of the way we can now look at WHILE statements. WHILE statements are a little different to IF statements because they perform the instructions inside the { } brackets while a comparison remains true.

<?php
$a = 1;
$b = 5;

while ($a < $b) { //while $a is less than $b
     echo $a;
     $a++; //increment $a by 1
}
?>

WHILE loops come in very handy sometimes – for example when we look at arrays we will use a WHILE loop to good effect. A famous example of using ‘the loop’ is the WordPress blogging system. This is the basic form of IF and WHILE loops. Remember them!

News Roundup

February 12th, 2007

News Roundup - Week Ending 11/02/07

Another instalment of News Roundup. If you can attach a piece of news to all the symbols, leave a comment! There are 6 symbols – how many pieces of news can you attach to them?!

Last Week’s Answers
  • NTL and Virgin merge – Virgin Media
  • Intel débuts new 45nm process
  • Windows Vista officially launched
  • BBC get go ahead for on-demand service