My table looks like
name column1 column2
p1 2 3
p2 4 6
What I want only to display in my View page is
2
In my Controller page:
public function table(){
$this->load->model('table_model');
$data['value']= $this->table_model->getData();
$this->load->view('table', $data);
}
In my Model page:
public function getData(){
$this->db->select(*);
$this->db->from('table');
$this->db->where("name = 'p1'");
$query = $this->db->get();
return $query->result();
}
In my View page: I tried
<?php echo $value->column1; ?>
but gives me an error
Message: Trying to get property of non-object
$query->result()
returns an array of rows.
If you're looking for just the first row use return $query->row();
or $value[0]->column1
.
Edit for example:
$query->result()
returns:
Array (
[0] => Object (
name => p1
column1 => 2
column2 => 9
)
)
$query->row()
returns:
Object (
name => p1
column1 => 2
column2 => 9
)
if your model using return $query->result()
use <?php echo $value[0]->column1; ?>
<?php echo $value[0]->column1; ?>
if your model using return $query->row()
use <?php echo $value->column1; ?>
<?php echo $value->column1; ?>
Model-
public function getData(){
$this->db->select(*);
$this->db->from('table');
$this->db->where("name = 'p1'");
$query = $this->db->get();
$data = $query->result();
foreach($data as $row){
$value = $row->column1;
}
return $value;
}
view- <?php echo $value; ?>
<?php echo $value; ?>
Controller-
public function table(){
$this->load->model('table_model');
$data['value']= $this->table_model->getData();
$this->load->view('table', $data);
}
Try this code
Model:
public function getData($p1){
$this->db->where('name = p1'); // you can use this or this $this->db->where('name',$p1);
$query = $this->db->get('table');
return $query->result(); // if you use result() you will use in the view is foreach to display the data but if you use row() you can directly call the data i will give example
}
Controller:
public function table(){
$this->load->model('table_model');
$data['value']= $this->table_model->getData();
$this->load->view('table', $data);
}
View:
in using row()
<?=$value->column;?> //try this or
<?=echo $value->column; ?>
in using result()
<?php foreach($value as $val) : ?>
<tr>
<td><?=$val->column;?></td>
</tr>
<?php endforeach; ?>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.