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.

Slides:



Advertisements
Similar presentations
XHTML Basics. What is XHTML? XHTML is newer than the old HTML XHTML has stricter rules and does not allow some elements formerly used in HTML One benefit.
Advertisements

Lecture 6/2/12. Forms and PHP The PHP $_GET and $_POST variables are used to retrieve information from forms, like user input When dealing with HTML forms.
Tutorial 6 Creating a Web Form
Objectives Using functions to organize PHP code
PHP Functions and Control Structures. 2 Defining Functions Functions are groups of statements that you can execute as a single unit Function definitions.
Outline IS400: Development of Business Applications on the Internet Fall 2004 Instructor: Dr. Boris Jukic PHP.
Tutorial 12 Working with Arrays, Loops, and Conditional Statements
Chapter 4 Functions and Control Structures PHP Programming with MySQL.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, © Copyright 2010 All Rights Reserved. 1.
August Chapter 1 - Essential PHP spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information Science and Technology.
PHP – Get & Post; Functions; and Arrays IS6116 – 07 th February 2011.
Chapter 4 Handling User Input PHP Programming with MySQL 2nd Edition
Practical PHP IDIA Spring 2012 Bridget M. Blodgett.
INTERNET APPLICATION DEVELOPMENT For More visit:
Class 3Intro to Databases Arrays Sending Values to a Script Manually For and While Loops.
The foreach LooptMyn1 The foreach Loop The foreach loop gives an easy way to iterate over arrays. foreach works only on arrays, and will issue an error.
2010/11 : [1]Building Web Applications using MySQL and PHP (W1)PHP Recap.
PHP and MySQL for Client-Server Database Interaction Chapter 10.
School of Computing and Information Systems CS 371 Web Application Programming PHP - Basics Serving up web pages.
Arrays – What is it? – Creation – Changing the contents Functions – What is it? – Syntax – How they work – Anonymous functions A quick lesson in Objects.
Nael Alian Introduction to PHP
XHTML Instructor: Charles Moen CSCI/CINF XHTML  A stricter version of HTML  Extensible HTML  The XHTML specification is maintained by the World.
>> Introduction to HTML: Tags. Hyper - is the opposite of linear Text – words / sentences / paragraphs Mark-up – Marking the text Language – It is a language.
PHP meets MySQL.
1 Web Development Lecture # 11 Introduction to XHTML (Chapter # 4) It.GulGasht.Com.
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.
Open Source Software Unit – 3 Presented By Mr. R.Aravindhan.
Introduction to PHP – Part 2 Sudeshna Dey. Arrays A series of homogeneous elements Elements have values in form of stored data Has a key associated with.
PHP Programming with MySQL Slide 4-1 CHAPTER 4 Functions and Control Structures.
Variables and ConstantstMyn1 Variables and Constants PHP stands for: ”PHP: Hypertext Preprocessor”, and it is a server-side programming language. Special.
PHP. What is PHP? PHP stands for PHP: Hypertext Preprocessor PHP is a server-side scripting language, like ASP PHP scripts are executed on the server.
Chapter 2 Functions and Control Structures PHP Programming with MySQL 2 nd Edition.
Arrays. Indexed Arrays Use numbers as keys: $grades = array(); $grades[0] = ' A ' ; $grades[1] = ' A- ' ; $grades[2] = ' B+ ' ; Or with one statement:
Introduction to PHP Advanced Database System Lab no.1.
PHP Open source language for server-side scripting Works well with many databases (e.g., MySQL) Files end in.php,.php3 or.phtml Runs on all major platforms.
3 1 Sending Data Using an Online Form CGI/Perl Programming By Diane Zak.
PHP Constructs Advance Database Management Systems Lab no.3.
Week 4 PHP H ypertext P reprocessor Reference : Official Site :
5 1 Data Files CGI/Perl Programming By Diane Zak.
Server-Side Scripting with PHP ISYS 475. PHP Manual Website
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, © Copyright 2015, Fred McClurg, All Rights.
Control Structures. The if Conditional Condition must go within parentheses Statements come after the curly brace Example: if (empty ($name)) { print.
הרצאה 4. עיבוד של דף אינטרנט דינמי מתוך Murach’s PHP and MySQL by Joel Murach and Ray Harris.  דף אינטרנט דינמי משתנה עפ " י הרצת קוד על השרת, יכול להשתנות.
Dynamic Web Authoring JavaScript – Looping statements COM311H Zheng, School of C&M, UUJ1.
Chapter 1 Introduction to PHP Part 1. Textbook’s Code DOWNLOADS PHP and MySQL for Dynamic Web Sites Complete Set of Scripts.
THE TEXT ELEMENTS. THE HEADING ELEMENTS,,,, and - introduce new section of content as a title or a header. Heading sizes range from H1 to H6, where H1.
Chapter 8 Error Handling and Debugging. Debugging “When you get frustrated, step away from the computer!”
Chapter 16 Web Pages And CGI Scripts Department of Biomedical Informatics University of Pittsburgh School of Medicine
 An array stores multiple values in one single variable.  Example: Output: I like Honda Civic, BMW and Toyota.
Introduction to PHP 1.What is PHP? What Is PHP?  php: hypertext preprocessor  Server-side scripting language—like ASP—scripts are executed on server.
Arrays Strings and regular expressions Basic PHP Syntax CS380 1.
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,
INTERNET APPLICATIONS CPIT405 Forms, Internal links, meta tags, search engine friendly websites.
Tutorial 6 Creating a Web Form
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.
XHTML Basics. What is XHTML? XHTML is newer than, but built upon, the original HTML (Hyper Text Markup Language) platform. XHTML has stricter rules and.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - JavaScript: Arrays Outline 11.1 Introduction 11.2 Arrays 11.3 Declaring and Allocating Arrays.
 2001 Prentice Hall, Inc. All rights reserved. Outline 1 JavaScript.
PHP using MySQL Database for Web Development (part II)
Session 2 Basics of PHP.
Introduction to PHP Part 1
PHP Introduction.
While Loops BIS1523 – Lecture 12.
Web Systems Development (CSC-215)
Web Systems Development (CSC-215)
HYPERTEXT PREPROCESSOR BY : UMA KAKKAR
Tutorial 6 PHP & MySQL Li Xu
PHP PART 2.
PHP an introduction.
Presentation transcript:

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 numbers and/or strings The array’s index can be numbers or strings You can use the superglobals $_POST or $_GET. Its indexes are the names of the form inputs. Therefore, $_POST[‘first_name’] refers to the value typed in a form input created by this code

Syntax Rules for naming arrays Begin with dollar sign Continue with a letter or underscore except superglobals which must start with an underscore after the $.

Creating an Array Use the array () function to create an array Example: $list = array (‘apples’, ‘bananas’, ‘oranges’); The first element in the previous example will be automatically indexed at 0 unless otherwise indicated. Example for specifying an index: $list = array (1 => ‘apples’, ‘bananas’, ‘oranges’); You can use the range function to create an array of items based on a range of values. Example: $ten = range (1, 10); // no need for single quotes for numbers $alpha = range (‘a’, ‘z’);

Code Example Script 7.1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " No Soup for You! Mmmmm...soups: <?php // Script soups1.php // Address error handing. ini_set ('display_errors', 1); error_reporting (E_ALL & ~E_NOTICE); // Create the array. $soups = array ( 'Monday' => 'Clam Chowder', 'Tuesday' => 'White Chicken Chili', 'Wednesday' => 'Vegetarian' ); // Try to print the array. print " $soups "; // Print the contents of the array. print_r ($soups); ?>

Adding items to an array If you do not specify an index for the new element you want to add then it will be automatically added after the last element in the array. If you use an index that already exists in the array the new element will override the old element in the array.

Code example Script 7.2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " No Soup for You! Mmmmm...soups: <?php // Script soups2.php // Address error handing. ini_set ('display_errors', 1); error_reporting (E_ALL & ~E_NOTICE); // Create the array. $soups = array ( 'Monday' => 'Clam Chowder', 'Tuesday' => 'White Chicken Chili', 'Wednesday' => 'Vegetarian' );

continued // Count and print the current number of elements. $number1 = count ($soups); print " The soups array originally had $number1 elements. "; // Add three items to the array. $soups['Thursday'] = 'Chicken Noodle'; $soups['Friday'] = 'Tomato'; $soups['Saturday'] = 'Cream of Broccoli'; // Count and print the number of elements again. $number2 = count ($soups); print " After adding 3 more soups, the array now has $number2 elements. "; print_r($soups); ?>

Creating Multidimensional Arrays Script 7.4 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " My Books and Chapters <?php // Script books.php // Address error handing. ini_set ('display_errors', 1); error_reporting (E_ALL & ~E_NOTICE); // Create the first array. $phpvqs = array (1 => 'Getting Started', 'Variables', 'HTML Forms and PHP', 'Using Numbers'); // Create the second array. $phpadv = array (1 => 'Advanced PHP Programming', 'Object-Oriented Programming', 'Databases', 'Security'); // Create the third array. $phpmysql = array (1 => 'Introduction to PHP', 'Programming with PHP', 'Creating Dynamic Web Sites', 'Introduction to SQL and MySQL'); // Create the multidimensional array. $books = array ( 'PHP VQS' => $phpvqs, 'PHP Advanced VQP' => $phpadv, 'PHP and MySQL VQP' => $phpmysql );

continued // Print out some values. print " The third chapter of my first book is {$books['PHP VQS'][3]}. "; print " The first chapter of my second book is {$books['PHP Advanced VQP'][1]}. "; print " The fourth chapter of my third book is {$books['PHP and MySQL VQP'][4]}. "; // See what happens with foreach. foreach ($books as $key => $value) { print " $key: $value \n"; }

Nested foreach Script 7.4 modified <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " My Books and Chapters <?php // Script books.php // Address error handing. ini_set ('display_errors', 1); error_reporting (E_ALL & ~E_NOTICE); // Create the first array. $phpvqs = array (1 => 'Getting Started', 'Variables', 'HTML Forms and PHP', 'Using Numbers'); // Create the second array. $phpadv = array (1 => 'Advanced PHP Programming', 'Object-Oriented Programming', 'Databases', 'Security'); // Create the third array. $phpmysql = array (1 => 'Introduction to PHP', 'Programming with PHP', 'Creating Dynamic Web Sites', 'Introduction to SQL and MySQL');

continued // Create the multidimensional array. $books = array ( 'PHP VQS' => $phpvqs, 'PHP Advanced VQP' => $phpadv, 'PHP and MySQL VQP' => $phpmysql ); // See what happens with nested foreach. foreach ($books as $title => $chapters) { print " $title"; foreach ($chapters as $number => $chapter) { print " Chapter $number is $chapter"; } ?>