lecture 20 robb t. koether - hampden-sydney...

31
PHP Arrays Lecture 20 Robb T. Koether Hampden-Sydney College Fri, Feb 28, 2014 Robb T. Koether (Hampden-Sydney College) PHP Arrays Fri, Feb 28, 2014 1 / 31

Upload: others

Post on 29-Jun-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture 20 Robb T. Koether - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · 1 PHP Arrays 2 Iteration Structures 3 Displaying Arrays 4 Associative

PHP ArraysLecture 20

Robb T. Koether

Hampden-Sydney College

Fri, Feb 28, 2014

Robb T. Koether (Hampden-Sydney College) PHP Arrays Fri, Feb 28, 2014 1 / 31

Page 2: Lecture 20 Robb T. Koether - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · 1 PHP Arrays 2 Iteration Structures 3 Displaying Arrays 4 Associative

1 PHP Arrays

2 Iteration Structures

3 Displaying Arrays

4 Associative Arrays

5 Connecting to a Database

6 Assignment

Robb T. Koether (Hampden-Sydney College) PHP Arrays Fri, Feb 28, 2014 2 / 31

Page 3: Lecture 20 Robb T. Koether - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · 1 PHP Arrays 2 Iteration Structures 3 Displaying Arrays 4 Associative

Outline

1 PHP Arrays

2 Iteration Structures

3 Displaying Arrays

4 Associative Arrays

5 Connecting to a Database

6 Assignment

Robb T. Koether (Hampden-Sydney College) PHP Arrays Fri, Feb 28, 2014 3 / 31

Page 4: Lecture 20 Robb T. Koether - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · 1 PHP Arrays 2 Iteration Structures 3 Displaying Arrays 4 Associative

PHP Arrays

Initializing an Array$a = array(5, 10, 15, 20);$b[0] = 5;$b[1] = 10;$b[2] = 15;$b[3] = 20;

Arrays may be initialized by using the keyword array.Or, the array elements may be assigned individually without usingthe keyword.

Robb T. Koether (Hampden-Sydney College) PHP Arrays Fri, Feb 28, 2014 4 / 31

Page 5: Lecture 20 Robb T. Koether - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · 1 PHP Arrays 2 Iteration Structures 3 Displaying Arrays 4 Associative

Outline

1 PHP Arrays

2 Iteration Structures

3 Displaying Arrays

4 Associative Arrays

5 Connecting to a Database

6 Assignment

Robb T. Koether (Hampden-Sydney College) PHP Arrays Fri, Feb 28, 2014 5 / 31

Page 6: Lecture 20 Robb T. Koether - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · 1 PHP Arrays 2 Iteration Structures 3 Displaying Arrays 4 Associative

while Statements

The while Statementwhile (condition)

while-block;

while statements in PHP are just like while statements in C.

Robb T. Koether (Hampden-Sydney College) PHP Arrays Fri, Feb 28, 2014 6 / 31

Page 7: Lecture 20 Robb T. Koether - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · 1 PHP Arrays 2 Iteration Structures 3 Displaying Arrays 4 Associative

for Statements

The for Statementfor (initial; condition; update)

for-block;

for statements in PHP are just like for statements in C.

Robb T. Koether (Hampden-Sydney College) PHP Arrays Fri, Feb 28, 2014 7 / 31

Page 8: Lecture 20 Robb T. Koether - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · 1 PHP Arrays 2 Iteration Structures 3 Displaying Arrays 4 Associative

foreach Statements

The foreach Statementforeach (array_name as var_name)

Process var_name

The foreach statement will process an array’s elements.

Robb T. Koether (Hampden-Sydney College) PHP Arrays Fri, Feb 28, 2014 8 / 31

Page 9: Lecture 20 Robb T. Koether - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · 1 PHP Arrays 2 Iteration Structures 3 Displaying Arrays 4 Associative

foreach Statements

The foreach Statementforeach (array_name as index_name => var_name)

Process index_name and var_name

The foreach statement will also process an array’s elements andtheir indexes.

Robb T. Koether (Hampden-Sydney College) PHP Arrays Fri, Feb 28, 2014 9 / 31

Page 10: Lecture 20 Robb T. Koether - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · 1 PHP Arrays 2 Iteration Structures 3 Displaying Arrays 4 Associative

Outline

1 PHP Arrays

2 Iteration Structures

3 Displaying Arrays

4 Associative Arrays

5 Connecting to a Database

6 Assignment

Robb T. Koether (Hampden-Sydney College) PHP Arrays Fri, Feb 28, 2014 10 / 31

Page 11: Lecture 20 Robb T. Koether - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · 1 PHP Arrays 2 Iteration Structures 3 Displaying Arrays 4 Associative

Displaying Arrays

Displaying an Array$a = array(5, 10, 15, 20);$i = 0;while ($i < count($a)){

echo $a[$i] . ’ ’;$i++;

}

produces

Output5 10 15 20

Robb T. Koether (Hampden-Sydney College) PHP Arrays Fri, Feb 28, 2014 11 / 31

