templates - How to correctly pass objects from the controller to the view in PHP? -


for learning purposes, built simple mvc-pattern. in order learn how display content of specific database table in view, wrote simple read-function in model (m_crud.php) so:

<?php  class crud{      private $database;     private $db_table = 'products';      function __construct()     {         global $database;         $this->database = $database;     }       // read (select)      public function read(){          $query="select * $this->db_table";          $result= $this->database->query($query);          $num_result=$result->num_rows;           if($num_result>0){             while($rows=$result->fetch_assoc()){                  $this->data[]=$rows;                  //print_r($rows);                    }              return $this->data;         }            } } 

the database details stored in init.php, crud-object initiated well:

<?php  // connect database  $server = 'localhost'; $user   = 'root'; $pass   = 'mypassword'; $db     = 'mydatabase;  $database = new mysqli($server, $user, $pass, $db);   // error reporting  mysqli_report(mysqli_report_error); ini_set('display_errors', 1);   // include objects  include('app/models/m_crud.php'); include('app/models/m_template.php');   // create objects  $crud = new crud(); // creates instance of crud class   // start session  session_start(); 

my controller (that's part stuck) looks follows:

<?php include('app/init.php');  $template->load('app/views/v_members.php');  $obj = $crud->read();      if ( ! empty($obj))     {      // pass product data view     // how correctly pass object data view?       } 

at moment, instantiate crud-object within view display content ofy 'products'-table, works, breaks mvc pattern:

<?php include("includes/private_header.php"); ?>  <div id="content">  <?php $obj=new crud; $obj->read(); ?>  <table width="500" border="1" cellpadding="5">   <tr>     <th width="16" scope="row">id</th>     <td width="95">category</td>     <td width="95">name</td>     <td width="140">description</td>     <td width="104">price</td>     <td width="71">image</td>     <td>action</td>   </tr>    <?php foreach($obj->data $val){     extract($val);      ?>     <tr>     <td scope="row"><?php echo $id; ?></td>     <td><?php echo $category_id; ?></td>     <td><?php echo $name; ?></td>     <td><?php echo $description; ?></td>     <td><?php echo $price; ?></td>     <td><?php echo $image; ?></td>      <td><a href="edit.php?id=<?php echo $id; ?>">edit</a>|<a href="delete.php?id=<?php echo $id; ?>">delete</a></td>   </tr>     <?php }  ?> </table>   </div>  <?php include("includes/private_footer.php"); ?> 

how can pass crud-object controller view in order clean view?

set variables echoed in view in if block of controller. assuming "$template->load('app/views/v_members.php');" calls view, place after if block. otherwise, include controller @ top of view script.

$obj = $crud->read();  if ( ! empty($obj)) {  foreach($obj->data $val){  extract($val); }   } //if template->load function calling view $template->load('app/views/v_members.php'); 

Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -