Viewers: in countries Watching now:
In PHP with MySQL Beyond the Basics, expert instructor Kevin Skoglund introduces powerful PHP programming techniques using object-oriented programming (OOP). Both novice and experienced PHP developers will benefit from the efficient, well-organized, reusable, and easy-to-understand code that OOP offers. Kevin shows how OOP techniques can streamline database queries, help manage sessions, and simplify user logins. While building a real-world web application, Kevin also includes practical advice on topics ranging from structuring code to logging user actions. Exercise files accompany the course.
Now that we have all the CRUD written for our user class, we're ready to apply those methods to our photograph class. Now it's taken us a long time to get to this point, but in a way, this is what we have been working towards all along. The intermediate PHP we learned, the object oriented skills, working with files and directories, uploading files and then developing all of the CRUD for the user model, all of that is so that we're able now to work with the photograph class, which will allow us to upload photographs and work with them in an object oriented way using those same methods that we just wrote.
Now there is one key difference between uploading photographs with the photograph class that we're about to write and the file uploading that we did a view chapters ago. That's why we'll be using the database in order to keep track of the files once they are uploaded. Previously, we just simply uploaded them to the file system and did not have a database keeping track of them. We're going to combine our file uploading skills with the create method that we just completed. Now there are few reasons why storing image information in the database is going to be better than relying on the file system alone. First, it's easier to search and sort images using SQL. Second, we're going to then be able to relate other database records to our photographs records. So for example, when we let people make comments on our photos, we'll able to have an entry in the database for photograph and then have entries for comments that are related to it.
Since we're going to need a database, let's start out by creating that. Now if you remember back in chapter 5 when we created our users table, we also granted access privileges to gallery as a user and we gave it a password. You might have given it something different than me. That's fine, but you can look in that config.php to mind yourself of what it was. So we have the user, the password and the database name. We're going to use those now, instead of logging in as root. Now you could log in as root. If those doesn't work for you, that's fine, but I'm going to open up Terminal here. In order to get to those, we're just going to do mysql -u for the user and instead of root, it's going to be gallery and then --password= and here is my password for gallery and then the database that we want to log in to. So that will put us directly in that database logged in as gallery. Since we said Grant All Privileges, we have the ability to go ahead and create tables there as well.
So the table that we want to create, it's going to look like this. CREATE TABLE photographs. We're going to have an id NOT NULL AUTO_INCREMENT PRIMARY KEY. The file name, which will be the name of the image file. I went ahead and told that it's just VARCHAR (255). The image type. You remember the type is one of the things that is returned to us automatically. It's the mine type; we get that information when we do a file upload. So we'll go head and store it in the database, as well as the size, and then the last thing is going to be caption. That's going to be something that the user will be able to supply. They can caption the photograph. I went ahead and just set that also. It has a 255 character limit, but you can make it text if you wanted to allow people to write much longer description. So for now, I'm just going to limit it 255.
So it says Query OK. So it has now created the table and I can confirm that with SHOW TABLES. So now I have both photographs and users. If say SHOW FIELDS FROM photographs, we can actually see those and see that they were all created properly. So let's quit out of my SQL now and let's switch back over to our main website. Let's create a new file in the includes directory. So I'll open up a new TextMate file and save it, not in the btb_sandbox but inside photo_gallery, includes and we're going to call it photograph.php.
This is where we're going to put our php which is going to define, class Photograph extends DatabaseObject. Even though there is nothing in DatabaseObject, there will be some day. So we'll go head and do that for now. Then at the top, you can already guess what our attributes are going to look like. It's very similar to what we did for user. If you need to open up user as a comparison, put those side-by-side and you can see that we went ahead and declared table name, we declared what database fields and then each of those database fields gets an attribute as well.
Notice also here that there is this nice require_once for the database. That's a good idea. Since we're going to be using the database class, we'll go head and require it at the top. Then last of all, all of these common database methods that we wrote before, they should now all be abstracted so that we can reuse them. So all of them should work. I'm just going to copy them and paste them. Again, once we have database object working, we won't need to do that any more. So for now, I'll just put all those in there, I can collapse all these down, just so they are out of the way. Here we go. So now we have got our common database methods. We're ready to add any new methods that we need to photograph.
Now there is one other thing that I want us to take care of, which is if you remember, when we did the initialize here, you will remember that we required the user file. So we'll want to do the same thing for photograph, so that it gets loaded. Remember that we do have a function called autoload, which is here, which will take care of trying to find it. But since we know we want to load it, let's go ahead and just specifically put that in and initialize, so that we make sure that the photograph class gets loaded up. The very last thing is where we're going to upload these files to. We want to upload them into the photo_ gallery, into public, into images and that is where we're going to put them.
So we want to make sure that that's writable. So let's go back here and let's just go into... for me that's Sites/ photo_gallery and inside public. We should be able to see images. So that is the folder. We want to make sure that is writable by our web server. But right now, it doesn't look like it is. So we want to do something like sudo chown www.images. That for me, well at my web server, which runs as www, I'm going to put my password. It will let it be able to be the owner of that file. Now we have talked about privileges before.
If you want to do something different, you want to make trimod 777, that works just as well. But the important thing is to make sure if we're about to start uploading images into this folder, that's the web sever has the ability to put them there. Once we do, then we're ready to go back to our photograph class and start making the customization that's going to allow it to handle uploaded files. That's what we'll do in the next movie.
Find answers to the most frequently asked questions about PHP with MySQL Beyond the Basics .
Here are the FAQs that matched your search "" :
Sorry, there are no matches for your search "" —to search again, type in another word or phrase and click search.
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.