render the values of controllers to view

后端 未结 2 1452
长情又很酷
长情又很酷 2021-01-27 13:21

I am newbie to class and objects.Here I am making an application for invoice.So for the controllers as in the frameworks I have taken them as handlers.So for the UserHandler I

相关标签:
2条回答
  • 2021-01-27 14:11

    Are you using a particular framework or have you rolled your own? Zend Framework for instance would be like this.

    <?php
    class UserHandler extends Zend_Controller_Action {
      public $db;
      public function __construct($db) {
        $this->db = $db;
      }
      public function index() {
        $this->view->data = $this->db->query("SELECT * FROM nt_user"));
      }
     }
    

    And then in the view

    <?php 
        //view.phtml
        var_dump($this->data);
    

    This will give you the array dump that you have in your post. I'm not going to do the looping and displaying stuff though, I'm sure you can work that out yourself.

    0 讨论(0)
  • 2021-01-27 14:15

    You can create a static class using magic methods __get and __set to the the values like:

    class Data {
    
        public static $atributos = array();
    
        public function __get($chave) {
            return array_key_exists($chave, $this->atributos) ? $this->atributos[$chave] : NULL;
        }
    
        public function __set($chave, $valor) {
            $this->atributos[$chave] = $valor;
        }
    
    }
    

    To set a value, you use: Data::__set('view_data', $my_var);

    On the view you just do: Data::__get('view_data');

    I think it will work =)

    0 讨论(0)
提交回复
热议问题