class 3intro to databases arrays sending values to a script manually for and while loops
TRANSCRIPT
Class 3Intro to Databases
Arrays
Sending Values to a Script Manually
For and While Loops
Class 3Intro to Databases
Arrays
There are 2 main types of arrays in PHP
•Numerical Arrays
•Indexed Arrays
We’re going to look at how to create and access these variables.
In PHP there are also several “built-in” arrays. These are called superglobal arrays. We’re going to look at 2 commonly used in form processing.
$_POST
$_GET
Class 3Intro to Databases
Numerical Arrays
An numeric array is a list of items, for example:
$artists=(“Alice Neel”, “Robert Rauschenberg “, “Kiki Smith”, “Dan Flavin”);
To create an array
$artists=array();
To put values in an array
$artists=(“Alice Neel”, “Robert Rauschenberg “, “Kiki Smith”, “Dan Flavin”);
Will add all at once
or
$artists[4]= “Giotto”;
Will add at a specific location
or
$artists[]= “Giotto”;
Will add at end
Class 3Intro to Databases
Numerical Arrays
$artists=(“Alice Neel”, “Robert Rauschenberg “, “Kiki Smith”, “Dan Flavin”);
To access values
To access values in numerical arrays, we use the index number
$this_artist= $artists[0];
echo "My favorite artist is ".$this_artist; //prints Alice Neel
Download arrays_numeric.zip
Change arrays_numeric.php to print out an artists name
Note that arrays start at ZERO
Class 3Intro to Databases
Numerical Arrays
To add values$artists[4]= “Giotto”;
Will add at a specific location
or$artists[]= “Giotto”;Will add at end
Either will give us
$artists=(“Alice Neel”, “Robert Rauschenberg “, “Kiki Smith”, “Dan Flavin”, “Giotto”);
In arrays_numeric.php
Add an artist’s name to the end of the array
And print it out
$this_artist= $artists[4];echo "<br> or maybe it's ".$this_artist; //prints Giotto
Class 3Intro to Databases
Getting the number of elements in an array
/ /get the count of an array$total_artists=count($artists);echo "<p>There are ".$total_artists." artists in my list";
In arrays_numeric.php print out the number of elements in your array
Class 3Intro to Databases
While Loop
$artists=(“Alice Neel”, “Robert Rauschenberg “, “Kiki Smith”, “Dan Flavin”, “Giotto”);
The While Loop//using the WHILE loopprint("<p><b>Using the WHILE loop</b>");
$i=0;while($i<$total_artists){
print("<br> $artists[$i]");$i++;
}
In arrays_numeric.php use the WHILE loop to print out all the values
Note that we are keeping track of 3 variables here
$artist is the array holding the artists names
$total_artist holds the number of items in our array
$i is used as a counter in the WHILE loop.We initialize it at 0We set the condition that it must be less than $total artistAnd we increment 1 each time it goes through the loop
Class 3Intro to Databases
For Loop
$artists=(“Alice Neel”, “Robert Rauschenberg “, “Kiki Smith”, “Dan Flavin”, “Giotto”);
The FOR Loop// print them all out, using the FOR loopprint("<p><b>Using the FOR loop</b>");
for($i=0; $i<$total_artists; $i++){print("<br> $artists[$i]");
}
In arrays_numeric.php use the FOR loop to print out all the values
Note that we are keeping track of 3 variables here
$artist is the array holding the artists names
$total_artist holds the number of items in our array
$i is used as a counter in the FOR loop.We initialize it at 0We set the condition that it must be less than $total artistAnd we increment 1 each time it goes through the loop
Class 3Intro to Databases
ForEach Loop
$artists=(“Alice Neel”, “Robert Rauschenberg “, “Kiki Smith”, “Dan Flavin”, “Giotto”);
The FOREACH Loop// print them all out, using the FOREACH loopprint("<p><b>Using the FOREACH loop</b>");
foreach($artists as $value){print("<br> $value");
}
In arrays_numeric.php use the FOREACH loop to print out all the values
Class 3Intro to Databases
ForEach Loop
$artists=(“Alice Neel”, “Robert Rauschenberg “, “Kiki Smith”, “Dan Flavin”, “Giotto”);
To print out all values// printing another wayecho "<p>Another way to print the contents";
// print each artist as $valueforeach($artists as $value){
print("<br> $value");}
In arrays_numeric.php use the FOREACH loop to print out all the values
Here we can access each element of a for loop using FOREACH
Class 3Intro to Databases
Indexed Arrays
An indexed array is a list of items. Each item is made up of a key/value pair.
The key is how you access the value.
$artists=array('oil paint'=>'Alice Neel', 'mixed media'=>'Robert Rauschenberg', 'bronze'=>'Kiki Smith', 'fluorescent lights'=>'Dan Flavin');
Class 3Intro to Databases
Indexed Arrays
To create an array$artists=array();
To put values in an array
$artists=array('oil paint'=>'Alice Neel', 'mixed media'=>'Robert
Rauschenberg', 'bronze'=>'Kiki Smith', 'fluorescent
lights'=>'Dan Flavin');
or
$artists[‘fresco’]= “Giotto”;
Class 3Intro to Databases
Indexed Arrays
$artists=array('oil paint'=>'Alice Neel', 'mixed media'=>'Robert Rauschenberg', 'bronze'=>'Kiki Smith', 'fluorescent lights'=>'Dan Flavin');
To access values
To access values in indexed arrays, we use the key
$this_artist= $artists[‘oil paint’];
echo "My favorite oil painter is ".$this_artist; //prints Alice Neel
Download arrays_indexed.zip
Change arrays_indexed.php to print out an artists name
Note that arays start at ZERO
Class 3Intro to Databases
Numerical Arrays
To add values$artists[‘fresco’]= “Giotto”;
Will add a value with this key
Gives us$artists=array('oil paint'=>'Alice Neel', 'mixed media'=>'Robert Rauschenberg', 'bronze'=>'Kiki Smith ', 'fluorescent lights'=>'Dan Flavin ', ‘fresco'=>'Dan Flavin ');
In arrays_indexed.php
Add an artist’s name the array
And print it out
$artists[‘fresco’]= “Giotto”; echo "<br>My favorite fresco painter is".$this_artist;
//prints Giotto
Class 3Intro to Databases
ForEach Loop
$artists=array('oil paint'=>'Alice Neel', 'mixed media'=>'Robert Rauschenberg', 'bronze'=>'Kiki Smith ', 'fluorescent lights'=>'Dan Flavin ', ‘fresco'=>'Dan Flavin ');
The FOREACH Loop// print them all out, using the FOREACH loopecho "<p><b>In original order</b>";
foreach($artists as $key=>$value){print("<br> <i> $key </i> $value");
}
In arrays_indexed.php use the FOREACH loop to print out all the keys and values
Class 3Intro to Databases
Sorting by Value
$artists=array('oil paint'=>'Alice Neel', 'mixed media'=>'Robert Rauschenberg', 'bronze'=>'Kiki Smith ', 'fluorescent lights'=>'Dan Flavin ', ‘fresco'=>'Dan Flavin ');
ASORT// print them all out, sorted by valueecho "<p><b>Sorted by artist name</b>";asort($artists);
foreach($artists as $key=>$value){print("<br> <i> $key </i> $value");
}
In arrays_indexed.php use the ASORT to print out all the keys and values
Class 3Intro to Databases
Sorting by Key
$artists=array('oil paint'=>'Alice Neel', 'mixed media'=>'Robert Rauschenberg', 'bronze'=>'Kiki Smith ', 'fluorescent lights'=>'Dan Flavin ', ‘fresco'=>'Dan Flavin ');
KSORT // print them all out, sorted by key echo "<p><b>Sorted by medium</b>";ksort($artists);
foreach($artists as $key=>$value){print("<br> <i> $key </i> $value");
}
In arrays_indexed.php use the ASORT to print out all the keys and values
Class 3Intro to Databases
Sending Values to a Script Manually
Here we’re going to look at how to send hidden variables in a form.
Class 3Intro to Databases
Sending Values to a Script Manually
Download and unzip cl_3_manual.zip
Class 3Intro to Databases
Sending Values to a Script Manually
HTML (calculator.html)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/2000/REC-xhtml1-20000126/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" /><title>Cost Calculator</title>
</head><body>
<!-- Script 2.6 - calculator.html -->
<form action="handle_calculator.php?source=calculator.html" method="post">
<select name="quantity"><option value="">Select a quantity:</option><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5">5</option><option value="6">6</option>
</select>
<div align="left"><input type="submit" name="submit" value="Total!" /></div>
<input type="hidden" name="price" value="19.95" /><input type="hidden" name="taxrate" value=".05" />
</form><!-- End of Form -->
</body></html>
Class 3Intro to Databases
Sending Values to a Script Manually
HTML
<form action="handle_calculator.php?source=calculator.html" method="post">
Here we are sending both POST and GET variables
When a variable is sent in a url string it is a GET variable
When variables are sent through a form they can be either GET or POST variables, depending on the form METHOD
2 IMPORTANT ARRAYS
POST variables can be accessed using the superglobal $_POST
GET variables can be accessed using the superglobal $_GET
Class 3Intro to Databases
Sending Values to a Script Manually
PHP (handle_calculator.php)<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN“ "http://www.w3.org/TR/2000/REC-xhtml1-20000126/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" /><title>Cost Calculator</title>
</head><body><?php# Script 2.8 - handle_calculator.php
if (isset($_GET['source'])) {
if ($_GET['source'] == 'calculator.html') { // Are they coming from the right page?
if (is_numeric($_POST['quantity'])) { // Is the quantity a number?
// Make the calculations and print the results.$total = ($_POST['quantity'] * $_POST['price']) * ($_POST['taxrate'] + 1);$total = number_format ($total, 2);echo "You are purchasing <b>{$_POST['quantity']}</b> widget(s) at a cost of
<b>\${$_POST['price']}</b> each. With tax, the total comes to <b>\$$total</b>.\n";
} else { // Quantity is not a number.echo '<p><b>Please enter a valid quantity to purchase!</b></p>';
}
} else { // The source is not right.echo '<p><b>You have accessed this page inappropriately!</b></p>';
}
} else { // The source is not set.echo '<p><b>You have accessed this page inappropriately!</b></p>';
}
?></body></html>
Class 3Intro to Databases
Sending Values to a Script Manually
Upload calculator.html and handle_calculator.php
Change the price & tax rate