I Have A Problem to Fetch Data From Database in Codeigniter .. its gives an Error This is My View:-
<tr>
<td><?php echo $row->Country_id;?></td>
<td><?php echo $row->country_name;?></td>
</tr>
Model:-
//data is retrive from this query
$query = $this->db->get('country');
return $query->result_array();
Controller:-
//load the database
$this->load->database();
//load the model
$this->load->model('select');
//load the method of model
$data['h']=$this->select->select();
//return the data in view
$this->load->view('select_view', $data);
Your Controller
$this->load->database();
//load the model
$this->load->model('select');
//load the method of model
$data['h']=$this->select->get_data(); //You can change the function name
//return the data in view
$this->load->view('select_view', $data);
Your Model
$query = $this->db->get('country');
return $query->result_array();
Your view
Use foreach loop to show your data
<tr>
<td><?php echo $row['Country_id'];?></td>
<td><?php echo $row['country_name'];?></td>
</tr>
you can try this code:
Controller
public function getdata(){
$condition_array = array();
$data = '*';
$result = $this->common->select_data_by_condition('Tablename', $condition_array, $data, $sortby = '', $orderby = 'ASC', $limit = '', $offset = '', $join_str = array());
}
print_r($result);
Model
function select_data_by_condition($tablename, $condition_array = array(), $data = '*', $sortby = '', $orderby = '', $limit = '', $offset = '', $join_str = array()) {
$this->db->select($data);
$this->db->from($tablename);
//if join_str array is not empty then implement the join query
if (!empty($join_str)) {
foreach ($join_str as $join) {
if (!isset($join['join_type'])) {
$this->db->join($join['table'], $join['join_table_id'] . '=' . $join['from_table_id']);
} else {
$this->db->join($join['table'], $join['join_table_id'] . '=' . $join['from_table_id'], $join['join_type']);
}
}
}
//condition array pass to where condition
$this->db->where($condition_array);
//Setting Limit for Paging
if ($limit != '' && $offset == 0) {
$this->db->limit($limit);
} else if ($limit != '' && $offset != 0) {
$this->db->limit($limit, $offset);
}
//order by query
if ($sortby != '' && $orderby != '') {
$this->db->order_by($sortby, $orderby);
}
$query = $this->db->get();
//if limit is empty then returns total count
if ($limit == '') {
$query->num_rows();
}
//if limit is not empty then return result array
log_message('debug', 'fetching data result:' . $this->db->last_query());
return $query->result_array();
}
If you want to access your items like $row->country_name
you need to use result()
not result_array()
.
I Solve My Problem By This minor Change. View:-
<?php foreach($h as $row):?>
<tr>
<td><?php echo $row->Country_id;?></td>
<td><?php echo $row->country_name;?></td>
</tr>
<?php endforeach; ?>
Model:-
//data is retrive from this query
$query = $this->db->get('country');
return $query->result();
Controller:-
$this->load->database();
//load the model
$this->load->model('select');
//load the method of model
$data['h']=$this->select->select();
//return the data in view
$this->load->view('select_view', $data);