Few thoughts;
First you have to ask yourself "what is presentation logic?". For me it's not just HTML in a PHP string but also the code responsible for generating it.
In your example that means;
PHP Code:
while($row = $iterator->getCurrent()) {
echo "<p>Book Title: <b>" . $row['BookTitle'] . "</b><br><br>";
$iterator->next();
}
echo "<b><p align=center>This is the End</b></p>";
OK - now to make what you're doing work, you need three logical "entities" for your application; a Model (the Application logic code), a View (the presentation layer) and a Controller which is basically the middle man, connecting the application logic with the presentation logic.
Here goes;
The Controller...
PHP Code:
<?php
// books.php: this is the controller script
require_once('application/init.php');
// Include the application logic Model
require_once('books_model.php');
// Include the presentation layer View
require_once('books_view.php');
$db =& new MyDatabase(DB_DATABASE, DB_SERVER);
$db->connect(DB_USER, DB_PASS);
$booksModel = & new BooksModel($db);
$booksView = & new BooksView($booksModel);
echo ( $booksView->toString() );
?>
The Model....
PHP Code:
<?php
// books_model.php
class BooksModel {
var $db;
var $resultSet;
function BooksModel(& $db) {
$this->db = & $db;
$this->build();
}
function build() {
$sql ="SELECT * FROM test";
$this->resultSet = & $this->db->query($sql);
}
function &getIterator() {
return new QueryIterator($this->resultSet);
}
}
?>
The View
PHP Code:
<?php
// books_view.php
class BooksView {
var $booksModel;
var $html;
function BooksView (& $booksModel) {
$this->booksModel= &$booksModel;
$this->html='';
$this->build();
}
function build() {
$iterator = & $this->booksModel->getIterator();
while($row = $iterator->getCurrent()) {
$this->html .= "<p>Book Title: <b>" . $row['BookTitle'] . "</b><br><br>";
$iterator->next();
$this->html .= "<b><p align=center>This is the End</b></p>";
}
}
function toString() {
return $this->html;
}
}
?>
If you don't like rendering the output in a class, I'd suggest using a class that loads prodecural PHP "View" scripts which contain echo statements, while using output buffering to control what actually gets sent the browser.
Bookmarks