V, I've got. Let's do M and maybe I'll end up with a C.
September 13, 2011 2:08 PM Subscribe
I'm learning PHP and MySQL, trying to work from scratch before I ramping up to a framework. I've installed a templating system, just to play with it, so my templating/view logic feels well-organized. But how do I organize the other simple DB query code so it's more MVC-ish?
I'm using a template engine, and I end up with this in my index.php:
//Begin index.php
require_once('config.php');
// Include template (view) class for TinyButStrong
include_once('lib/tbs_class.php');
$TBS = new clsTinyButStrong;
LoadTemplate('templates/'.basename($_SERVER['SCRIPT_NAME'], '.php').'.html');
$site_title = 'Weights and Measures!';
// This would be considered model logic, I think -- (how) do I spin this off into another class file(s) or something?
$sql = 'SELECT * FROM billings';
$cur_month = date("m");
$month_minus_one = date("m") - 1;
$month_minus_two = date("m") - 2;
$cur_month_sql = 'SELECT * FROM billings WHERE MONTH(DATE) = '.$cur_month;
$month_minus_one_sql = 'SELECT * FROM billings WHERE MONTH(DATE) = '.$month_minus_one;
$month_minus_two_sql = 'SELECT * FROM billings WHERE MONTH(DATE) = '.$month_minus_two;
// MergeBlock is sending data from the database to the template engine, so I can reference the data in the template/view with the template engine's tags.
$TBS->MergeBlock('billings, billings2, billingsjs', $Db->GetRows($sql));
$TBS->MergeBlock('billings0', $Db->GetRows($cur_month_sql));
$TBS->MergeBlock('billings1', $Db->GetRows($month_minus_one_sql));
$TBS->MergeBlock('billings2', $Db->GetRows($month_minus_two_sql));
$TBS->Show();
$mysqli->close();
Any advice appreciated. I know people disagree about templating classes vs. plain PHP, and I know that a framework is SO worth it. But this is the path I've chosen for my next exercise and I'd like to see it through. Thanks!
posted by circular to computers & internet (17 answers total) 2 users marked this as a favorite
include_Once("lib/tbssql_mysql.php");
$Db = new clsTbsSQL($srv='',$uid='',$pwd='',$db='',$drv=''); //make the connection
$srv="localhost";
$uid="dbuser";
$pwd="password";
$db="appdatabase";
$drv='';
$table="billings";
$Db->Connect($srv,$uid,$pwd,$db,$drv=''); // $drv is only for certain db's
posted by circular at 2:11 PM on September 13, 2011