Jump to content

Getting my feet wet with MVC... I want to pass a mysqli result set


oljoha

Recommended Posts

 

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 */
} ?>

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>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.