2

Sorry for the basic question, but I couldn't fidn the answer anywhere online. I started with codeigniter yesterday And all I am trying to do is selecting data from my database. However I don't get any errors nor does it show any data. If i change the database tabel name. It immediately throws an error.

Model

<?php

    class Customer_model extends CI_Model {

        public function get_customers($id) {
            if ($id != FALSE) {
                $query = $this->db->get_where('customer', array('id' => $id));
            } else {
                return FALSE;
            }
        }

    }

Controller

<?php

    class Customer extends CI_Controller {

        public function show($id) {
            $this->load->model('customer_model');
            $news = $this->customer_model->get_customers($id);

            $data['customer_name'] = $news['customer_name'];
            $data['streetname'] = $news['streetname'];
            $this->load->view('customers', $data);
        }

    }

VIEW

<?php echo "hallo"; ?>
<?php echo $customer_name; ?>

** EDIT, in my page is can see the echo of hello. So I am in the correct page, but there are no errors nor notices of any data missing.

If anyone have any idea what I'm doing wrong, thanks a zillion. btw database connectivity is included and also the package is loaded.

thanks

6

1 Answer 1

4

In Model

class Customer_model extends CI_Model {

    public function get_customers($id) { # refactored 

        $this->db->select(*);            
        $query = $this->db->get_where('customer', array('id' => $id));
        $result = $query->result_array();

        $count = count($result); 

        if(empty($count)){
            return false;
        }
        else{
            return $result;
        }
    }
}

In Controller

class Customer extends CI_Controller {

    public function show($id) {

        if(empty($id)) # Added
        { 
            echo "Token Invalid";
        }
        else{
            $this->load->model('customer_model');
            $news = $this->customer_model->get_customers($id);

            if($news == false){ # Added
                echo "No result Found";
            }
            else{
                $data['customer_name'] = $news[0]['customer_name']; # Changed
                $data['streetname'] = $news[0]['streetname']; # Changed
                $this->load->view('customers', $data);              
            }   
        }
    }
}
2
  • 1
    This is the solution!! but little explanation make it better!!
    – Saty
    Mar 16, 2016 at 13:03
  • Worked like a charm. i was missing some important concepts of the framework, like explicity loading the model class and returning it's value to the controller. Thanks for clearing it up mate. Mar 16, 2016 at 14:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.