Permalink
Browse files

Cleaning + Update

Remove old functions and unnecessary comments
  • Loading branch information...
atomicframeworks committed Jan 6, 2012
1 parent adb3ab7 commit 9531dda08d9d055d7f1896fbfe561c8be0643e4c
@@ -1,5 +1,5 @@
<?php
-
+ //// Class to connect to a model and store data
class Controller {
public $model;
public $data;
@@ -10,19 +10,17 @@ public function __construct($args) {
$this->$key = $item;
}
}
- //// Include the ( class . view ) action.php script
- //require_once();
+ // Create new model for the controller
$controller = $this->model = new Model($args);
-
}
-
-
+
+ //// Pass statement to model
public function addStatement($statement){
$this->model->addStatement($statement);
return $this;
}
- // Execute all Statements in array and passed
+ //// Execute all statements in array and passed
public function invoke($statements = array ()){
if (!empty($statements)){
// If array loop through each statement
@@ -39,58 +37,35 @@ public function invoke($statements = array ()){
return $this;
}
-
- //// Fetch all results from statements
+ //// Fetch all results from executed statements
public function fetchAll($fetchStyle = PDO::FETCH_CLASS){
$this->data = $this->model->fetchAll($fetchStyle);
return $this;
}
-
- //// Execute only Statements passed
- public function executeStatements($statements = array ()){
- if (!empty($statements)){
- // If array loop through
- if(is_array($statements)){
- foreach($statements as $statement){
- $this->addStatement($statement);
- }
- }
- else{
- $this->addStatement($statements);
- }
- }
- return $this;
- }
-
- public function setView($view_str = ''){
- $this->view = new $this->model.$this->view;
- return $this;
- }
-
-
+ // Begin transaction with model's PDO MySQL connection. Statements execute only on commit.
public function beginTransaction(){
$this->model->beginTransaction();
return $this;
}
+ // Commit transaction to model's PDO MySQL connection.
public function commitTransaction(){
$this->model->commitTransaction();
return $this;
}
-
public function displayView(){
+ // Set controller scope
$controller = $this;
+ // Set data scope
$data = $controller->data;
+ // Load view html template
$viewFile = ROOT . DS . 'application' . DS . 'views' . DS . $this->database. DS .$this->view.'.php';
+ // Include it
if(file_exists($viewFile)){
include($viewFile);
}
- }
-
-
+ }
}
-
-
?>
@@ -1,5 +1,5 @@
<?php
- // Connect to mysql database and return PDO handle
+ //// Connect to mysql database and return PDO handle
function modelConnect($dbname = '', $user = 'root', $pass = '',$host = 'localhost'){
//START DB connection try block
$dbh = false;
@@ -1,4 +1,5 @@
<?php
+ //// Class to build and store complex MySQL queries
class ModelStatement {
public $statement_str;
public $select_str;
@@ -27,8 +28,6 @@ public function __construct($args = array ()){
}
//// SELECT
-
-
if (!empty($this->select_str)){
// Explode multiple select fields and stagger with as fields
$this->select_str = 'SELECT '.$this->select_str;
@@ -74,16 +73,12 @@ public function __construct($args = array ()){
}
else{
}
-
- //echo "Statement: ".$this->statement_str;
- //echo '<br/>';
+ //echo "Statement: ".$this->statement_str." <br/>";
}
public function __toString(){
return (string) $this->statement_str;
- }
-
-
+ }
}
?>
@@ -1,41 +1,32 @@
<?php
- // MySQL database connection
- //require_once (ROOT . DS . 'includes' . DS . 'DBConnect.inc.php');
- //include_once('includes/DBConnect.inc.php');
- //include_once('includes/DBStatement.class.php');
-
+ //// Class model base to hold and represent MySQL database connection
class Model {
public $model;
public $host = 'localhost';
public $statementArray = array ();
-
//// Contstruction
public function __construct($args = array ()) {
-
if (!empty($args)){
foreach ($args as $key=>$item){
$this->$key = $item;
}
}
-
- // Get PDO handle
- $this->model = modelConnect($this->database,$this->username,$this->password,$this->host);
-
+ // Connect to DB and get PDO handle
+ $this->model = modelConnect($this->database,$this->username,$this->password,$this->host);
}
-
+ //// Execute all statements in statementArray
public function invoke(){
-
+ // Loop statements and execute
foreach ($this->statementArray as $statement){
try {
$statement->execute();
}
catch(PDOException $e) {
// Rollback changes to database on fail
-
try {
$this->model->rollback();
}
@@ -46,39 +37,34 @@ public function invoke(){
}
}
}
-
-
+
//// Prepare & add statement string to object statement array
public function addStatement($statement = ''){
if (!empty($statement)){
$statement = new modelStatement($statement);
}
-
- //// Prepare the statement
+ // Prepare the statement
try {
$preparedStatement = $this->model->prepare($statement);
- //// Add to models statement array
+ // Add to models statement array
array_push($this->statementArray, $preparedStatement);
}
catch(PDOException $e) {
-
echo $e->getMessage();
}
-
return $this;
}
- //// Fetch all results from statements push to array and return
+ //// Fetch all results from statements. Push each result to array and return
public function fetchAll($fetchStyle = PDO::FETCH_CLASS){
$return = array ();
if (!empty($this->statementArray)){
foreach ($this->statementArray as $statement){
try {
- $retAr = array ('data'=>'data','statement'=>'stmt');
- $statementReturn = $statement->fetchAll($fetchStyle);
- $retAr['data'] = $statementReturn;
- $retAr['statement'] = $statement->queryString;
+ $statementData = $statement->fetchAll($fetchStyle);
+ $statementString = $statement->queryString;
+ $returnArray = array ('data'=>$statementData,'statement'=>$statementString);
array_push($return, $retAr);
}
catch(PDOException $e) {
@@ -88,7 +74,6 @@ public function fetchAll($fetchStyle = PDO::FETCH_CLASS){
}
}
}
-
return $return;
}
@@ -115,5 +100,5 @@ public function commitTransaction(){
return $this;
}
}
-
+
?>
@@ -1,6 +1,6 @@
<?php
- class proxy {
+ class Proxy {
public $id;
public $ip;
@@ -15,5 +15,4 @@ public function __construct($args = array()){
}
}
-
?>
@@ -1,3 +0,0 @@
-<?php
-
- echo 'Proxy class loaded <br/>';
@@ -22,7 +22,7 @@
<form method="get" action="./show">
- Query: <input type="text" size="50" maxlength="50" name="query" value='select * from proxies where country = "China"'> <br />
+ Query: <input type="text" size="100" maxlength="100" name="query" value='select INET_NTOA(ip) as ip, id, port, http_code, last_update from proxies where country = "China"'> <br />
<input type="submit" value="query">
</form>
View
@@ -3,9 +3,7 @@
//// Global config vars
define('DEVELOPMENT_ENVIRONMENT',true);
- define('DB_NAME', 'proxy');
- define('DB_USERNAME', 'kevin');
- define('DB_PASSWORD', 'f00d');
+ define('DB_USERNAME', 'user');
+ define('DB_PASSWORD', 'pass');
define('DB_HOST', 'localhost');
-
View
@@ -2,11 +2,11 @@
//// Globals
- global $controller;
define('DS', DIRECTORY_SEPARATOR);
define('ROOT', dirname(__FILE__));
//// Load up our bootstrap (which loads config & shared files)
require_once (ROOT . DS . 'library' . DS . 'bootstrap.php');
+
Oops, something went wrong.

0 comments on commit 9531dda

Please sign in to comment.