Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Time to bring the data from WordPress right into Dreamweaver. In this lesson, we'll show you how to make a database connection, create a record set and then add the data from that record set to your Dreamweaver page. So in Dreamweaver, let's open up a previously created file to hold our dynamic data. Now this is just a standard PHP page. It's not hooked into WordPress at all. I am going to go to File > Open and then navigate to my Desktop > Exercise Files, go down to Chapter 11, and open up 11_02. I'll open that up, and it'll come in unstyled.
So the very first thing we are going to do is save it to the local Site Root. So choose File > Save As and then on the Save As dialog box, click Site Root and Save. Now you do not want to update the links because this is coming in with all the links intact, so click No, and there is our basic page we styled with a Exhibits header. Let me go into Live view, so you can see the full page, and it conforms to the rest of the site, not to our blog_comp.
Now the next step is to create a connection to the MySQL database used by WordPress. Now I'm going to need to open up my Database panel, but I need to drop out of Live view temporarily for that to happen. We'll click on Live view and then go to Window > Databases, and that will open up the Database panel which also has Bindings and Server Behaviors as well. We want to add a new MySQL connection. So, click on the plus sign and choose MySQL Connection. Now let's give our connection a name. I like starting with C-O-N-N, conn, and I am going to call this connExhibits.
MySQL server is localhost, my User name is Root, my Password is also root. Now that we've entered the first four fields, we can click Select. If you try to click Select before then, you won't see any databases. So click Select, and there is the databases that have been set up on my localhost. Here is roux_blog, that's the one we want, so we'll click OK. Now, it's always a good idea to test to make sure that it connects all right, and everything looks beautiful. So let's click OK once and then again to make that connection.
Now what Dreamweaver is actually doing is in the Site Root, it creates a folder called Connections and then puts a PHP page with your Connection name as the file name, so here we see connExhibits.php. Now we are ready to go to the Bindings tab. So I am going to collapse files and then click on my Bindings here, and here you can see that we have three out of the four steps already established, and we are going to complete that forth step by adding a record set, but that record set is going to employ a fairly complex bit of SQL.
So to save a little time, I have gone ahead and created that for you. I am going to open up that file from the exercise files again. So go over to File > Open, and let's head back to our Desktop > Exercise Files in Chapter 11, again 11_02, and this time open up recordset.sql. So you may look at this, and if you're not that familiar with SQL and think of it as a very complex SQL statement, it's actually relatively straightforward. Let me walk through it a little bit so you understand it better. But basically, what we are doing here is selecting a set of records, hence the name recordset.
So we want the recordset to be distinct so that there are no duplicates, and that's the keyword distinct here, and we are going to bring in the WordPress post title as well as the content. So just those two items, the title and the content, and we're bringing those in from a table called wp_posts which is pretty much self- explanatory, and then also the wp_term_relationships table is being used. Now that's the table that monitors the categories among other things and its relationship to the various posts.
So after that connection is made here in the WHERE statement, we want to make sure that we are filtering on a field called post_status listed here, and that is set to private and then also another field in the wp_term_relationships table called term_taxonomy_id, and that's going to be set to a specific value. Now just because I use this SQL statement a lot, I have this set up with a couple of question marks, but we want to replace those question marks with a certain value, and what is that value? Well, it's going to be the category number that we saw earlier, which as you might recall for me is 10.
The order of the records that is returned, which is the next and final line on the SQL statement, is set to showing the newest one first or in other words the dates descend. So I am going to select the SQL statement, copy it, and head back to my exhibits.php file where we are ready to create the recordset. So I'm in the Bindings panel, I'll choose the plus sign, and then select Recordset (Query). Now what you see on the screen here is what's known as the simple version of the Recordset. There are two different views. We want to switch to the Advanced view.
Now if you have done some database work before, and you have established another recordset where you did use Advanced view, that will come up automatically. So, I am going to call this rsExhibits. And I want to make sure that my connection is set to connExhibits, and since that's our only connection, that's what's going to happen. Now after I press Tab, it goes to the SQL field, and I can delete what's selected and paste in with a Command+V, MySQL statement. Now let's go ahead and test this by clicking the Test button here. And here you see the one record that we have, Student Exhibit, and a little bit of content there.
That's good. So click OK once and then one more time to close the Recordset. All right. We are actually ready to bring the content onto the page. So I am going to put my cursor where I want it to appear, and you have to do this in Design view, you can't do it in Live view, and I am just going to put it right after the paragraph, and I am going to expand my Recordset that you see here--let me bring the Field name over a little bit so you can see all the information-- and here you see the two fields that we brought in, post_title and post_content. We are going to insert post_title first.
So you can drag and drop it onto the screen, or if you've already positioned your cursor as I have where you wanted to go, you can select what you want and then click Insert. Now, that will come in as a standard paragraph, as you can see down here in the Tag Selector at the bottom of the screen. Let me open up my Property Inspector, and I am going to change that Format to an h2 tag. After that, I'll hit Return so after the title we'll put in the content. Now, this time I am just going to go ahead and drag that content in to show you the alternative way that you can work with this, and each time I do, you see that it lists in curly braces, and it slightly highlighted the Recordset name rsExhibits and then dot and whatever the field name is.
Okay, so there is our two bits of content. Let's save the page, and now I am going to go into Live view, and Live view works not only for CSS and PHP, but it also works for MySQL. So we can see that brings in the dynamic content, and we do have a little bit of work to do here, but that's good because it shows that up. So I am going to go ahead and create a new CSS rule in main.css. Let's go down a little bit here, and I'll put it towards the bottom, right above the @media statement for my phone.
So this will be one for mainContent and an unordered list, and that will handle any unordered lists that are encountered here. I am just going to set the margin, 0 at the top, 0 on the right, do 15 pixels on the bottom, and 20 pixels on the left. So I'll save that and then head back to Design view, and it's much better. But at the moment, this dynamic page would only show one event. So if I toggle off Live view, you can see that I just have these two bits of code here, moreover, the event that it would always show would be the most current.
Now that might work in some situations, that might be in fact exactly what you want to do, but let's say we want it to show multiple events. So what do we have to do? Well, what we need to do is wrap whatever it is we want to repeat in what's known as a Repeat Region server behavior. So I am going to add one more bit of content, and that's a horizontal rule here, and I'll put that right after my content so that we have a little bit of separation between the two. I am just going to go right to the Source Code and put my cursor there.
So instead of a paragraph tag here, I'll make this an hr tag, okay. So, we'll save that and then head back to Design view, and you can see my new horizontal rule there. So now we are ready to add a Dreamweaver server behavior. I am actually going to go back into the Split view as I found it's actually easier a lot of times to select exactly the content that you want. And in this case, what I want are these three code lines here, the h2 which has the PHP code for showing the title, the paragraph tag which has the PHP code for showing post_content, and then my horizontal rule.
With that selected, let's go over to our Server Behaviors panel where you can see certain Server Behaviors have already been added. There is our Recordset, and these are the two items that we inserted into the text. So, I'll click the Add button and choose the second option which is Repeat Region. Now when it comes up, you'll show a list of your recordsets. If we had more than one defined, it would be available to us from the list here, but we only have rsExhibits. So that's pre-selected for us. And there is two options. You can choose to show a finite number of records at a time, and it's definable, it can be 10 as the default setting is or 5 or 3, or if you want to show all the records as we do, you can choose All Records.
So then click OK and Dreamweaver will add in the necessary code, it's a PHP do-while loop. And let's go back to Design view now, and we'll turn on Live view, click Save and right now what you'll see is basic code, so we still have just one bit of content here, so you're only going to see one record. Let's go back to WordPress, and I'm going to add a new post that I'll call Spotlight Exhibit, and we have some sample content as well. And again, I am going to add in a few bullet points. Okay, we are going to do those same two things that we did before when we changed the visibility to private, so let's go over to Edit, next to Visibility, change that to Private, and click OK, and we already have our Exhibit category set up, so I'll click Exhibits and Update.
Let's head back to Dreamweaver, and I am just going to refresh the page, and now we have two exhibits listed on our Exhibit page, Spotlight Exhibit, and Student Exhibit. There is the separator and everything looks in proper date order that we had set up. So, this is really just the start of what you can do with data from a WordPress-driven database. All of the other Dreamweaver Server Behaviors are at your disposal as well.
Get unlimited access to all courses for just $25/month.Become a member
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.