【问题标题】:Codeigniter: Simple echo of data from DBCodeigniter:数据库中数据的简单回显
【发布时间】:2012-06-12 04:23:08
【问题描述】:

创建模型以便我可以像这样从控制器调用它的最佳方法是什么?

$this->model_model->function->dbname

我有以下模型,但它是垃圾:

型号:

function systemName()
{

    $query = $this->db->query("SELECT cms_name FROM options");

    return $query->result();
}

更新:

function systemOptions($options)
{   
    $this->db->select($options);

    $query = $this->db->get('options');

    return $query->num_rows();
}

【问题讨论】:

    标签: php sql codeigniter model


    【解决方案1】:

    你为什么想要?为什么不这样称呼呢?:

     $this->model_model->functionname('dbname');
    

    一个完整的模型骨架是这样的:

    <?php
    class Mymodel extends Model
    {
        function get_some_entries($number_rows)
        {
                return $result = $this -> db -> query("SELECT id, name
                                                FROM tablename
                                                LIMIT $number_rows");
        }
    }
    ?>
    

    【讨论】:

    • 那更好,但我只看到了 Array。
    • @JessMcKenzie:我用骨架更新了它。根据需要进行调整。
    • 我已经更新了我的问题,但它似乎只是给了我 id 为什么?
    【解决方案2】:

    最好的方法?您可能可以阅读 CodeIgniter general guide 并根据您的情况进行更改。但基本思想是,您创建模型类,然后从控制器加载。然后只需相应地调用模型函数。例如,

    class Custom_model extends CI_Model {
    
    
    function __construct()
    {
        parent::__construct();
    }
    
    function systemName()
    {
    
        $query = $this->db->query("SELECT cms_name FROM options");
        return $query->result();
    }
    
    ...
    ...
    
    function systemOptions($options)
    {   
        $this->db->select($options);
    
        $query = $this->db->get('options');
    
        return $query->num_rows();
    }
    
    
    }
    
    <?php
    class CustomController extends CI_Controller {
    
    public function __construct()
    {
        parent::__construct();       
        $this->load->model('custom_model', 'fubar');
    }
    
    public function index()
    {
        $result = $this->fubar->systemName('dbname'); 
        print_r ($result);
    }
    
    }
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-03
      • 2013-12-21
      • 2017-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多