Getting the Basic with PHP-MySql
Design Patterns are general OOP solutions for any computer language, and yet each language has different typical uses. One of the most important uses of PHP is to communicate with MySql. As the “middleman” between HTML and MySql, developers need to juggle three languages well in the HTML< ->PHP< ->MySql sequence and shuffle. All of the OOP work, though, belongs to PHP since neither HTML nor MySql is object oriented. This post is dedicated to the fundamental OOP questions surrounding HTML< ->PHP connections, instantiating class instances and creating re-usable MySql classes. I’d also like to touch on using comments in code and perhaps stir up a little controversy with comments about certain “standards” for comments in PHP.
The first thing to do is to take the program for test drive and look at the code. Run the program by clicking the Play button and click the Download button to get the files.
The HTML Communication
Object oriented programming is all about objects communicating with one another. The first communication is between HTML and the PHP object it first calls. As the user interface (UI), the HTML document must get data from the user and pass it to PHP for processing. In PHP object communication, each object can call the object as a whole or specify a method or property in the other object to perform a specific task. With HTML, we pretty much have to be satisfied with a call to an object (or object trigger) and depend on PHP to carry the ball from that point on. However, if an HTML document has several forms, each form can call a different object, or each form submit button can have a different name. By using the unique name for each submit button a single Client class can determine which operation the user requests. The following HTML is set up for that purpose:
< !doctype html> Display All Data in Table with a new Form
The PHP class uses 4 MySql fields to store and retrieve the information:
|
You may also want to take a quick look at the CSS file that goes with the HTML.
@charset "UTF-8"; /* CSS Document */ /*66635D (gray) E54140 (red) F7EBD5 (cream) 6CC5C1 (aqua) 000000 (black) */ body { background-color: #F7EBD5; font-family: sans-serif; color: #66635D; } a { color: #E54140; text-decoration: none; font-weight: bold; } .warning { color: #E54140; font-weight: bold; } h1 { font-family: "Arial Black", Verdana, Helvetical,sans-serif; background-color: #6CC5C1 ; color: #E54140; text-align: center; } header { background-color: #E54140; color: #E54140; padding:1em; color: #F7EBD5; text-align: center; } .fl { float: left; } div { padding-left: 1em; } h2 { color: #000000; } li { font-weight: bold; } iframe { background-color:#6CC5C1; } |
One thing that may stir the pot (among developers) found in the HTML document is the use of iframe tags. HTML5 retained iframes, and in the jQuery Mobile documentation, you will find the use of iframes. They no longer present a hazard to SEOs; and so I don’t see the harm using them to display PHP output. Besides, I could not find a suitable replacement. (I’m not about to mix PHP and HTML to create a workable hack straight out of the 9th circle of Hell.) So see if you can live with iframes for now, and we’ll get smelling salts for the fainters.
The Client Sorts it Out
When the UI has many options that must be handled by a client object to make the appropriate request from a collection of objects, each with a single responsibility, there’s going to be some kind of conditional statement to sort out which object to use. For lots of good reasons that I won’t go into here, switch/case and if/ifelse statements are to be used sparingly and judiciously in OOP and design pattern programming. An alternative approach would be to add several methods to the Client class and then have individual trigger files launched from the different forms in the HTML document call the appropriate Client method. That’s messy, but because HTML cannot address a class method, that may actually be a more orthodox approach. (Keep those smelling salts handy.)
The basic logic of the approach used is this:
- When a submit button is pressed it can be trapped by the isset() method
- Provide a name for each submit button
- Test for variable being set and not NULL
- Once tested use unset() to remove set state
- Call object of set variable
Now take a look at the Client class to see how that reasoning has been implemented:
< ?php ERROR_REPORTING( E_ALL | E_STRICT ); ini_set("display_errors", 1); function __autoload($class_name) { include $class_name . '.php'; } class Client { //Variable to select the correct class private $task; //Which submit button used? public function __construct() { if(isset($_POST['insert'])) { unset($_POST['insert']); $this->task= new DataEntry(); } elseif(isset($_POST['all'])) { unset($_POST['all']); $this->task= new DataDisplay(); } elseif(isset($_POST['update'])) { unset($_POST['update']); $this->task= new DataUpdate(); } elseif(isset($_POST['kill'])) { unset($_POST['kill']); $this->task= new DeleteRecord(); } } } $worker = new Client(); ?> |
The Client has four operations reflecting the four classes:
- DataEntry()
- DataDisplay()
- DataUpdate()
- DeleteRecord()
That’s all there is to the communication between HTML and the PHP Client class. The Client works (as do all client classes) to make requests. The origin of the request is the user through the HTML UI.
Continue reading ‘Basic PHP-MySql OOP’
Recent Comments