====== 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 ==== * Create a new file called 'arrays.php' in your top level directory using Visual Studio Code. * Prepare it with the usual code for a basic file.
==== Named Arrays ==== * We've seen how to create an array like this. $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 ==== * Now, let's display this inside the
tag. echo $animals; * This won't work. When you view the page, you'll see the word 'Array' only. echo doesn't work for arrays. * Try this instead. 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'; * We can read the array in the same way. Add this code to the
tag. echo "MURRAY: the first item of animals is $animals['item1']."; * Now view the page and check the results. ==== 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
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' * You should see your text ten times with the number increasing from 0 to 9. * So this is another way of running the same code multiple times. ==== 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; * We could also move the table to another location as follows. $table->move($location); * There is a whole school of programming based on objects (Object-oriented programming) and objects could be the subject of many classes, but this is all you need to know to use the database connection. [[en:web_development:databases:tables|Next: Tables]]