Today, I got a request from my customer to remove some teachers from a list on our website and to add another. I thought it was a hardcoded list where I simply added the names to an html document. Thankfully, I was wrong.
In the *space where the page lives, I had build a nice simplish php application. The url looks like this , http://studentmoodle.accelerateu.org/student/portal/studentindex.php?action=staff
Note the ?action=staff at the end of the URL. That is a trademark of the old MVC design pattern, where you send the requests through a common page or event (studentindex.php) with an action that tells the page which event to load, ?action=staff.
In studentindex.php, there are *events that looks like this.
include_once('../config.php');
include_once('appWideVars.php');
include_once('DAO.php');
echo "div class='container'>";
echo "div id='wrapper'>";
switch ($action) {
case 'contactus':
require_once('header.php');
require_once('menu.php');
echo "div class='movetoleft'>";
require_once('contactus.php');
echo "/div>";
require_once('footer.php');
break;
case 'staff':Sorry, I had to break the html a little in the post - so it would not try to actually implement it. You can see the intelligent design where I have set a page variable and am using a case statement to evaluate its result. This makes me happy, remembering how to build an application the intelligent way. The page actions is stored in the $action variable. This is set in the included appWideVars.php file.
require_once('header.php');
require_once('menu.php');
echo "div class='movetoleft'>";
require_once('teachers.php');
echo "/div>";
require_once('footer.php');
break;
The code in appWideVars.php that looks for and sets the page action in the $action variable
if(isset($_REQUEST['action'] )){
$action = $_REQUEST['action'];
}else{//initial state
$action = "home";
}
The case I was looking for, 'staff' includes a page called teachers.php. When I opened that page, I see that in fact I have not hardcoded the teacher names into the page but rather had persisted them in a DB somewhere. I knew this based on the include of a file called DAO.php. This is clear evidence of data coming from a DB and not living in the page. DAO.php is a acronym called (data access object). Its job is to handle communication with data persisting in a DB somewhere.
Here is the teachers.php content.
include_once('DAO.php');";
echo 'h3>AccelerateU Teachers h3>';
$result = getTeachers();
//var_dump($result);
echo "table>
tr>
th>Subject/td>
th>Teacher - Email/td>
th>Bio/td>
tr>
";
foreach($result as $i){
echo "tr>";
echo "td>".$i->subjects."
echo "td>a href='mailto:".$i->email."' title='teacher email'>".$i->fname." ".$i->lname."/a>/td>";
echo "td>".$i->bio."/td>";
echo "tr>";
}
echo "table>";
Sorry - I had to break the html in the page. I am calling the getTeachers() method and looping over the result set and using html to show the output.
Notice the include DAO.php statement in the top of the page. When I open the DAO.php file, I can find the getTeachers method.
function getTeachers(){
global $DB;
//echo "test";
return
$DB->get_records_sql('
SELECT idteachers, fname, lname, subjects, email, pic, bio
FROM geniusintegration.teachers
ORDER BY subjects, lname
');
}
This method gets all the records from the geniusintegration.teachers table and returns them to the DAO.php file, which returns them to the teachers.php file
In a nutshell the MVC design pattern in action
model = the DAO.php file
view = teachers.php
controller = studentindex.php
The end.
No comments:
Post a Comment