PHP & MySQL by Tom Butler

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



Copyright Disclaimer:
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.