At this point, we have our book list, but we now need a way to create a book and edit books. This will be the first connection to the database that we do.
Open the books.php file. 0:00 I've started you out with a single book example. 0:02 Thinking about how we're going to be using this site, 0:05 it makes sense to extract the book section into its own file, so 0:08 that we can include it multiple times on our page. 0:12 Let's create a new file inside the inc folder. 0:14 We'll call this book.php. 0:21 Inside book.php, move the whole media div. 0:23 Going back to books.php, we'll include this file instead. 0:33 Next, we need to get the book data from our database. 0:51 This can be done by creating a new function. 0:55 Let's open functions.php. 0:57 We'll call this new function getAllBooks. 1:03 We'll be using the global DB. 1:10 And the try catch block for our query. 1:17 We'll rethrow this exception. 1:35 For our query, we'll SELECT all FROM books. 1:43 We'll prepare our statement. 1:57 And then we'll execute. 2:03 Now, we can return, FetchAll. 2:10 Back in the book.php file, we'll use this new function. 2:20 First, we need to include the bootstrap file. 2:25 Then, we'll wrap the include statement with a foreach loop that gets all 2:39 the books, 2:42 GetAllBooks as book. 2:47 Finally, in the book file, 2:56 let's replace the hard coded 3:01 data with our variables. 3:07 Let's go back to our browser and refresh the list. 3:31 Great. 3:35
You need to sign up for Treehouse in order to download course files.Sign up