oljoha Posted July 22, 2008 Share Posted July 22, 2008 I'm trying to create my own framework based on the MVC pattern. To keep things as clean as possible I wish to keep PHP in the viewer to a bare minimum. And I don't want any HTML in the model... Therefore I want to call the function from the model and pass back the result set to the viewer. Passing a row back is easy. But builing a list ( i.e. <ul><li>$Row</li><li>$Row</li><li>$Row</li></ul> ) is giving me trouble. Does anybody have any ideas about the cleanest way to do this? Ole <?php class Model { var $conn; private $result; private $row; function __construct() { $this->conn = new mysqli(DB_HOST,DB_USER,DB_PASS,DB_DATABASE) or die(mysqli_error($this->conn)); if ( mysqli_connect_errno() ) { echo "Database not available."; exit; } } function __destruct() { $this->conn->close(); } /* Member functions and variables go here */ function displayx() { $result = $this->conn->query('SELECT * FROM my_table'); $rows = ??? $result->close(); return $rows; } /* End of member functions and variables */ } ?> <?php require_once 'model.lib.php'; class View extends Model { /* Member functions and variables go here */ function display() { $rows = ''; $row = ''; global $page_content; $rows = $this->displayx(); $page_content .= "<ul>"; foreach ($rows as $row) { $page_content .= "<li>$Row</li>"; } $page_content .= "</ul>"; } /* End of member functions and variables */ } ?> Quote Link to comment https://forums.phpfreaks.com/topic/116071-getting-my-feet-wet-with-mvc-i-want-to-pass-a-mysqli-result-set/ Share on other sites More sharing options...
ignace Posted July 23, 2008 Share Posted July 23, 2008 what you need are called helpers, view helpers in more particular, they help your view parse difficult content class View { protected $_vars; protected static $_helpers; public function __set($var, $value) {} public function __get($var) {} public function __call($helper, $args) { // check if helper exists, if so load it and store it (static variable $helpers for example) return $helper->$helper($args); // something along these lines.. } } class View_ListHelper { // because a constructor can not return anything, you need a custom method, list() for example public function list($data) {} } in your controller: $this->view->data = $model->getData(); then in your view, assuming your script is included within your view object: <div class="my_list"> <?php echo $this->list($this->data); /* loads the View_ListHelper and executes the list() method which returns a html list */ ?> </div> Quote Link to comment https://forums.phpfreaks.com/topic/116071-getting-my-feet-wet-with-mvc-i-want-to-pass-a-mysqli-result-set/#findComment-597353 Share on other sites More sharing options...
oljoha Posted July 23, 2008 Author Share Posted July 23, 2008 Thanks ignace! Quote Link to comment https://forums.phpfreaks.com/topic/116071-getting-my-feet-wet-with-mvc-i-want-to-pass-a-mysqli-result-set/#findComment-597880 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.