Building webpages with PHP involves the use of links and URLs. A PHP link is simply an HTML link, however, combining PHP and HTML is a great way to make links dynamic. This video walks through the creation of dynamic links using the echo command to inject PHP values into HTML. Using this method, you can create dynamic links based on code or even using values from a MySQL database.
Now that we have our PHP fundamentals, we're ready to start building webpages with PHP. Think back for a moment to the diagram that we saw, with the request response cycle, that shows requests starting at the browser, going to the web server, the web server processes our PHP and returns an HTML page back to the browser. That cycle, that request response cycle begins with a browser request. Nothing happens on the web server unless the user sends in some information to us. And there are only three ways that we can get data from users on the web. There's URL's and links.
There's forms, web forms that you fill out with information and click submit, and then there's cookies. Those are browser cookies that are stored on the browser. And each one of these corresponds to an HTTP method. The first is Get, the second is Post and the last is Cookie. So, URLs and Links are Get requests, a form is a Post request. And Cookie, well, it's not really a cookie request, but it's the way that we access the cookie information that piggybacks on each request. So, these are the three main things that we're going to be looking at. Now, every web language has a different way to interact with these three types of user data, and over the next three chapters, we'll look at how it's done in PHP.
We'll start by looking at URLs and links. So to begin looking at links, let's first go to our basic.html page. And let's do Save As on that. And we're going to save the first one as firstpage.php. And I'll save that. And now, let's go ahead and, before we do anything else let's do another Save As. And we're going to change this one to secondpage.php. That will make it really obvious that we have these two pages, and I'm also going to go and open them both up so that they're both visible. So here's first page, and here's second page.
Now I can switch back and forth, and I can start to make links between them. That's what I want to do here, that's how we're going to see this. So let's go ahead and give this one a title of First Page and save it. We'll this one a title of Second Page. Now, we'll be able to see that in the browser and know which one is which. So let's go to first page, and let's make a link on First Page. A link in PHP is just an HTML link, that's all it is. So open our tag and then a href equals, and then inside quotes we're going to put where the page is going to go to, then we need some text for it, call it second page, and then close the A tag.
So where are we going to link it to? Second_page.php. That's going to link it to the second page. Now, that's not PHP, that's just basic HTML. We don't even have PHP tags in that. Let's go and bring that up in a browser. Localhost, for me it's going to be kevinstovand/sandbox and then firstpage.php. There we are, there's my link. Nice and simple. I click on it, and there I am, now I'm on the second page. See how that worked? I can click the back button, go back to the first page, and link back to the other one. So that's it. That's all there is to making links, in PHP is to make HTML links. Now, the great thing about PHP though is that we can start to improve these links. We can make them more dynamic.
So, for example, instead of having second page, we can cut that and let's go up here. And let's make a PHP tag, and let's call this link name equals and then in quotes, second page. Now, we could use our PHP to provide the text. PHP Echo, and then dollar sign, link name. Now, make sure that you use Echo, very important here. We're outputting code into the HTML, so we need that echo. Super important, don't forget it. This won't get the job done for you.
So, now let's just bring it up and try it again real quick. We'll go back to our first page and let's just reload the page, and you see that we still see the second page. So now we've got some dynamic text, this is something that could potentially be pulled from a database, for example. Or maybe we have an if else statement that decides if one condition is true, give it the length this name, if it's something else, give it a different name. We can also have PHP that decides which link to display. So it's either log in if you're not logged in, or it's log out if you are already logged in. One of the ways that we send a lot of dynamic data, is by sending something in the query string.
The query string is what comes after the page name. So for example, id equals 1, that would be a static link. Let's just save that. Come back over here and reload the page. And now when I go to second page, you can see it says PHP id equals 1. That's the kind of thing that PHP is really good for. Let's imagine that we had a loop for a second. We could put a loop around this tag, and then maybe we were outputting a list of categories on a website or subjects. We could go through and use a loop to iterate through each of the subjects. So we can have a subject id equals 1, this link, subject id 2 equals this length.
Let's go ahead and just make PHP equals, and I'll just make 2 for now, just so that's something a little different. And then here, PHP, remember we need that echo, and then id. There we go. Rmemeber to do those echoes. Now we come back here to first page, reload the page, and the link is going to go id two. You can see that the very bottom of the page, the browser gives me a hint for that, where I can click on it and see now id is equal to two. So try that. If you want to try putting this inside the loop and looping from the numbers one through ten.
And then you can see that every time id will increment, and you would then have links that go to different ids. Now, once we send it to the other page though, the second page needs to be able to pick up on that. The second page, it's very important that it says, alright, I have an id 2 that requires some different processing than if I had id number four. Otherwise it's not a dynamic page, so how do we get that get value to work with. We'll look at that in the next movie.
- What is PHP?
- Installing and configuring PHP and MySQL
- Exploring data types
- Controlling code with logical expressions and loops
- Using PHP's built-in functions
- Writing custom functions
- Building dynamic webpages
- Working with forms and form data
- Using cookies and sessions to store data
- Connecting to MySQL with PHP
- Creating and editing database records
- Building a content management system
- Adding user authentication
Skill Level Beginner
Q: This course was revised on 6/4/2013. What changed?
A: The old version of this course was 6 years old and it was time for a complete revision, using PHP 5.4. (The tutorials will work with any version of PHP and covers any differences you might encounter). The author has also added updated installation instructions for Mac OS X Mountain Lion and Windows 8. The topics and end project are the same, but the code is slightly different. It also addresses frequently asked questions from the previous version.
Q: This course was updated on 5/20/2015. What changed?
A: We added one movie called "Changing the document root in Yosemite," which helps the Mac installation run more smoothly.
PHP with MySQL Beyond the Basics (2009)with Kevin Skoglund10h 26m Intermediate
Ajax with PHP: Add Dynamic Content to Websiteswith Kevin Skoglund3h 37m Intermediate
1. PHP Overview
2. Mac Installation
3. Windows Installation
4. First Steps
5. Exploring Data Types
6. Control Structures: Logical Expressions
7. Control Structures: Loops
8. User-Defined Functions
10. Building Web Pages with PHP
11. Working with Forms and Form Data
12. Working with Cookies and Sessions
13. MySQL Basics
14. Using PHP to Access MySQL
15. Building a Content Management System (CMS)
16. Using Site Navigation to Choose Content
17. Application CRUD
18. Building the Public Area
19. Regulating Page Access
Next steps2m 4s
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.