• No results found

Click on Submit Query and these names will be displayed.

do while Loops

10. Click on Submit Query and these names will be displayed.

How It Works

This deceptively simple task isn't actually as simple as it first might have seemed. We start with the simple

HTML file dynamic.html, which takes the number of children as form input. This number is passed as the

variable $Number to the dynamic.php script.

How many children do you have? <INPUT NAME="Number" TYPE="Text">

Then in our PHP script, we started by introducing a little trickery. We mentioned in Chapter 1 that if you pass HTML tags along as part of a text statement, the PHP is translated into HTML. The same idea applies to the

contents of any statements, so by sending a <FORM> tag as part of an echo statement, we're telling the

browser that we want to create a form.

<FORM METHOD=GET ACTION="dynamic2.php"> <?php

echo

Once we're inside the <FORM> tag, we need to create a number of text boxes. We know how many we need, as

the user will have supplied this number on the last page, and it will be contained in the variable $Number.

This is a perfect time to introduce a for loop. We want to display the number of text boxes to match

$Number, but if the user doesn't have any children, then we don't want to display any text boxes at all. This

means we have to start the $Counter at zero, and if the value in $Number is zero, we want to jump over the

loop entirely. Hence the counter starts at zero, and is incremented by one at the end of each loop. The loops continue while the counter is lower than the number of children:

