This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
en:web_development:databases:php [2021/12/23 15:23] mag [Foreach] |
en:web_development:databases:php [2023/08/16 09:33] (current) |
||
---|---|---|---|
Line 17: | Line 17: | ||
include(' | include(' | ||
?></ | ?></ | ||
+ | |||
+ | ==== 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 in this actviity. | ||
+ | * 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. | ||
==== PDO Connection ==== | ==== PDO Connection ==== | ||
Line 22: | Line 35: | ||
* Copy the following code at the top of the first PHP block. | * Copy the following code at the top of the first PHP block. | ||
< | < | ||
- | define (' | + | define (' |
include_once DBCONNECT; | include_once DBCONNECT; | ||
- | $db = 'webdev'; | + | $db = 'techschoolwebdev'; |
$dsn = " | $dsn = " | ||
try | try | ||
Line 37: | Line 50: | ||
* In the next line we define the database name (< | * In the next line we define the database name (< | ||
* The fourth line creates a string with all our connection details including the host, user name and the character set we'll use. < | * The fourth line creates a string with all our connection details including the host, user name and the character set we'll use. < | ||
- | * The final block sets up the connection. This primarily occurs in < | + | * The final block sets up the connection. This primarily occurs in < |
* We wrap the connection in a < | * We wrap the connection in a < | ||
* It's not necessary to remember all this. It's sufficient to know where to find it and to include it in your code each for each site. | * It's not necessary to remember all this. It's sufficient to know where to find it and to include it in your code each for each site. | ||
Line 51: | Line 64: | ||
* Next, we need to set up the query by sending only that data to the database. | * Next, we need to set up the query by sending only that data to the database. | ||
< | < | ||
- | * < | + | * < |
* We can now safely send our parameters and have the database run our query. | * We can now safely send our parameters and have the database run our query. | ||
< | < | ||
- | * The final step is to retrieve the results of the query, which we do in a loop. | + | * The final step is to retrieve the results of the query, which we do in a loop to get our data line by line. |
- | + | ||
- | ==== While Loops ==== | + | |
- | * 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. | + | |
- | < | + | |
- | while ($i < 10) { | + | |
- | error_log(" | + | |
- | $i++; | + | |
- | }</ | + | |
- | * < | + | |
- | * < | + | |
- | * < | + | |
- | * Finally, < | + | |
- | * Open the page ' | + | |
- | < | + | |
- | * 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. | + | |
- | * Let's use it to get our data line by line. | + | |
* Add the following code after < | * Add the following code after < | ||
< | < | ||
Line 78: | Line 73: | ||
}</ | }</ | ||
* This code fetches the a single row of data from our query result < | * This code fetches the a single row of data from our query result < | ||
- | |||
- | ==== Displaying Arrays ==== | ||
- | |||
* Let's display that row in our error log. Place the following code inside the < | * Let's display that row in our error log. Place the following code inside the < | ||
< | < | ||
* If you refresh your page and check your logs, you'll see the data from our database. | * If you refresh your page and check your logs, you'll see the data from our database. | ||
- | * Notice the new command | + | * Notice the new use of two parameters with < |
* The first parameter < | * The first parameter < | ||
* The second parameter < | * The second parameter < | ||
Line 123: | Line 115: | ||
} | } | ||
?> | ?> | ||
- | </body> | + | </tbody> |
</ | </ | ||
</ | </ | ||
Line 157: | Line 149: | ||
==== Repackaging Data ==== | ==== Repackaging Data ==== | ||
- | * While what we've done is great, it feels like we're reprinting data unnecessarily. | + | * While what we've done is great, it feels like we're reprinting data (first published etc) unnecessarily. |
* It would be better to present one line for each hero with a list of movies they' | * It would be better to present one line for each hero with a list of movies they' | ||
* To do that, let's repackage the data into another array. | * To do that, let's repackage the data into another array. | ||
Line 165: | Line 157: | ||
* Here we are creating an array called < | * Here we are creating an array called < | ||
* The character array has an element called ' | * The character array has an element called ' | ||
- | * Notice that in PHP we can name an array element. This isn't possible in Javascript, where array elements are always numbered. | ||
* The trick is that if the character is repeated, it will overwrite the existing data and we don't mind. That is, < | * The trick is that if the character is repeated, it will overwrite the existing data and we don't mind. That is, < | ||
* Do the same for ' | * Do the same for ' | ||
Line 173: | Line 164: | ||
< | < | ||
* The < | * The < | ||
- | |||
- | ==== Foreach ==== | ||
- | * There' | ||
- | * Add the following code to the end of the last block of PHP code. | ||
- | < | ||
- | foreach($my_array as $name => $item) { | ||
- | error_log(" | ||
- | }</ | ||
- | * The first line creates an array, but unlike the arrays we've seen, each item in the array is named. | ||
- | * Instead of referring to an item by number (< | ||
- | * The second 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 < | ||
- | * < | ||
- | * Try it now to be sure you understand it. | ||
- | * The value of this is that we don't need any extra code to determine the length of the array. | ||
* Now, to display our hero data, we can loop through the < | * Now, to display our hero data, we can loop through the < | ||
< | < | ||
Line 201: | Line 177: | ||
< | < | ||
</ | </ | ||
- | * Run this code to see that it shows a single row per hero and lists all the movies (all those captured in the database) in the last cell. | + | * Run this code to see that it shows a single |
* Here's the final code in case you are having trouble with placement of the various items. | * Here's the final code in case you are having trouble with placement of the various items. | ||
< | < |