Page 12: Lecture 20 Robb T. Koether - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · 1 PHP Arrays 2 Iteration Structures 3 Displaying Arrays 4 Associative

Displaying Arrays

Displaying an Array$a = array(5, 10, 15, 20);for ($i = 0, $i < count($a), $i++)

echo $a[$i] . ’ ’;

produces

Output5 10 15 20

Robb T. Koether (Hampden-Sydney College) PHP Arrays Fri, Feb 28, 2014 12 / 31

Page 13: Lecture 20 Robb T. Koether - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · 1 PHP Arrays 2 Iteration Structures 3 Displaying Arrays 4 Associative

Displaying Arrays

The foreach Statementforeach ($a as $b)

echo $b . ’ ’;

Or, we may use a foreach loop.

Robb T. Koether (Hampden-Sydney College) PHP Arrays Fri, Feb 28, 2014 13 / 31

Page 14: Lecture 20 Robb T. Koether - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · 1 PHP Arrays 2 Iteration Structures 3 Displaying Arrays 4 Associative

Displaying Arrays

The foreach Statementforeach ($a as $i => $b)

echo "Element $i is $b<br/>";

Or, we may use a foreach loop.

Robb T. Koether (Hampden-Sydney College) PHP Arrays Fri, Feb 28, 2014 14 / 31

Page 15: Lecture 20 Robb T. Koether - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · 1 PHP Arrays 2 Iteration Structures 3 Displaying Arrays 4 Associative

Displaying Arrays

The print_r() Functionprint_r($a);

Or, we may use the print_r function.This is more appropriate for debugging.The above statement will produce the following.

OutputArray ( [0] => 5 [1] => 10 [2] => 15 [3] => 20 )

Robb T. Koether (Hampden-Sydney College) PHP Arrays Fri, Feb 28, 2014 15 / 31

Page 16: Lecture 20 Robb T. Koether - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · 1 PHP Arrays 2 Iteration Structures 3 Displaying Arrays 4 Associative

Displaying Arrays

The var_dump() Functionvar_dump($a);

Or, we may use the var_dump function.This is also very helpful in debugging.The above statement will produce the following.

Outputarray(4) { [0]=> int(5) [1]=> int(10) [2]=> int(15)

[3]=> int(20) }

Robb T. Koether (Hampden-Sydney College) PHP Arrays Fri, Feb 28, 2014 16 / 31

Page 17: Lecture 20 Robb T. Koether - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · 1 PHP Arrays 2 Iteration Structures 3 Displaying Arrays 4 Associative

Displaying Arrays

Heterogeneous Arrays$a = array(5, "dog", true, 6.99);var_dump($a);array(4) { [0]=> int(5) [1]=> string(3) "dog"

[2]=> bool(true) [3]=> float(6.99) }

Arrays in PHP may be heterogeneous.

Robb T. Koether (Hampden-Sydney College) PHP Arrays Fri, Feb 28, 2014 17 / 31

Page 18: Lecture 20 Robb T. Koether - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · 1 PHP Arrays 2 Iteration Structures 3 Displaying Arrays 4 Associative

Outline

1 PHP Arrays

2 Iteration Structures

3 Displaying Arrays

4 Associative Arrays

5 Connecting to a Database

6 Assignment

Robb T. Koether (Hampden-Sydney College) PHP Arrays Fri, Feb 28, 2014 18 / 31

Page 19: Lecture 20 Robb T. Koether - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · 1 PHP Arrays 2 Iteration Structures 3 Displaying Arrays 4 Associative

Associative Arrays

Associative Arraysvariable_name = array(index => value,...)

In PHP, arrays may be associative instead of numerically indexed.In an associative array, two lists of values are associated as a listof paired elements.

The first member of each pair serves as the index.The second member of each pair serves as the value.

Each index must be unique.

Robb T. Koether (Hampden-Sydney College) PHP Arrays Fri, Feb 28, 2014 19 / 31

Page 20: Lecture 20 Robb T. Koether - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · 1 PHP Arrays 2 Iteration Structures 3 Displaying Arrays 4 Associative

Associative Arrays

Associative Arrays$author = array(

"A Tale of Two Cities" => "Charles Dickens","Treasure Island" => "Robert Louis Stevenson","Tom Sawyer" => "Mark Twain","Oliver Twist" => "Charles Dickens","Roughing It" => "Mark Twain"

);

Robb T. Koether (Hampden-Sydney College) PHP Arrays Fri, Feb 28, 2014 20 / 31

Page 21: Lecture 20 Robb T. Koether - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · 1 PHP Arrays 2 Iteration Structures 3 Displaying Arrays 4 Associative

Associative Arrays

Associative Arrays$title = "Treasure Island";echo "The book " . $title . " was written by "

. $author[$title];

produces

The book Treasure Island was written by RobertLouis Stevenson

Robb T. Koether (Hampden-Sydney College) PHP Arrays Fri, Feb 28, 2014 21 / 31

Page 22: Lecture 20 Robb T. Koether - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · 1 PHP Arrays 2 Iteration Structures 3 Displaying Arrays 4 Associative