for ($Counter=0; $Counter<$Number; $Counter++) {

Inside the loop we're faced with another problem. We need to personalize each text box with a number, yet if we display the current count of the loop, the text box number will be one below what it should be. So we

create an 'offset variable' to get around this, which is just $counter plus one:

display another message:

if ($Counter==0) echo"Press the button to move on"; ?>

The third script is the one that just prints out the names you've just supplied to the array. Sounds easy enough,

but first you must take into account the fact that the variable $Number has not been passed to the new script.

It only existed in the last page, dynamic.php. So, we're left with a situation where we know names might

have been supplied to the array $Child[], but we don't know how many, if any. This means we resort to our

old friend the while loop, and we have to set the child counter $Count manually. We start it at zero,

because it might be zero.

$Count=0;

We display a text message on the web page:

echo "Your children's names are:";

We then start a do while loop, as we want the contents of the loop to run at least once:

do {

On entering the loop, we display the first item in the array, $Child[0]. If there is nothing in the array, then

nothing apart from the line breaks will be displayed. Otherwise it will display the first child's name:

echo"<BR><BR>$Child[$Count]";

We then alter the contents of the $CheckEmpty variable to be the contents of $Child[0]. If it's empty,

$CheckEmpty will trigger the end of the loop later on. If it's not empty we will iterate again.

$CheckEmpty = "$Child[$Count]";

We increment the count:

$Count=$Count+1;

Then we check to see if $CheckEmpty is an empty variable. If it is we drop out of the loop; if not we

number of name fields to read in; loops allow us to be more dynamic.

Arrays

We briefly encountered arrays already earlier in this book, and have just encountered them again. It's now time to introduce them more formally. Arrays are a set of variables which all have the same name, but each has a

different index. Each member of the array is called an element. You can create arrays in the same way you

create variables, as long as you remember to put the square brackets around them to denote the index:

$StatesOfTheUSA[1] = "Washington"; $StatesOfTheUSA[2] = "California";

You don't have to assign them in order numerically, you can jump as few, or as many entries as you want:

$StatesOfTheUSA[49]="Alaska"; $StatesOfTheUSA[13]="Alabama";

In fact you can dispense with the numeric indexing completely and use characters instead. Arrays like this are often known as associative arrays:

$StateCapital["ca"] = "Sacramento"; $StateCapital["il"] = "Springfield";

Note that, if you wish to access the contents of an associative array, you can drop the quotation marks

surrounding the index value if you wish. To display Sacremento, you can type either:

echo $StateCapital["ca"];

or

Setting the initial values of array variables is, surprisingly enough, a process known as initialization. We've come across the first way to initialize arrays in two examples in this book already: we don't worry about the indexing and let PHP do it automatically for us. We create one item in the array, then we create another with the same name:

$Author[]="William Shakespeare"; $Author[]="Franz Kafka";

Without the square brackets, PHP would not have known that we are dealing with array variables, and would have replaced the first value with the second one: the square brackets indicate that we want to store the values in an array.

The lack of an index value lets PHP decide where to put them. You'll find that if the $Author[] array hasn't been

used before, then the values above will be stored in $Author[0] and $Author[1]. PHP will carry on assigning

new values to the next element in the array.

We have also encountered the second way to initialize an array, using explicit index values:

$Author[0]="William Shakespeare"; $Author[1]="Franz Kafka";

Here we don't have abide by the constraints of auto-numbering that PHP would otherwise impose on us − we can

assign index values out of sequence, as described earlier. PHP is different from many programming languages, where arrays are concerned, on two counts. First, we don't have to predefine the data type of the array, stating whether it will contain numbers or text. This is consistent with PHP's policy on variables: you don't have to choose a data type, PHP does it for you. Second, you also don't have to specify how large the array is before it is created. Once again, PHP determines how large the maximum index number in the array needs to be; for example

$Author[] only needs to contain 2 items.

There are two more ways of populating arrays in PHP and both make use of the array() construct. If we

take our authors code snippet, it can be redefined as follows:

$Author = array ("William Shakespeare", "Franz Kafka");

Once again we're asking PHP to automatically generate the index values. Again, the index values start at zero, and new values placed in the array are placed in the unfilled element with the lowest index. If you were to

echo the contents of $Author[1] after this line of code, it would contain Franz Kafka.

However, this can be a little counter-intuitive, you know there are 50 states in the USA, and if you finish with

49, you will have the niggling feeling that one has been missed out. To get around this, the array() function

allows you to pick the index you want the array to start at. It makes use of the => operator to do this, as follows:

$StatesOfTheUSA = array (1 => "Alabama", "Alaska", "Arizona", "Arkansas", "California", "Colorado", "Connecticut", "Delaware", "Florida", "Georgia", "Hawaii", "Idaho", "Illinois", "Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana", "Maine", "Maryland", "Massachusetts", "Michigan", "Minnesota", "Mississippi", "Missouri", "Montana", "Nebraska", "Nevada", "New Hampshire", "New Jersey", "New Mexico", "New York", "North Carolina", "North Dakota", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota", "Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Washington", "West Virginia", "Wisconsin", "Wyoming");

In other words, you place the number you want the index to start at, followed by the => operator and then your

list of values as normal. Now if you were to echo the contents of $StatesOfTheUSA[50], it would be

Wyoming, whereas without this shift of one it would have been empty. It doesn't have to be 1, it equally be 101, and then Wyoming would have the index of 150.

If you wish to index a large associative array, then you have to set each value individually. For example, with our states, it might be as follows:

$StatesOfTheUSA = array ("al" => "Alabama", "ak" => "Alaska", "az" => "Arizona", "ar" => "Arkansas", "ca" => "California", "co" => "Colorado", "ct" =>

"Connecticut", "de" => "Delaware", "fl" => "Florida", "ga" => "Georgia", "hi" => "Hawaii", "id" => "Idaho", "il" => "Illinois", "in" => "Indiana", "ia" => "Iowa", "ks" => "Kansas", "ky" => "Kentucky", "la" => "Louisiana", "me" => "Maine", "md" => "Maryland", "ma" => "Massachusetts", "mi" => "Michigan", "mn" => "Minnesota", "ms" => "Mississippi", "mo" => "Missouri", "mt" => "Montana", "ne" => "Nebraska", "nv" => "Nevada", "nh" => "New Hampshire", "nj" => "New Jersey", "nm" => "New Mexico", "ny" => "New York", "nc" => "North Carolina", "nd" => "North Dakota", "oh" => "Ohio", "ok" => "Oklahoma", "or" => "Oregon", "pa" => "Pennsylvania", "ri" => "Rhode Island", "sc" => "South Carolina", "sd" => "South Dakota", "tn" =>

Related documents