Reputation: 11
Unable to display the data in a CodeIgniter view. Need to print the builders_name in a CodeIgniter view. So far I have the following code:
Model :
public function get_builders()
{
$query1 = $this->db->select('name')->from('builders')->where("name LIKE 'a%'")->limit(3)->get();
$query2 = $this->db->select('name')->from('builders')->where("name LIKE 'b%'")->limit(3)->get();
$result1 = $query1->result();
$result2 = $query2->result();
return array($result1, $result2);
}
Controller:
public function index(){
$this->load->database();
$data['h']= $this->Builders_Model->get_builders();
$this->load->view('home', $data);
}
View:
<?php foreach ($h->result as $row) { ?>
<tr>
<td><a href="#"><?php echo $row->name;?></td></a>
</tr>
<?php } ?>
Upvotes: 1
Views: 4103
Reputation: 1
It's really working model
public function get_builders()
{
$this->db->select('name');
$this->db->from('builders');
$this->db->like('name','a');
$this->db->or_like('name','b');
$this->db->limit(3);
$query = $this->db->get();
return $query->result();
}
controller
public function index(){
$this->load->model('Builder_model');
$data= $this->Builders_Model->get_builders();
$this->load->view('home', ['data'=>$data]);
}
view
<?php foreach ($h->result as $row) { ?>
<tr>
<td><a href="#"><?php echo $row->name;?></td></a>
</tr>
<?php } ?>
Upvotes: 0
Reputation: 27
First this is you haven't loaded the Builder_modal in your controller try to do some thing like
$this->load->model('Builder_model');
after $this->load->database()
;
Next thing in your view you have to echo like
echo $h;
or var_dump($h)
if you have an array
Upvotes: 0
Reputation: 4033
You have to use $h variable in foreach for notation :
<?php
<tr>
<td><a href="#"><?php echo $rowDetail->name; ?></td></a>
</tr>
}
?>
Upvotes: 0
Reputation: 17
you can use :
<?php
foreach($h as $row){?>
<tr>
<td><a href="#"><?php echo $row->name;?></td></a>
</tr>
}?>
Upvotes: 0
Reputation: 246
Don't like this try it
public function get_builders()
{
$query1 = $this->db->select('name')
->from('builders')
->like('name','a')
->or_like('name','b')
->limit(3)->get();
$result = $query1->result();
return $result;
}
views code
<?php foreach ($h as $row) { ?>
<tr>
<td><a href="#"><?php echo $row->name;?></td></a>
</tr>
<?php } ?>
Hope fully works . Thank You
Upvotes: 1
Reputation: 3237
What you pass to the view in Codeigniter is accessible by its name. Just <php echo $h; ?>
and you should be good to go
Upvotes: 0