Class 3Intro to Databases Arrays Sending Values to a Script Manually For and While Loops.

Slides:



Advertisements
Similar presentations
» PHP arrays are lists of values stored in key-value pairs. » Uses of arrays: Many built-in PHP environment variables. Database functions use arrays.
Advertisements

LIS651 lecture 1 arrays functions & sessions Thomas Krichel
Outline lecture Revise arrays Entering into an array
PHP I.
CHAPTER 10 COMMON PROGRAMMING TECHNIQUES. SENDING VALUES TO A SCRIPT Hidden form input type: Depending on the method, $_POST['do'] or $_GET['do'] will.
 PHP's name is an recursive acronym for:  Hypertext Preprocessor  PHP script is an HTML- embedded scriting language  Designed to do something only.
 HTTP Communication  HTTP Communication for Web  Static  Dynamic  Dynamic Content  PHP  PHP Block  PHP Programming Style  Database  MySQL.
Objectives Using functions to organize PHP code
1 PHP Statement Constructs Server Scripting. 5-2 Basic Statement All Statements end in a semicolon. Statements are delimited from the HTML code by enclosing.
Functions & Arrays. Functions Functions offer the ability for programmers to group together program code that performs specific task or function into.
PHP Functions and Control Structures. 2 Defining Functions Functions are groups of statements that you can execute as a single unit Function definitions.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14 Web Database Programming Using PHP.
Outline IS400: Development of Business Applications on the Internet Fall 2004 Instructor: Dr. Boris Jukic PHP.
MS3304: Week 4 PHP & HTML Forms. Overview HTML Forms elements refresher Sending data to a script via an HTML form –The post vs. get methods –Name value.
PHP MOHAMMED SHURRAB TO MISS/ RASHA ATTALLAH. What is PHP? Stands for "PHP Hypertext Preprocessor" Server-side scripting language HTML-embedded Supports.
Introduction to PHP and Server Side Technology. Slide 2 PHP History Created in 1995 PHP 5.0 is the current version It’s been around since 2004.
Using Arrays in PHP (Part I) Lecture # 6. What is an array? It is a collection of multiple values assembled into one variable An array can consist of.
(c) Manzur Ashraf, Short course, KFUPM PHP & MySQL 1 Basic PHP Class 2.
INTERNET APPLICATION DEVELOPMENT For More visit:
Lecture 6 – Form processing (Part 1) SFDV3011 – Advanced Web Development 1.
Lecture 7 – Form processing (Part 2) SFDV3011 – Advanced Web Development 1.
Decisions, Loops, and Arrays Achmad Arwan, S.Kom.
1 PHP and MySQL. 2 Topics  Querying Data with PHP  User-Driven Querying  Writing Data with PHP and MySQL PHP and MySQL.
Copyright © 2003 Pearson Education, Inc. Slide 5-1 The Web Wizard’s Guide to PHP by David Lash.
SHOPPING CARTS CHAPTER 19. E-COMMERCE Typically, an e-commerce site will have public pages and admin pages.
Mr. Justin “JET” Turner CSCI 3000 – Fall 2015 CRN Section A – TR 9:30-10:45 CRN – Section B – TR 5:30-6:45.
Week 7. Lecture 2 Functions, Arrays, PHP&MySQL. Function with More than one argument and a return statement For a function to return a value, the return.
Introduction to PHP “PHP is a server-side scripting language designed specifically for the Web. Within an HTML page, you can embed PHP code that will be.
Lecture 8 – Cookies & Sessions SFDV3011 – Advanced Web Development 1.
ITM © Port, Kazman1 ITM 352 More on Forms Processing.
PHP Programming with MySQL Slide 4-1 CHAPTER 4 Functions and Control Structures.
1 CS428 Web Engineering Lecture 20 Control Structures, Loops and Pointers File Uploading Function (PHP - III)
Class 2Intro to Databases Goals of this class Include & Require in PHP Generating Random Numbers in PHP Arrays – Numerically Indexed and Associative Program.
CSC 2720 Building Web Applications Server-side Scripting with PHP.
Intro to PHP Carl-Erik Svensson. What is PHP? PHP is a widely-used general-purpose scripting language that is especially suited for Web development and.
1 PHP Intro PHP Arrays After this lecture, you should be able to: Create and manipulate PHP Arrays: Create and manipulate PHP Arrays: Indexed Arrays Indexed.
Arrays. Indexed Arrays Use numbers as keys: $grades = array(); $grades[0] = ' A ' ; $grades[1] = ' A- ' ; $grades[2] = ' B+ ' ; Or with one statement:
Just a Little PHP Programming PHP on the Server. Common Programming Language Features Comments Data Types Variable Declarations Expressions Flow of Control.
PHP Constructs Advance Database Management Systems Lab no.3.
Week 4 PHP H ypertext P reprocessor Reference : Official Site :
PHP. $_GET / $_POST / $_SESSION PHP uses predefined variables to provide access to important information about the server and requests from a browser.
Slide 1 PHP Arrays and User Defined Functions ITWA133.
Server-Side Scripting with PHP ISYS 475. PHP Manual Website
Introduction to PHP.
ITM © Port, Kazman1 ITM 352 More on Forms Processing.
Forms Overview, Query string, Submitting arrays, PHP & HTML, Input types, Redirecting the user Mario Peshev Technical Trainer Software.
Creating Databases for Web applications Server side vs client side PHP basics Homework: Get your own versions of sending working: both html and Flash!
CHAPTER 7 Introduction to PHP5 Part II อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา 1.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
ITM © Port, Kazman1 ITM 352 Simple Arrays. ITM © Port, Kazman2 Arrays r Collections r Array basics m Declaration, allocation, and initialization.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14 Web Database Programming Using PHP.
 An array stores multiple values in one single variable.  Example: Output: I like Honda Civic, BMW and Toyota.
Dr. Abdullah Almutairi Spring PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages. PHP is a widely-used,
Creating PHP Pages Chapter 10 PHP Arrays. Arrays An array can store one or more values in a single variable name. An element of an associative accessed.
PHP Overview. What is PHP Widely available scripting language Free Alternative to Microsoft’s ASP Runs on the Web Server; not in the browser Example:
Unit – 3 Control structures. Condition Statements 1.If.…..else :- Has someone ever told you, "if you work hard, then you will succeed"? And what happens.
11 – Introduction to PHP(1) Informatics Department Parahyangan Catholic University.
PHP Tutorial. What is PHP PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages.
CGS 3066: Web Programming and Design Spring 2016 PHP.
1 PHP Array PHP Arrays An indexed array is similar to one provided by a conventional programming language. An indexed array is similar to one provided.
PHP WORKSHOP (Session 1) INFO 257 Supplement. Outline What is PHP ? PHP Language Basics PHP Exercise.
1 Server Side scripting PHP. 2 What is PHP? PHP stands for PHP: Hypertext Preprocessor PHP is a server-side scripting language, like ASP PHP scripts are.
PHP using MySQL Database for Web Development (part II)
PHP (Session 1) INFO 257 Supplement.
Example of a table. Note that I looked at an example under CIS44.
Arrays: Checkboxes and Textareas
Arrays in PHP are quite versatile
PHP PART 2.
PHP an introduction.
Presentation 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 " 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 " 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 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

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

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

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

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

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

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

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) Cost Calculator Select a quantity:

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

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! '; } ?>

Class 3Intro to Databases Sending Values to a Script Manually Upload calculator.html and handle_calculator.php Change the price & tax rate