Codeigniter:在Helper中加载模型和控制器结果

时间:2014-01-10 13:01:09

标签: php codeigniter

不知道什么是正确的方法。但我想要做的是为positions列表或下拉列表设置一个辅助函数(将为此设置参数),这样就可以通过编写单个代码`get_positions($)来定位列表/下拉列表对整个系统的可用性。类型)'

问题是没有加载模型/控制器变量。

错误讯息:

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: positions

Filename: helpers/content_helper.php

Line Number: 14

辅助

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');


// Position List
if ( ! function_exists('get_positions'))
{
    function get_positions()
    {
        $CI =& get_instance();
        $CI->load->model('admin/hr/hr_model');

        if(count($positions)):
            echo form_open();

            $array = array();
            foreach($positions as $position):   
                $label[] = $position->label;
                $pos[] = $position->position;
            endforeach;

            echo form_dropdown('positions', array_combine($pos, $label));
            echo form_close();
        endif;
    }
}

模型

<?php (defined('BASEPATH')) OR exit('No direct script access allowed');

class HR_Model extends MY_Model
{

    protected $_table_name      =   'positions';
    protected $_order_by        =   'label ASC';
    public $rules               =   array();


    public function get_new()
    {
        $position = new stdClass();
        $position->position = '';
        $position->label = '';
        return $position;
    }


    public function get_positions($id = NULL, $single = FALSE)
    {
        $this->db->get($this->_table_name);        
        return parent::get($id, $single);
    }

}

控制器

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class HR extends MX_Controller
{

    public function __construct()
    {
        parent::__construct();
        $this->load->model('admin/hr/hr_model');
    }

    public function index()
    {
        // Fetch all pages
        $this->data['positions'] = $this->hr_model->get_positions();        

        // Load view
        $this->load->view('hr/index', $this->data);
    }

}

最终我要看的是通过调用get_positions($type)来创建可用于整个系统的辅助函数

1 个答案:

答案 0 :(得分:1)

您必须在$positions之前定义if (count($positions)):变量。您应该从Model中调用get_positions方法。