230 likes | 361 Views
Arrays Sending Values to a Script Manually For and While Loops. 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.
E N D
Arrays Sending Values to a Script Manually For and While Loops
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
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
NumericalArrays $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
NumericalArrays • 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
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
While Loop $artists=(“Alice Neel”, “Robert Rauschenberg “, “Kiki Smith”, “Dan Flavin”, “Giotto”); The While Loop //using the WHILE loop print("<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 0 We set the condition that it must be less than $total artist And we increment 1 each time it goes through the loop
For Loop $artists=(“Alice Neel”, “Robert Rauschenberg “, “Kiki Smith”, “Dan Flavin”, “Giotto”); The FOR Loop // print them all out, using the FOR loop print("<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 0 We set the condition that it must be less than $total artist And we increment 1 each time it goes through the loop
ForEach Loop $artists=(“Alice Neel”, “Robert Rauschenberg “, “Kiki Smith”, “Dan Flavin”, “Giotto”); The FOREACH Loop // print them all out, using the FOREACH loop print("<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
ForEach Loop $artists=(“Alice Neel”, “Robert Rauschenberg “, “Kiki Smith”, “Dan Flavin”, “Giotto”); To print out all values // printing another way echo "<p>Another way to print the contents"; // print each artist as $value foreach($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
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');
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”;
IndexedArrays • $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.phpto print out an artists name Note that arays start at ZERO
NumericalArrays • 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
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 loop • echo "<p><b>In original order</b>"; • foreach($artists as $key=>$value){ print("<br> <i> $key </i> $value"); • } • In arrays_indexed.phpuse the FOREACH loop to print out all the keys and values
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 value • echo "<p><b>Sorted by artist name</b>"; asort($artists); • foreach($artists as $key=>$value){ print("<br> <i> $key </i> $value"); • } • In arrays_indexed.phpuse the ASORT to print out all the keys and values
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.phpuse the ASORT to print out all the keys and values
Sending Values to a Script Manually Here we’re going to look at how to send hidden variables in a form.
Sending Values to a Script Manually Download and unzip cl_3_manual.zip
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>
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
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.phpif (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>
Sending Values to a Script Manually Upload calculator.html and handle_calculator.php Change the price & tax rate