how to get data from database throught model using codeigniter with for each loop

475 views Asked by At

http error occured while calling data from model using function

model

    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;
        }
    }

controller

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

There are 3 answers

0
Abhinav On

First of all, No need to include the curly braces for $q->result

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

No need to use validation_errors in your php file.You can directly load your form page.Use validation_errors() in view page.

In your Controller, do this

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

Then in your formpage you can echo

<?php echo validation_errors(); ?>
1
Abdulla Nilam On

change this line to $this->data['showcombo'] = $this->load->sales_model->getComboProduct();

this

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

Because your

model name is

 public function getProductCombo()
{

}
0
Dilip kumar On

Firstly you load model in controller. And then called function, which you have defined in model..

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