User Tools

Site Tools


en:web_development:media:named_arrays

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

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 - Media ======+====== Web Development Lesson - Media ======
 ===== Named Arrays ===== ===== Named Arrays =====
 ==== Objective ==== ==== Objective ====
Line 37: Line 37:
     }     }
 ?> ?>
-            <!-- <img src="https://images.pexels.com/photos/674010/pexels-photo-674010.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500" /> 
-            <img src="https://images.ctfassets.net/hrltx12pl8hq/61DiwECVps74bWazF88Cy9/2cc9411d050b8ca50530cf97b3e51c96/Image_Cover.jpg?fit=fill&w=480&h=270" /> 
-            <img src="https://images.ctfassets.net/hrltx12pl8hq/3MbF54EhWUhsXunc5Keueb/60774fbbff86e6bf6776f1e17a8016b4/04-nature_721703848.jpg?fit=fill&w=480&h=270" /> 
-            <img src="https://cdn.pixabay.com/photo/2021/08/25/20/42/field-6574455__340.jpg" /> 
-            <img src="https://www.gettyimages.es/gi-resources/images/500px/983794168.jpg" /> 
-            <img src="https://media.istockphoto.com/photos/concept-of-an-open-magic-book-open-pages-with-water-and-land-and-picture-id1279460648?b=1&k=20&m=1279460648&s=170667a&w=0&h=uZa830sWo8hlFN0Y7FnQ14giNC0Z2EBNuTMuNJeJhQg=" /> --> 
         </div>         </div>
     </main>     </main>
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.
 <code>$animals = array('dog', 'elephant', 'rabbit');</code> <code>$animals = array('dog', 'elephant', 'rabbit');</code>
   * 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, in PHP (unlike in Javascript) we can assign names for each element.   * 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'. 
 <code>    $animals = array('item1' => 'dog', 'item2' => 'elephant', 'item3' => 'rabbit');</code> <code>    $animals = array('item1' => 'dog', 'item2' => 'elephant', 'item3' => 'rabbit');</code>
 +  * Update the '$photos' array in 'media.php' to take advantage of this format.
 +<code>    $photos = array("Photo 1" => "https://images.pexels.com/photos/674010/pexels-photo-674010.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500",
 +                    "Photo 2" => "https://images.ctfassets.net/hrltx12pl8hq/61DiwECVps74bWazF88Cy9/2cc9411d050b8ca50530cf97b3e51c96/Image_Cover.jpg?fit=fill&w=480&h=270",
 +                    "Photo 3" => "https://images.ctfassets.net/hrltx12pl8hq/3MbF54EhWUhsXunc5Keueb/60774fbbff86e6bf6776f1e17a8016b4/04-nature_721703848.jpg?fit=fill&w=480&h=270",
 +                    "Photo 4" => "https://cdn.pixabay.com/photo/2021/08/25/20/42/field-6574455__340.jpg",
 +                    "Photo 5" => "https://www.gettyimages.es/gi-resources/images/500px/983794168.jpg",
 +                    "Photo 6" => "https://media.istockphoto.com/photos/concept-of-an-open-magic-book-open-pages-with-water-and-land-and-picture-id1279460648?b=1&k=20&m=1279460648&s=170667a&w=0&h=uZa830sWo8hlFN0Y7FnQ14giNC0Z2EBNuTMuNJeJhQg=");</code>
  
 ==== Displaying Arrays ==== ==== Displaying Arrays ====
   * Now, let's display this inside the <html><main></html> tag.   * Now, let's display this inside the <html><main></html> tag.
-<code   echo $animals;</code> +  * <html>echo $photos;</html> won't work. When you view the page, you'll see the word 'Array' only because <html>echo</html> only prints strings.
-  * This won't work. When you view the page, you'll see the word 'Array' only<html>echo</html> doesn't work for arrays.+
   * Try this instead.   * Try this instead.
-<code>    print_r($animals);</code> +<code>    print_r($photos);</code> 
-  * <html>print_r</html> is a +  * <html>print_r</html> displays more complex structures including arrays.
   * 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['item3'] = 'rabbit';</code>     $animals['item3'] = 'rabbit';</code>
   * We can read the array in the same way. Add this code to the <html><main></html> tag.   * We can read the array in the same way. Add this code to the <html><main></html> tag.
-<code>    echo "MURRAY: the first item of animals is $animals['item1'].";</code>+<code>    echo "MURRAY: the first photo is {$photos['Photo 1']}.";</code>
   * 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's another loop that is extremely useful for processing arrays. It's the 'foreach' loop.+  * We've seen how to do a 'for loop' in the previous activity, but there's another loop that is extremely useful for processing arrays. It's the 'foreach' loop.
   * Add the following PHP code to the <html><main></html> tag after creating your array.   * Add the following PHP code to the <html><main></html> tag after creating your array.
-<code>    foreach($animals as $name => $item) { +<code>    foreach($photos as $name => $link) { 
-        echo "MURRAY: $name is a $item";+        echo "MURRAY: $name is a $link";
     }</code>     }</code>
-  * The first line sets up the loop. It says 'run through this code for each item in the array <html>$animals</html>, where the reference for each item is <html>$name</html> and the item itself is <html>$item</html>'+  * The first line sets up the loop. It says 'run through this code for each item in the array <html>$photos</html>, where the reference for each item is <html>$name</html> and the item itself is <html>$link</html>'
-  * We can now refer to the name and the actual item by using the variables <html>$name</html> and <html>$item</html>+  * We can now refer to the name and the actual item by using the variables <html>$name</html> and <html>$link</html>
-  * <html>echo</html> then display these values so that we can read them in the HTML code.+  * <html>echo</html> can then display these values so that we can read them in the HTML code.
   * 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.
 +<code><?php
 +    foreach($photos as $name => $link) {
 +?>
 +            <figure>
 +                <img src='<?php echo $link; ?>' alt='<?php echo $name; ?>'>
 +                <figcaption><?php echo $name; ?></figcaption>
 +            </figure>
 +<?php
 +    }
 +?></code>
 +  * There are a couple of things to note here.
 +  * We've used a couple of new HTML tags. <html><figure></html> is a block element that can be used to present an image and a caption. <html><figcaption></html> is used to hold the caption for an image.
 +  * 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 <html>\t</html> and <html>\n</html> to make it readable in the View Page Source view, we've left the HTML as is and created a new PHP block for each variable to be displayed.
 +  * 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.
 +<code>figcaption {
 +  display: block;
 +  text-align: center;
 +  font-family: 'Arial', sans-serif;
 +  color: green;
 +}</code>
  
 ==== 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:web_development:media:exercises|Next: Exercises]]
-  * 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 <html>audio</html> has built-in functions like <html>.play()</html>, <html>.paused()</html> and built-in information like <html>.paused</html>. That's because <html>audio</html> 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 <html> [] </html>, we access information and functions using <html>-></html>+
-  * To use an abstract example, we could create an object called $table. +
-  * We could set the number of legs on the table as follows. +
-<code>$table->legs = 4;</code> +
-  * We could also move the table to another location as follows. +
-<code>$table->move($location);</code> +
-  * 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]]+
en/web_development/media/named_arrays.1640468913.txt.gz · Last modified: 2023/08/16 09:33 (external edit)