PHP for Absolute Beginners

Book description

PHP is a server-side scripting language that enables you to develop dynamic sites that engage users in ways that are simply not possible using only HTML and CSS.PHP for Absolute Beginners takes a practical approach to teaching you how to build dynamic content for your website using PHP. You'll quickly get started with practical PHP projects, learning how to build a dynamic image gallery. By the end of the book youll will have developed a personal blog complete with a password protected admin module.

PHP for Absolute Beginners won't swamp you with every detail of the full PHP language up front instead, youll be introduced to a small, versatile subset of PHP and learn to use it creatively to develop dynamic web sites. In the process you will learn to use variables, control structures, functions, and objects to your advantage. You will learn how to plan and create databases and how to organize your PHP scripts beautifully. At the end of the book, you will be a confident PHP user, ready to take the next steps in creating great websites.

Table of contents

  1. Cover
  2. Title
  3. Copyright
  4. Contents at a Glance
  5. Contents
  6. About the Authors
  7. About the Technical Reviewer
  8. Acknowledgments
  9. Introduction
  10. Part I: PHP/MySQL Basics
    1. Chapter 1: Setting Up a PHP Development Environment
      1. Why You Need Apache, MySQL, and PHP
        1. How PHP Works
        2. Apache and What It Does
        3. Storing Info with MySQL
      2. Installing PHP, Apache, and MySQL
        1. Installing XAMPP
        2. Open the XAMPP Control Panel
        3. What If Apache Isn’t Running?
        4. Verify That Apache and PHP Are Running
      3. Choosing a PHP Editor
      4. Creating Your First PHP File
      5. Running Your First PHP Script
      6. Summary
    2. Chapter 2: Understanding PHP: Language Basics
      1. Embedding PHP Scripts
        1. Using echo
        2. What Is a Variable?
        3. Displaying PHP Errors
      2. Creating an HTML5 Page with PHP
        1. Including a Simple Page Template
        2. Returning Values
        3. Including the Template
        4. Commenting Your Code
        5. Avoiding Naming Conflicts
      3. Page Views
        1. Making a Dynamic Site Navigation
        2. Passing Information with PHP
        3. Accessing URL Variables
        4. Using isset( ) to Test If a Variable Is Set
        5. $_GET, a Superglobal Array
        6. Including Page Views Dynamically
        7. Concatenation
        8. Strict Naming Convention
        9. Displaying a Default Page
      4. Validating Your HTML
      5. Styling the Site with CSS
      6. Declaring a Page_Data Class
        1. Classes Make Objects
        2. Highlighting Current Navigation Item with a Dynamic Style Rule
      7. Summary
    3. Chapter 3: Form Management
      1. What Are Forms?
      2. Setting Up a New PHP Project
        1. Seeing for Yourself
        2. Creating a Dynamic Navigation
        3. Creating Page Views for the Form
        4. Spending Your Time Wisely: Conventions and Reuse
      3. A Super-Simple Search Form
        1. Trying Your Search Form
        2. Forms Encode URL Variables
      4. Named PHP Functions
        1. The Basic Syntax for Functions
        2. Using Function Arguments for Increased Flexibility
      5. Creating a Form for the Quiz
        1. Showing the Quiz Form
        2. Evaluating the Quiz Response
      6. Curly’s Law: Do One Thing
        1. Meaningful Names
      7. Styling Forms
      8. Exercises
      9. Summary
    4. Chapter 4: Building a Dynamic Image Gallery with Image Upload
      1. Setting Up a Dynamic Site
        1. Prerequisites: A Folder with Some Images
        2. Creating a Navigation
        3. Creating Two Dummy Page View Files
        4. Creating the Index File
        5. Time to Test
      2. Adding Two Style Sheets to One Page
        1. Staying DRY
        2. Improving the Page_Data Class with a Method
        3. Is It a Function or a Method?
        4. What Is $this?
        5. Using the New Method
        6. You Can Only Use Methods That Are Declared
      3. Preparing a Function for Displaying Images
        1. Iteration
        2. Using a DirectoryIterator to Find Files in a Folder
        3. Showing All Images
      4. Creating a Form View
        1. Showing a Form for Uploading Images
        2. $_FILES
      5. Uploading Files with PHP
        1. Planning an Uploader Class
        2. Using the Uploader Class
        3. The Single Responsibility Principle
      6. Summary
    5. Chapter 5: Spicing Up Your Image Gallery with JavaScript and CSS
      1. Client-Side vs. Server-Side Programming
      2. Coding a Lightbox Gallery
        1. Embedding an External JavaScript File
        2. Preparing the Page_Data Class for JavaScript Files
        3. Preparing the Page Template for JavaScript Files
        4. Writing and Running an External JavaScript File
        5. Using window.console.log( )
        6. JavaScript Arrays
        7. Simple Progressive Enhancement
        8. Creating Markup for the Overlay and Big Image
        9. Showing the Overlay
        10. Hiding the Overlay and Resize Thumbnails
        11. Showing a Big Image
        12. Hiding the Big Image
        13. Using a CSS Animation
        14. Coding Challenge
      3. Summary
    6. Chapter 6: Working with Databases
      1. The Basics of MySQL Data Storage
      2. Manipulating Data with SQL
      3. Developing a Database for the Poll
        1. Creating a Database Using CREATE
        2. The INSERT Statement
        3. The SELECT Statement
        4. The UPDATE Statement
      4. Coding a Database-Driven Site Poll
        1. Separating Concerns with MVC
        2. Creating the Poll Project
        3. Making a Poll Controller
        4. Making a Poll Model
        5. Making a Poll View
        6. Hooking Up Poll View with Poll Model
        7. Coding Is Like Playing the Blues
        8. Connecting to MySQL from PHP
        9. Using Constructor Arguments
        10. Sharing the Database Connection with the Poll Model
        11. Retrieving Data with a PDOStatement
        12. PDO and PDOStatement Objects
        13. Showing a Poll Form
        14. Updating a Database Table According to Form Input
      5. Summary
  11. Part II: A Blogging System
    1. Chapter 7: Building the Entry Manager
      1. Creating the blog_entry Database Table
      2. Planning the PHP Scripts
      3. Creating the Blog Site
        1. Creating the Entry Manager Navigation
        2. Loading Admin Module Controllers
        3. Creating the Entry Input Form
        4. Styling the Editor
        5. Connecting to the Database
        6. Using Design Patterns
        7. Writing the Entry_Table Class
        8. Processing Form Input and Saving the Entry
      4. Summary
    2. Chapter 8: Showing Blog Entries
      1. Creating a Public Blog Front Page
        1. Creating a Blog Controller
        2. Getting Data for All Blog Entries
        3. Preparing a View for All Blog Entries
        4. Hooking Up View and Model
        5. Responding to User Requests
        6. Getting Entry Data
        7. Creating a Blog View
        8. Displaying an Entry
        9. Code Smell: Duplicate Code
        10. Using the Private Access Modifier
      2. Summary
    3. Chapter 9: Deleting and Updating Entries
      1. Creating a Model for Administrative Links
        1. Displaying Administrative Links
        2. Populating Your Form with the Entry to Be Edited
        3. Handling Entry Deletion
        4. Deleting Entries from the Database
        5. Responding to Delete Requests
        6. Preparing a Model to Update Entries in the Database
        7. Controller: Should I Insert or Update?
        8. Communicating Changes
        9. Insisting on a Title
        10. Improving Editor Usability with Progressive Enhancement
        11. Embedding Your External JavaScript
        12. Showing a Warning If Title Is Empty
        13. Other Usability Flaws
        14. A Coding Challenge: Fix a Usability Flaw
      2. Summary
    4. Chapter 10: Improving Your Blog with User Comments and Search
      1. Building and Displaying the Comment Entry Form
        1. A Combined View
        2. Creating a Comment Table in the Database
        3. Using a Foreign Key
        4. Building a Comment_Table Class
        5. Staying DRY with Inheritance
        6. Is-a Relationships
        7. Using Inheritance in Your Code
        8. Inserting New Comments into the Database
        9. Retrieving All Comments for a Given Entry
        10. Inserting a Comment Through the Comment Form
        11. Practice Makes Perfect
      2. Searching for Entries
        1. The Search View
        2. Responding to a User Search
        3. The Search Model
        4. A Search Result View
        5. Loading a Search Result View from the Controller
        6. Exercise: Improving Search
      3. Summary
    5. Chapter 11: Adding Images to Blog Entries
      1. Problem: Cannot Delete an Entry with Comments
        1. Understanding Foreign Key Constraints
        2. Deleting Comments Before Blog Entry
      2. Improving Usability with WYSIWYG
        1. Integrating TinyMCE
      3. Creating an Image Manager
        1. Showing a Form for Uploading Images
        2. A Quick Refresher on the $_FILES Superglobal Array
        3. Uploading an Image
        4. What Could Possibly Go Wrong?
      4. Displaying Images
        1. Deleting Images
      5. Using an Image in a Blog Entry
        1. Improving Editor Usability
      6. Summary
    6. Chapter 12: Password Protection
      1. Creating an admin_table in the Database
        1. Encrypting Passwords with MD5
        2. One-Way Encryption
        3. Sufficient Security
        4. Adding Administrators in the Database
      2. Building an HTML Form
      3. Saving New Administrators in the Database
      4. Planning Login
        1. Creating a Login Form
        2. Hiding Controls from Unauthorized Users
        3. Logging In a User
        4. Logging Users Out
        5. Allowing Authorized Users Only
        6. Exercises
      5. Summary
    7. Chapter 13: Going Public with Your Blog
      1. Web Host Requirements
      2. Exporting and Importing Your Database
      3. Preparing Your PHP Files for Upload
        1. Uploading Files with FileZilla FTP
      4. Summary
  12. Index

Product information

  • Title: PHP for Absolute Beginners
  • Author(s): Thomas Blom Hansen, Jason Lengstorf
  • Release date: August 2014
  • Publisher(s): Apress
  • ISBN: 9781430268147