Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Starting the Photograph class

From: PHP with MySQL Beyond the Basics

Video: Starting the Photograph class

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.

Starting the Photograph class

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.

Show transcript

This video is part of

Image for PHP with MySQL Beyond the Basics
PHP with MySQL Beyond the Basics

108 video lessons · 43644 viewers

Kevin Skoglund
Author

 
Expand all | Collapse all
  1. 4m 4s
    1. Welcome
      1m 28s
    2. Using the exercise files
      2m 36s
  2. 4m 36s
    1. PHP and MySQL installation
      48s
    2. Project setup
      3m 48s
  3. 54m 26s
    1. Using variable variables
      5m 20s
    2. Applying more array functions
      5m 32s
    3. Building dates and times: Epoch/Unix
      8m 47s
    4. Formatting dates and times: Strings and SQL
      10m 6s
    5. Setting server and request variables
      5m 37s
    6. Establishing global and static variable scope
      5m 43s
    7. Making a reference assignment
      3m 0s
    8. Using references as function arguments
      4m 8s
    9. Using references as function return values
      6m 13s
  4. 32m 16s
    1. Introducing the concept and basics of OOP
      6m 25s
    2. Defining classes
      4m 7s
    3. Defining class methods
      3m 30s
    4. Instantiating a class
      6m 0s
    5. Referencing an instance
      4m 40s
    6. Defining class properties
      7m 34s
  5. 50m 52s
    1. Understanding class inheritance
      6m 24s
    2. Setting access modifiers
      8m 3s
    3. Using setters and getters
      4m 2s
    4. Working with the static modifier
      8m 20s
    5. Reviewing the scope resolution operator
      2m 50s
    6. Referencing the Parent class
      6m 49s
    7. Using constructors and destructors
      6m 10s
    8. Cloning objects
      3m 27s
    9. Comparing objects
      4m 47s
  6. 14m 48s
    1. Overview of the project
      3m 9s
    2. Creating project and asset directories
      6m 8s
    3. Creating a MySQL database
      5m 31s
  7. 1h 24m
    1. Creating the MySQL Database class
      6m 12s
    2. Adding queries to the MySQL Database class
      3m 9s
    3. Using the database object
      6m 47s
    4. How OOP is an improvement
      5m 16s
    5. Creating a User class
      6m 25s
    6. Instantiating user objects
      8m 46s
    7. Revising find methods to instantiate
      4m 3s
    8. Autoload: The undeclared object safety net
      4m 8s
    9. Creating the Session class
      7m 11s
    10. Logging in using the Session class
      8m 56s
    11. Initializing files and path constants
      6m 2s
    12. Using path content for layout
      4m 51s
    13. Late static binding
      13m 5s
  8. 1h 29m
    1. File system basics
      5m 28s
    2. Understanding file permissions
      8m 48s
    3. Setting file permissions
      5m 7s
    4. PHP permissions
      10m 53s
    5. Accessing files
      9m 45s
    6. Writing to files
      5m 19s
    7. Deleting files
      1m 51s
    8. Moving the file pointer
      3m 58s
    9. Reading files
      6m 17s
    10. Examining file details
      6m 26s
    11. Working with directories
      6m 44s
    12. Viewing directory content
      5m 28s
    13. Creating a log file: Assignment
      5m 44s
    14. Creating a log file: Solution
      7m 56s
  9. 23m 52s
    1. Configuring PHP for file uploads
      5m 42s
    2. Sending files as form data
      4m 22s
    3. Inspecting uploaded files
      3m 55s
    4. Uploading errors
      3m 59s
    5. Moving uploaded files
      5m 54s
  10. 30m 0s
    1. Remaining user CRUD
      2m 4s
    2. Creating users
      5m 18s
    3. Updating users
      5m 58s
    4. Deleting users
      3m 39s
    5. Abstracting the database table name
      3m 30s
    6. Abstracting the attributes
      5m 56s
    7. Finding the database attributes
      3m 35s
  11. 48m 55s
    1. Starting the Photograph class
      6m 16s
    2. Coding the Photograph class
      5m 53s
    3. Saving photographs
      5m 22s
    4. Uploading photographs
      6m 46s
    5. Listing photographs
      5m 55s
    6. Storing messages in the Session class
      6m 15s
    7. Deleting photographs
      7m 54s
    8. Displaying photographs in the public area
      4m 34s
  12. 27m 23s
    1. Creating the Comment class
      7m 7s
    2. Building the comment form
      6m 43s
    3. Listing comments
      6m 5s
    4. Reviewing comments in the staff area
      7m 28s
  13. 24m 59s
    1. Understanding the concepts behind pagination
      2m 51s
    2. Using LIMIT, OFFSET, and COUNT
      2m 56s
    3. Finding the pagination variables
      5m 51s
    4. Using the Pagination class
      5m 2s
    5. Paginating photographs
      2m 53s
    6. Using pagination links
      5m 26s
  14. 43m 0s
    1. Configuring PHP for email
      7m 30s
    2. Sending email with mail()
      7m 9s
    3. Using headers
      6m 50s
    4. Reviewing SMTP
      4m 9s
    5. Using PHPMailer
      8m 41s
    6. Building notification for new comments
      8m 41s
  15. 17m 46s
    1. Using MVC architecture
      4m 12s
    2. Simple templating
      8m 10s
    3. Using the Smarty PHP templating engine
      2m 10s
    4. Building PHP libraries and frameworks
      3m 14s
  16. 1m 40s
    1. Goodbye
      1m 40s
  17. 1h 14m
    1. Installing on Mac 10.4 (Tiger)
      11m 59s
    2. Installing on Mac 10.5 (Leopard)
      11m 23s
    3. Configuring a Mac
      13m 39s
    4. The text editor on Mac
      3m 27s
    5. phpMyAdmin installation on Mac
      2m 26s
    6. Installing on Windows
      6m 30s
    7. Configuring on Windows
      8m 21s
    8. The text editor on Windows
      4m 11s
    9. MySQL basics: phpMyAdmin
      12m 26s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ .

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Join now Already a member? Log in

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.


Mark all as unwatched Cancel

Congratulations

You have completed PHP with MySQL Beyond the Basics.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member ?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferences from the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

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.

Learn more, save more. Upgrade today!

Get our Annual Premium Membership at our best savings yet.

Upgrade to our Annual Premium Membership today and get even more value from your lynda.com subscription:

“In a way, I feel like you are rooting for me. Like you are really invested in my experience, and want me to get as much out of these courses as possible this is the best place to start on your journey to learning new material.”— Nadine H.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.