Transcript Document

ITC 240: Web Application
Programming
S UBHA S H P R A JA PATI
0 4 / 21/15
Review
• Code Organization with PHP
• Include/ require
• Include_once/ require_once
Code Organization with PHP
Last Week’s Excercise
Index/ contact us page from last week’s exercise should look like below:
<?php include ‘header.php’ ?>
<?php include ‘leftNav.php’; ?>
<div class=‘container’>
The page content goes here.
</div>
<?php include ‘footer.php’; ?>
Today
• PHP Array
Variables
• Imagine we need to 5 names in a program. We can probably use variables like below:
$name1 = “Joe”;
$name2 = “John”;
$name3 = “Lisa”;
$name4 = “Brian”;
$name5 = “Tessa”;
What if we need to use for 25 people? Or 50 movies? Or 100 books?
Array
• Variable that can hold more than one value at a time.
• Useful when we want to store a group of (similar) data.
• Can be expanded
• Flexible
Array
Array declaration
$myArray = array ();
Initialize the values:
$myArray = array (‘cat’, ‘dog’, ‘monkey’);
Now Try this:
echo $myArray;
Finding the values in an array
var_dump ($myArray);
Array Structure
var_dump ($myArray) will give following:
array(3) { [0]=> string(3) "cat" [1]=> string(3) "dog" [2]=> string(6) "monkey" }
(print_r() can also be used)
The values are stored with number indexed (starting from 0).
index can be assigned manually:
$drink[0] = ‘coffee’;
$drink[1] = ‘tea’;
$drink[2] = ‘latte’;
Quiz
• Suppose we have an array:
$courses = array (‘english’, ‘math’, ‘physics’, ‘chemistry’);
what will be the value of $courses [2] ?
what will count ($courses) return?
Class Exercise 5.1
• Suppose we have an array: $colors = array (‘red’, ‘blue’, ‘green’). Print following:
• Blue
• Gren
• Red
• Now add another element to this array. Verify this with count ($colors), which should return 4.
Array Types
• Indexed arrays (one we just looked at)
• Associative arrays
• Multidimensional arrays
Associative Array
• Storing the array with named index
$fruit [‘apple’] = 55;
$fruit [‘banana’] = 23;
$fruit [‘mango’] = 45;
Or we can also write:
$fruit = array (‘apple’ => 55, ‘banana’=>23, ‘mango’=>45);
Class Exercise 5.2
• Go to our “Everything Seattle” exercise we did last week. Go to the menu and create the
header menu from the associative array.
For eg. $navMenu[“home”] = “Home Page” … etc.
Array Functions
• count () – gives array size
• sort() - sort arrays in ascending order
• rsort() - sort arrays in descending order
• asort() - sort associative arrays in ascending order, according to the value
• ksort() - sort associative arrays in ascending order, according to the key
• in_array ($element, $array) – checks if element is in given array
• array_reverse () – reverses the array elements
Check PHP manual for complete list
Class Exercise 5.3
• Suppose we have an array $myNumbers = array (5,2,7,1,8,9,24,56,3);
Find the smallest and the largest number in an array
Multi Dimensional Array
Array can contain another array as an:
$person = array (‘name’ => ‘Tina’, ‘age’=> 25, ‘address’ => array (‘Seattle’, ‘Lynnwood’));
check the array structure:
echo '<pre>';
print_r ($person);
echo '</pre>';
Multi Dimensional Array
Multi-Dimensional Array example with numbered index:
$flowers = array( array("rose", 6.5 , 10),
array("daisy", 3.75 , 20),
array("orchid", 8.25 , 34)
);
echo "You can get ". $flowers[0][0]." for US$".$flowers[0][1]." and there are ". $flowers[0][2]."
flowers available now.";
Multi Dimensional Array
We can think 2 dimensional array as matrix. A matrix can be thought of as a grid of numbers, arranged
in rows and columns. We might write the two-dimensional array out as follows to illustrate this point:
$myMatrix = array (
array (1,2,3,4),
array (5,6,7,8),
array (9,10,11,12),
array (13,14,15,16)
);
echo $myMatrix[2][3]; // what will this give?
unset()/isset()
These are two PHP built-in functions to do following:
Isset() - check whether a variable is set or not (od )
unset() – destroys the variable
We can destroy/delete the array element like this:
unset ($myArray[2])
Discussion/ Exercise
From our “Everything Seattle” Exercise, how can we create the side nav with multi-dimentional
array?