Web Development Lesson 7 - Databases
Named Arrays
Objective
In this activity you will learn some advanced array techniques for arrays in PHP, which will be useful when processing data from a database.
Setup
<?php
include('header.php');
include('menu.php');
?>
<main>
</main>
<?php
include('footer.php');
?>
Named Arrays
$animals = array('dog', 'elephant', 'rabbit');
This allows us to access each item in the array by number ($animals[0]).
For greater flexibility, in PHP (unlike in Javascript) we can assign names for each element.
Add the following code inside the first PHP block of 'arrays.php'.
$animals = array('item1' => 'dog', 'item2' => 'elephant', 'item3' => 'rabbit');
Displaying Arrays
echo $animals;
print_r($animals);
print_r
is a
When you view the page, you'll see some complicated text that looks something like the array.
To see it properly, right click on the page and select 'View Page Source'.
Now the formatting is much better and you can clearly read the array.
This is one way to create such an array, We can also assign each item separately.
$animals['item1'] = 'dog';
$animals['item2'] = 'elephant';
$animals['item3'] = 'rabbit';
echo "MURRAY: the first item of animals is $animals['item1'].";
Foreach
We've seen how to do a 'for loop' in an earlier class, but there's another loop that is extremely useful for processing arrays. It's the 'foreach' loop.
Add the following PHP code to the <main>
tag after creating your array.
foreach($animals as $name => $item) {
echo "MURRAY: $name is a $item";
}
The first line sets up the loop. It says 'run through this code for each item in the array $animals
, where the reference for each item is $name
and the item itself is $item
'.
We can now refer to the name and the actual item by using the variables $name
and $item
.
echo
then display these values so that we can read them in the
HTML code.
Try it now to be sure you understand it.
While Loop
Just as in Javascript, PHP has many types of loops. The next we'll look at is the 'while loop'.
Add the following code inside a PHP block. It might be best to put it at the end to keep it separate. Change my name to yours so you can find your results in the logs.
$i = 0;
while ($i < 10) {
error_log("MURRAY: the value of i is $i");
$i++;
}
$i = 0;
defines a variable and sets it to '0'.
while ($i < 10) {}
runs the code inside {}
as long as $i is less than 10.
error_log("MURRAY: the value of i is $i");
outputs a string containing the value of $i
to the logs so we can see it.
Finally, $i++;
increments $i
each loop so that it doesn't go on forever.
Open the page 'mysql.php' and check your logs using Putty.
cat /var/log/apache2/error.log | grep 'MURRAY'
Objects
Objects are an advanced data structure and we won't go into it too much here, but you'll need to understand a little bit to use the PHP database connection.
An object is like a named array where you can access each element by name.
But an object also has functions built into. Recall that in Javascript audio
has built-in functions like .play()
, .paused()
and built-in information like .paused
. That's because audio
is an object.
Similarly, in PHP, an object can have both data and functions. For example, a database connection object can have information about the connection and also have functions to extract data.
Whereas you access the information in an array using []
, we access information and functions using ->
.
To use an abstract example, we could create an object called $table.
We could set the number of legs on the table as follows.
$table->legs = 4;
$table->move($location);
Next: Tables