Reputation: 927
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
Upvotes: 2
Views: 5975
Reputation: 38584
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);
}
}
}
}
Upvotes: 4