Reputation: 103
I am going to fetch specific gender from db like gender='male' code is working fine but can't iterate it in foreach why it is
model you can check
class get_all_gender extends CI_Model {
//put your code here
public function select_male() {
$this->db->select('*');
$this->db->from('bride_groom_register');
$this->db->where("gender='male'");
$query = $this->db->get();
if ($query->result() > 0)
{
return $query;
} else {
return FALSE;
}
}
}
and controller i m getting data from database
class Welcome extends CI_Controller {
public function index()
{
$this->load->model('get_all_gender');
$data['res']=$this->get_all_gender->select_male();
$this->load->view('list',$data);
}
}
simple view
foreach ($res as $row)
{
echo $row->name;
}
Upvotes: 0
Views: 104
Reputation: 883
You should return the $query
's result()
instead of $query
itself. Your model should look like this:
class Get_all_gender extends CI_Model {//put your code here
public function select_male() {
$this->db->where("gender","male");
$query = $this->db->get("bride_groom_register");
if ($query->num_rows() > 0) {
return $query->result();
} else {
return FALSE;
}
}
}
You are saving the result in the controller:
$data['res'] = $this->get_all_gender->select_male();
So you can access it like this in the view:
if($res !== FALSE){
foreach ($res as $row)
{
echo $row->name;
}
}
You should check for the FALSE condition, otherwise the view will throw PHP errors when there is no rows returned from the model.
Upvotes: 1
Reputation: 38609
Try this
In Model
class get_all_gender extends CI_Model {
//put your code here
public function select_male()
{
$this->db->select('*');
$this->db->from('bride_groom_register');
$this->db->where("gender='male'");
$query = $this->db->get();
$result = $query->result_array();
if (!empty($result))
{
return $result;
}
else {
return FALSE;
}
}
}
In Controller
class Welcome extends CI_Controller {
public function index()
{
$this->load->model('get_all_gender');
$data['res'] = $this->get_all_gender->select_male();
$this->load->view('list',$data);
}
}
In View
if ($res != FALSE) {
foreach ($res as $value) {
echo $value;
}
}
else {
echo "Seleted Category is Empty";
}
Upvotes: 1
Reputation: 103
Class Name first letter upper case
class Get_all_gender extends CI_Model {//put your code here
public function select_male() {
$this->db->select('*');
$this->db->from('bride_groom_register');
$this->db->where("gender='male'");
$query = $this->db->get();
if ($query->num_rows() > 0) {
return $query->result_array();
} else {
return FALSE;
}
}
}
Upvotes: 0
Reputation:
Try code below on model
Class Name first letter upper case
Filename: Get_all_gender.php
<?php
class Get_all_gender extends CI_Model {//put your code here
public function select_male() {
$this->db->select('*');
$this->db->from('bride_groom_register');
$this->db->where('gender', 'male');
$query = $this->db->get();
if ($query->num_rows() > 0) {
return $query->result_array();
// Or
// return $query->row_array();
} else {
return FALSE;
}
}
}
On View
foreach ($res as $row)
{
echo $row['name'];
}
Upvotes: 0