This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
en:web_development:media:named_arrays [2021/12/25 13:48] mag created |
en:web_development:media:named_arrays [2023/08/16 09:33] (current) |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Web Development Lesson | + | ====== Web Development Lesson |
===== Named Arrays ===== | ===== Named Arrays ===== | ||
==== Objective ==== | ==== Objective ==== | ||
Line 37: | Line 37: | ||
} | } | ||
?> | ?> | ||
- | <!-- <img src=" | ||
- | <img src=" | ||
- | <img src=" | ||
- | <img src=" | ||
- | <img src=" | ||
- | <img src=" | ||
</ | </ | ||
</ | </ | ||
Line 160: | Line 154: | ||
==== Named Arrays ==== | ==== Named Arrays ==== | ||
- | * We've seen how to create an array like this. | + | * We've seen how to create an array in PHP like this. |
< | < | ||
* This allows us to access each item in the array by number ($animals[0]). | * This allows us to access each item in the array by number ($animals[0]). | ||
* For greater flexibility, | * For greater flexibility, | ||
- | * Add the following code inside the first PHP block of ' | ||
< | < | ||
+ | * Update the ' | ||
+ | < | ||
+ | "Photo 2" => " | ||
+ | "Photo 3" => " | ||
+ | "Photo 4" => " | ||
+ | "Photo 5" => " | ||
+ | "Photo 6" => " | ||
==== Displaying Arrays ==== | ==== Displaying Arrays ==== | ||
* Now, let's display this inside the < | * Now, let's display this inside the < | ||
- | <code> echo $animals;</code> | + | * <html>echo $photos;</html> won't work. When you view the page, you'll see the word ' |
- | * This won't work. When you view the page, you'll see the word ' | + | |
* Try this instead. | * Try this instead. | ||
- | < | + | < |
- | * < | + | * < |
* When you view the page, you'll see some complicated text that looks something like the array. | * 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' | * To see it properly, right click on the page and select 'View Page Source' | ||
Line 182: | Line 181: | ||
$animals[' | $animals[' | ||
* We can read the array in the same way. Add this code to the < | * We can read the array in the same way. Add this code to the < | ||
- | < | + | < |
* Now view the page and check the results. | * Now view the page and check the results. | ||
==== Foreach ==== | ==== Foreach ==== | ||
- | * We've seen how to do a 'for loop' in an earlier class, but there' | + | * We've seen how to do a 'for loop' in the previous activity, but there' |
* Add the following PHP code to the < | * Add the following PHP code to the < | ||
- | < | + | < |
- | echo " | + | echo " |
}</ | }</ | ||
- | * The first line sets up the loop. It says 'run through this code for each item in the array < | + | * The first line sets up the loop. It says 'run through this code for each item in the array < |
- | * We can now refer to the name and the actual item by using the variables < | + | * We can now refer to the name and the actual item by using the variables < |
- | * < | + | * < |
* Try it now to be sure you understand it. | * Try it now to be sure you understand it. | ||
+ | * When you're ready, add the following code to display the photos along with the respective names. | ||
+ | < | ||
+ | foreach($photos as $name => $link) { | ||
+ | ?> | ||
+ | < | ||
+ | <img src='<? | ||
+ | < | ||
+ | </ | ||
+ | <?php | ||
+ | } | ||
+ | ?></ | ||
+ | * There are a couple of things to note here. | ||
+ | * We've used a couple of new HTML tags. < | ||
+ | * Secondly, we've changed the way we encode the HTML block. Rather than include all the HTML inside a string, which requires us to add < | ||
+ | * This is much more readable for me. | ||
+ | * Refresh your page and check that it works as it should. | ||
+ | * It needs a bit of styling. Try this or add your own. | ||
+ | < | ||
+ | display: block; | ||
+ | text-align: center; | ||
+ | font-family: | ||
+ | color: green; | ||
+ | }</ | ||
==== While Loop ==== | ==== While Loop ==== | ||
Line 213: | Line 235: | ||
* So this is another way of running the same code multiple times. | * So this is another way of running the same code multiple times. | ||
- | ==== Objects ==== | + | [[en: |
- | * 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 < | + | |
- | * 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 < | + | |
- | * To use an abstract example, we could create an object called $table. | + | |
- | * We could set the number of legs on the table as follows. | + | |
- | < | + | |
- | * We could also move the table to another location as follows. | + | |
- | < | + | |
- | * 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: | + |