PHP & MySQL by Tom Butler
Author:Tom Butler
Language: eng
Format: epub
Publisher: SitePoint
Published: 2017-10-22T23:00:00+00:00
Updating the Controller to Use the Class
Now that we have the complete DatabaseTable class, let’s use it in the controllers.
Firstly, delete includes/DatabaseFunctions.php. All our functions are now stored inside the class in classes/DatabaseTable.php.
Secondly, let’s update public/jokes.php to use the new class:
<?php try { include __DIR__ . '/../includes/DatabaseConnection.php'; include __DIR__ . '/../classes/DatabaseTable.php'; $jokesTable = new DatabaseTable($pdo, 'joke', 'id'); $authorsTable = new DatabaseTable($pdo, 'author', 'id'); $result = $jokesTable->findAll(); $jokes = []; foreach ($result as $joke) { $author = $authorsTable->findById($joke['authorId']); $jokes[] = [ 'id' => $joke['id'], 'joketext' => $joke['joketext'], 'jokedate' => $joke['jokedate'], 'name' => $author['name'], 'email' => $author['email'] ]; } $title = 'Joke list'; $totalJokes = $jokesTable->total(); ob_start(); include __DIR__ . '/../templates/jokes.html.php'; $output = ob_get_clean(); } catch (PDOException $e) { $title = 'An error has occurred'; $output = 'Database error: ' . $e->getMessage() . ' in ' . $e->getFile() . ':' . $e->getLine(); } include __DIR__ . '/../templates/layout.html.php';
This controller is better. We no longer have to provide the table name and $pdo instance to each of the functions—total, findById and findAll. The functions can each be called on either the $jokesTable variable or the $authorsTable variable to run the relevant query on either table.
Let’s do the same thing with our other controllers.
Here’s the updated deletejoke.php:
<?php try { include __DIR__ . '/../includes/DatabaseConnection.php'; include __DIR__ . '/../classes/DatabaseTable.php'; $jokesTable = new DatabaseTable($pdo, 'joke', 'id'); $jokesTable->delete($_POST['id']); header('location: jokes.php'); } catch (PDOException $e) { $title = 'An error has occurred'; $output = 'Unable to connect to the database server: ' . $e->getMessage() . ' in ' . $e->getFile() . ':' . $e->getLine(); } include __DIR__ . '/../templates/layout.html.php';
And editjoke.php:
<?php try { include __DIR__ . '/../includes/DatabaseConnection.php'; include __DIR__ . '/../classes/DatabaseTable.php'; $jokesTable = new DatabaseTable($pdo, 'joke', 'id'); if (isset($_POST['joke'])) { $joke = $_POST['joke']; $joke['jokedate'] = new DateTime(); $joke['authorId'] = 1; $jokesTable->save($joke); header('location: jokes.php'); } else { if (isset($_GET['id'])) { $joke = $jokesTable->findById($_GET['id']); } $title = 'Edit joke'; ob_start(); include __DIR__ . '/../templates/editjoke.html.php'; $output = ob_get_clean(); } } catch (PDOException $e) { $title = 'An error has occurred'; $output = 'Database error: ' . $e->getMessage() . ' in ' . $e->getFile() . ':' . $e->getLine(); } include __DIR__ . '/../templates/layout.html.php';
This example can befoound in OOP-DatabaseTable
Now that you’re familiar with objects and classes, and you know that repeated code is a very bad thing for a programmer, it’s time to start tidying up these controller scripts.
While making the last few changes, you would have found yourself making similar changes in multiple locations. As I mentioned earlier in this book, the DRY (Don’t Repeat Yourself) principle states that it’s bad practice to have repeated code.
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8296)
Azure Data and AI Architect Handbook by Olivier Mertens & Breght Van Baelen(6716)
Building Statistical Models in Python by Huy Hoang Nguyen & Paul N Adams & Stuart J Miller(6693)
Serverless Machine Learning with Amazon Redshift ML by Debu Panda & Phil Bates & Bhanu Pittampally & Sumeet Joshi(6566)
Data Wrangling on AWS by Navnit Shukla | Sankar M | Sam Palani(6349)
Driving Data Quality with Data Contracts by Andrew Jones(6298)
Machine Learning Model Serving Patterns and Best Practices by Md Johirul Islam(6068)
Learning SQL by Alan Beaulieu(5994)
Weapons of Math Destruction by Cathy O'Neil(5778)
Big Data Analysis with Python by Ivan Marin(5353)
Data Engineering with dbt by Roberto Zagni(4349)
Solidity Programming Essentials by Ritesh Modi(3997)
Time Series Analysis with Python Cookbook by Tarek A. Atwan(3854)
Pandas Cookbook by Theodore Petrou(3567)
Blockchain Basics by Daniel Drescher(3292)
Hands-On Machine Learning for Algorithmic Trading by Stefan Jansen(2905)
Feature Store for Machine Learning by Jayanth Kumar M J(2812)
Learn T-SQL Querying by Pam Lahoud & Pedro Lopes(2794)
Mastering Python for Finance by Unknown(2743)
