In case of the URL /routes/view/ it maps to the controller "routes" and the action "view". I know the code is quite messy, but as a start I would like to know if the general direction is ok. I have the controller with the main actions. The controller sets the view. The view instantiates the model and gets the data from the model, put it in the template.
So the controller:
Code: Select all
<?php
include $_SERVER['DOCUMENT_ROOT'] . ('/app/views/routes_view.php');
class routes {
var $response;
function routes(&$locator){
$this->response =& $locator->get('Response');
}
function run(&$locator) {
$content = 'bla bla ';
$this->response->setContent($content);
}
function view(&$locator){
$view = new routesview();
$content = $view->viewroutes(3); // 3 is hardcoded for now
$this->response->setContent($content);
}
function insert($locator){}
function edit(&$locator){}
}
?>Code: Select all
<?php
require_once('/app/models/routeclimb.php');
require_once('A/Template.php');
class routesview {
public $template;
public function __construct()
{
$this->model = new RouteClimbModel();
$this->template = new A_TemplateInclude('tpl/routes_view.php');
}
public function viewroutes($id)
{
// get the data from the model
$routes = $this->model->listRouteClimbs($id);
$html = '';
if($routes){
// construct the HTML for the main content from the data I got from the model
$html = '';
foreach ($routes as $key => $value){
$html .= '<ul>';
$html .= '<li>Climb_id is: ' . $value['climb_id'] . '</li>'
. '<li>Climb_notes: '. $value['climb_notes'] . '</li>';
$html .= '</ul>';
}
} else {
$html = 'Currently no routes';
}
// render the template and content
$this->template->set('maintitle', 'This is the main title');
$this->template->set('subtitle', 'This is the subtitle');
$this->template->set('maincontent', $html);
echo $this->template->render();
}
public function insertroutes(){ }
}Code: Select all
<?php
include_once $_SERVER['DOCUMENT_ROOT'] . ('/lib/DBPDO.php');
class RouteClimbModel {
public $climb_id;
public $route_id;
public $user_id;
public $climb_date;
public $climb_style;
public $climb_tries;
public $climb_value;
public $climb_notes;
public $climb_dateposted;
public function __construct(){
$this->conn = DBpdo::conn();
}
public function save(){ }
public function update(){ }
public function listRouteClimbs($userid){
$stmt = $this->conn->prepare( "SELECT * FROM mc_routeclimbs WHERE user_id = :user_id" );
$stmt->bindParam( ":user_id", $userid );
$rs = $stmt->execute();
if ($rs) {
$result = array();
while( $row = $stmt->fetch() ) {
$result[] = $row;
}
return $result;
}
else
{
trigger_error('DB error: ' . $this->conn->errorMsg());
}
}
}Code: Select all
<html>
<body>
<p><a href="/">Home</a> | <a href="/training/">Training</a> | <strong><a href="/routes/">Routes</a></strong> | <a href="/boulders/">boulders</a></p>
<p><strong><a href="/routes/view/">View</a></strong> | <a href="/routes/edit/">edit</a> | <a href="/routes/insert/">insert</a></p>
<p>You are here: Front controller: Routes! Action:view</p>
<h1><?php echo $this->data['maintitle'];?></h1>
<h2><?php echo $this->data['subtitle'];?></h2>
<div id="maincontent">
<?php echo $this->data['maincontent'];?>
</div>
</body>
</html>The view also must get access to extra parameters somehow (/view/2007/10/ for example) and I'll have to add authentication (so that you only view your own routes).