1 / 23

Arrays Sending Values to a Script Manually For and While Loops

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.

allene
Télécharger la présentation

Arrays Sending Values to a Script Manually For and While Loops

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Arrays Sending Values to a Script Manually For and While Loops

  2. 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. 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. 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

  5. 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

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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. 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. 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

  14. 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

  15. 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

  16. 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

  17. 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

  18. Sending Values to a Script Manually Here we’re going to look at how to send hidden variables in a form.

  19. Sending Values to a Script Manually Download and unzip cl_3_manual.zip

  20. 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>

  21. 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

  22. 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>

  23. Sending Values to a Script Manually Upload calculator.html and handle_calculator.php Change the price & tax rate

More Related