如何使用codeigniter为每个循环从模型中获取数据

时间:2015-06-19 08:35:30

标签: codeigniter codeigniter-2 codeigniter-form-helper

使用函数

从模型调用数据时发生

http错误

模型

    public function getProductCombo() {
        $q = $this->db->get_where('products', array('type' => 'combo'));
        if ($q->num_rows() > 0) {
            foreach (($q->result()) as $row) {
                $data[] = $row;
            }
            return $data;
        }
    }

控制器

function sets() {
    $this->sma->checkPermissions();
    $this->load->helper('security');

    $this->data['error'] = (validation_errors() ? validation_errors() :                         
                                                  $this->session->flashdata('error'));

    // problem in this line also
    $this->data['showcombo'] = $this->load->sales_model->getComboProduct();


    $bc = array(array('link' => base_url(),
                      'page' => lang('home')),
                array('link' => site_url('sales'),
                      'page' => lang('products')),
                array('link' =>   '#', 'page' => "sets")
          );
    $meta = array('page_title' => "Add Sets", 'bc' => $bc);
    $this->page_construct('sales/sets', $meta, $this->data);

}

3 个答案:

答案 0 :(得分:0)

首先,不需要包含$q->result

的花括号
foreach ($q->result as $row)
{
  $data[] = $row;
}

无需在php文件中使用validation_errors。您可以直接加载form页面。在视图页面中使用validation_errors()

在您的控制器中,执行此操作

if ($this->form_validation->run() == FALSE)
{
    $this->load->view('myform');
}

然后在formpage echo <?php echo validation_errors(); ?>

onPostRender: function() {
    var _this = this;   // reference to the button itself
    editor.on('NodeChange', function(e) {
        _this.active(editor.formatter.match('h1'));
    })
}

答案 1 :(得分:0)

将此行更改为 $this->data['showcombo'] = $this->load->sales_model->getComboProduct();

$this->data['showcombo'] = $this->load->sales_model->getProductCombo();

因为你的

型号名称是

 public function getProductCombo()
{

}

答案 2 :(得分:0)

首先在控制器中加载模型。然后调用函数,您已在模型中定义..

          $this->load->model('sales_model','sales'); // sales is alias name of model name
          $this->data['showcombo'] = $this->sales->getComboProduct();