Associative Arrays

Associative Arrays and foreach

foreach ($author as $a)echo "The author is $a<br/>";

produces

The author is Charles DickensThe author is Robert Louis StevensonThe author is Mark TwainThe author is Charles DickensThe author is Mark Twain

Robb T. Koether (Hampden-Sydney College) PHP Arrays Fri, Feb 28, 2014 22 / 31

Page 23: Lecture 20 Robb T. Koether - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · 1 PHP Arrays 2 Iteration Structures 3 Displaying Arrays 4 Associative

Associative Arrays

Associative Arrays and foreach

foreach ($author as $t => $a)echo "The author of $t is $a<br/>";

produces

The author of A Tale of Two Cities is Charles DickensThe author of Treasure Island is Robert Louis StevensonThe author of Tom Sawyer is Mark TwainThe author of Oliver Twist is Charles DickensThe author of Roughing It is Mark Twain

Robb T. Koether (Hampden-Sydney College) PHP Arrays Fri, Feb 28, 2014 23 / 31

Page 24: Lecture 20 Robb T. Koether - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · 1 PHP Arrays 2 Iteration Structures 3 Displaying Arrays 4 Associative

Associative Arrays

PHP Arrays and HTML Tablesecho "<table>";echo "<tr><th>Title</th><th>Author</th></tr>";foreach ($author as $t => $a)

echo "<tr><td>$t</td><td>$a</td></tr>";echo "</table>";

The above code will produce a table of titles and authors.

Robb T. Koether (Hampden-Sydney College) PHP Arrays Fri, Feb 28, 2014 24 / 31

Page 25: Lecture 20 Robb T. Koether - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · 1 PHP Arrays 2 Iteration Structures 3 Displaying Arrays 4 Associative

Outline

1 PHP Arrays

2 Iteration Structures

3 Displaying Arrays

4 Associative Arrays

5 Connecting to a Database

6 Assignment

Robb T. Koether (Hampden-Sydney College) PHP Arrays Fri, Feb 28, 2014 25 / 31

Page 26: Lecture 20 Robb T. Koether - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · 1 PHP Arrays 2 Iteration Structures 3 Displaying Arrays 4 Associative

Accessing MySQL from PHP

PHP provides several functions for opening and querying adatabase through MySQL.The function mysql_connect() will connect to the MySQLserver.The function mysql_select_db() will open a database.See http://php.net/manual/en/book.mysql.php.See http://php.net/manual/en/book.mysqli.php for thenew, improved interface.See http://php.net/manual/en/mysqli.overview.php.

Robb T. Koether (Hampden-Sydney College) PHP Arrays Fri, Feb 28, 2014 26 / 31

Page 27: Lecture 20 Robb T. Koether - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · 1 PHP Arrays 2 Iteration Structures 3 Displaying Arrays 4 Associative

Connecting to MySQL

The mysql_connect() Functionmysql_connect(hostname,username, password)

wherehostname is the name of the server (localhost).username is the usernamepassword is the password

This function returns a “resource” that represents the connectionto the database.If the connection fails, the value of the resource is 0.

Robb T. Koether (Hampden-Sydney College) PHP Arrays Fri, Feb 28, 2014 27 / 31

Page 28: Lecture 20 Robb T. Koether - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · 1 PHP Arrays 2 Iteration Structures 3 Displaying Arrays 4 Associative

Opening a Database

The mysql_select_db() Functionmysql_select_db(database, resource)

wheredatabase is the name of the databaseresource is the resource returned by mysql_connect()

This function returns a boolean value that tells whether it wassuccessful.The resource parameter is unnecessary if there is only oneconnection.

Robb T. Koether (Hampden-Sydney College) PHP Arrays Fri, Feb 28, 2014 28 / 31

Page 29: Lecture 20 Robb T. Koether - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · 1 PHP Arrays 2 Iteration Structures 3 Displaying Arrays 4 Associative

Opening a Database

Example$hostname = "localhost";$username = "billybob";$password = "euclid";mysql_connect($hostname, $username, $password);mysql_select_db("tigerbook");

This example will open the database tigerbook.

Robb T. Koether (Hampden-Sydney College) PHP Arrays Fri, Feb 28, 2014 29 / 31

Page 30: Lecture 20 Robb T. Koether - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · 1 PHP Arrays 2 Iteration Structures 3 Displaying Arrays 4 Associative

Outline

1 PHP Arrays

2 Iteration Structures

3 Displaying Arrays

4 Associative Arrays

5 Connecting to a Database

6 Assignment

Robb T. Koether (Hampden-Sydney College) PHP Arrays Fri, Feb 28, 2014 30 / 31

Page 31: Lecture 20 Robb T. Koether - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · 1 PHP Arrays 2 Iteration Structures 3 Displaying Arrays 4 Associative

Assignment

AssigmentRead the documentation at the web pagehttp://php.net/manual/en/mysqli.overview.php.

Robb T. Koether (Hampden-Sydney College) PHP Arrays Fri, Feb 28, 2014 31 / 31