Download presentation
Presentation is loading. Please wait.
Published byThomas Fletcher Modified over 9 years ago
1
Class 3Intro to Databases Arrays Sending Values to a Script Manually For and While Loops
2
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
3
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
4
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
5
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 " or maybe it's ".$this_artist; //prints Giotto
6
Class 3Intro to Databases Getting the number of elements in an array / /get the count of an array $total_artists=count($artists); echo " There are ".$total_artists." artists in my list"; In arrays_numeric.php print out the number of elements in your array
7
Class 3Intro to Databases While Loop $artists=(“Alice Neel”, “Robert Rauschenberg “, “Kiki Smith”, “Dan Flavin”, “Giotto”); The While Loop //using the WHILE loop print(" Using the WHILE loop "); $i=0; while($i $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
8
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 loop print(" Using the FOR loop "); for($i=0; $i $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
9
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 loop print(" Using the FOREACH loop "); foreach($artists as $value){ print(" $value"); } In arrays_numeric.php use the FOREACH loop to print out all the values
10
Class 3Intro to Databases ForEach Loop $artists=(“Alice Neel”, “Robert Rauschenberg “, “Kiki Smith”, “Dan Flavin”, “Giotto”); To print out all values // printing another way echo " Another way to print the contents"; // print each artist as $value foreach($artists as $value){ print(" $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
11
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');
12
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”;
13
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
14
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 " My favorite fresco painter is".$this_artist; //prints Giotto
15
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 loop echo " In original order "; foreach($artists as $key=>$value){ print(" $key $value"); } In arrays_indexed.php use the FOREACH loop to print out all the keys and values
16
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 value echo " Sorted by artist name "; asort($artists); foreach($artists as $key=>$value){ print(" $key $value"); } In arrays_indexed.php use the ASORT to print out all the keys and values
17
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 " Sorted by medium "; ksort($artists); foreach($artists as $key=>$value){ print(" $key $value"); } In arrays_indexed.php use the ASORT to print out all the keys and values
18
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.
19
Class 3Intro to Databases Sending Values to a Script Manually Download and unzip cl_3_manual.zip
20
Class 3Intro to Databases Sending Values to a Script Manually HTML (calculator.html) Cost Calculator Select a quantity: 1 2 3 4 5 6
21
Class 3Intro to Databases Sending Values to a Script Manually HTML 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
22
Class 3Intro to Databases Sending Values to a Script Manually PHP (handle_calculator.php) Cost Calculator {$_POST['quantity']} widget(s) at a cost of \${$_POST['price']} each. With tax, the total comes to \$$total.\n"; } else { // Quantity is not a number. echo ' Please enter a valid quantity to purchase! '; } } else { // The source is not right. echo ' You have accessed this page inappropriately! '; } } else { // The source is not set. echo ' You have accessed this page inappropriately! '; } ?>
23
Class 3Intro to Databases Sending Values to a Script Manually Upload calculator.html and handle_calculator.php Change the price & tax rate
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.