We're moving along at a decent pace here, as I know all of you guys want to start actually "programming". As always, bring any questions to the thread and we'll get them taken care of right-quick.
For our next assignment (which will simply build off of our previous assignment) we're going to modify our web page to show a random picture from a list of images. When our page loads, instead of loading the same image every time, it'll choose a random image from a list we give it, and load that.
A heads up, we're learning quite a few concepts here, and I'm intentionally trying to give you only as much information as you absolutely need to stumble through the assignment. I fully expect that you guys will have questions.
We need to know a few things before we can accomplish our goal:
[list type=decimal]
[li]How to tell the server that we're going to do some PHP[/li]
[li]How to store a "list" of image urls[/li]
[li]How to get a random image url from our "list"[/li]
[li]How to use our random image url in our HTML[/li]
[/list]
1) How to tell the server we're going to do some PHP
This is a pretty easy part. We tell the server what's PHP and what isn't by wrapping our PHP in "php tags". This information can be found in the PHP documentation, which is very good, and very complete. For example, this is the documentation for PHP tags: http://php.net/manual/en/language.basic-syntax.phptags.php
The take-away from the link is that we declare when we start using PHP by putting "<?php" in our HTML document. We then declare when we stop using PHP by putting "?>" in our HTML document. Something to remember: You only want to add "?>" when it wouldn't be the last line of your file. The reasoning for this isn't important yet, but it'll likely save you some headache down the line.
2) How to store a list of image urls
The best way to store a list in PHP is using an "array". An array is a structure used in programming that can hold a "list" of information. Arrays get a lot more complex, but this is really all we need know about them for now.
Arrays are assigned to a "variable", and then accessed via that variable. The following link does a good job of explaining what variables are and how to use them (please read it before continuing): http://www.w3schools.com/php/php_variables.asp After you've read what variables are, you'll want to read about what arrays are and how to use those (please read it before continuing): http://www.w3schools.com/php/php_arrays.asp Once you have read through this information, use what you've learned to create an array of urls to some of your favorite images (I'd suggest 5-10 image urls).
3) How to get a random image url from our "list"
It's at this point that we'll have to use our first PHP functions! First we need to put our thinking hats on to figure out how we'd do this. To teach us a little about how to think like programmers, we'll think of our array as a deck of cards.
In real life, when we want to choose a "random" card from a deck, we'll spread out the cards in our hands and then pick one. What are we really doing here? We're using our eyes to gather information (how many cards their are), and then we choose a random card from all those available. Unfortunately, PHP doesn't have eyes, so we need to be PHP's eyes in this case.
So, what information is it that we gather using our eyes? We look down at the cards and we determine how many cards their are to choose from. In order to choose a random card from the deck, we need to know how big the deck is. This is the same for PHP when you want to pick a random item, called an element in a PHP array, from a list, called an array in PHP. In PHP we use functions to perform "actions". The action might be "find out how big that deck of cards is" or it might be "calculate this number cubed".
Now, PHP is also very exacting. While you and I may be okay with knowing "Yeah, we have, like 35 cards or something", PHP isn't cool with that. PHP needs to know exactly how many cards are in the deck. What do you do when you want to know exactly how many of something their are? You count them!
So, before we can randomly choose an element from our array, we need to know how big our array is, we need count the elements in our array. PHP has a function to do just that! The documentation for that function can be found here: http://php.net/manual/en/function.count.php
The documentation at php.net can be a little overwhelming, and the description at the top can be vague if you don't know what's going on. In these cases, it's generally easiest to just to scroll down a little bit to the "examples".
Like I described above, functions accomplish tasks, they tell PHP to perform some action. In the case of count, we're telling PHP to perform the action of counting all the elements in an array. What does PHP need in order to count all the elements in an array? The array, of course!
In order for a function to do anything, we generally need to give that function some information. We do this by passing in information. When you pass information into a function, each piece of information is called a parameter.
When we call the count function, we need to pass it 1 parameter, the array we want to count the elements in. You can see examples of this in the php.net documentation I posted above.
Okay, so when choosing a random card, what action do we need to perform after we know how many cards their are? We need to choose one at random. How do we choose a random number in PHP? We use the rand function! The documentation can be found here: http://php.net/manual/en/function.rand.php The rand function can be called in a couple of different ways, but the way we're going to call it involves giving it 2 parameters (2 pieces of information) and telling it to give us a random integer (integer meaning we want a whole number, as opposed to a decimal number) between a minimum and a maximum.
The first parameter we need to pass into the function is the minimum integer we'd be willing to get back. Conventional wisdom would tell us that the smallest number of something we could have is 1, but that conventional wisdom doesn't serve us well here. In PHP, arrays start at 0. That means the first element of the array will be accessed by asking php for the 0th element of an array. If this sounds confusing, just know that, when working with PHP arrays, you'll start counting from 0, instead of from 1.
So, we now know that we want a random integer, where the smallest possible integer we get back is 0. What else do we need to know to call our rand function? The maximum integer we'd be willing to get back. The maximum integer should refer to the last element in our array of image urls. Conventional wisdom might say that the last element in the PHP array would be equal to the number of elements in that array. Unfortunately, that conventional wisdom is, once again, incorrect when we're thinking about PHP arrays. Since our PHP array starts at 0, the last element is actually 1 less than the number of elements in our array. So, the maximum integer we'll want returned by rand is the number of elements in our array - 1.
Once we have our min and our max, we can call rand and get a random integer between 0 and the length of array (the number of elements in the array) - 1. We can then use this random integer turned by rand to access an element of the array and use it when building our html img tag. Information on how to concatonate strings can be found in the PHP documentation here: http://php.net/manual/en/language.operators.string.php Then, after we concatenate the string, we'll nee dot "echo" the string out, meaning we'll tell PHP to use the string in our html. Information on echo can be found here: http://php.net/manual/en/function.echo.php
After we've done all these things, we've essentially told PHP the following: When I load the page, take a random url from my list of urls and use it to create some html.
Now, I'm going to be blunt. I expect everyone in this thread to fail super-duper-ridiculously-hard on this assignment. I not only expect that you'll ask questions, I'm going to demand it. If you struggle for more than 5 minutes with any specific part, come back and ask a question. I want you guys to succeed, but I also want to keep this thread moving along at a good pace.
When moving at the pace we're moving, failing is completely unavoidable. However, I'm not going to leave you face down in the mud. I'm going to come back, pick you up, dust you off, and get you moving again after showing you what you tripped over. However, like PHP, I don't have eyes to see where or when you're having trouble, I need you to tell me.