duanboniao5903 2015-09-07 10:11
浏览 57
已采纳

从数组中获取数据

Hello I am new to codeigniter framework, and I need help do get some data from array that is forwarded to my view. In my controller's method I was load model who returns me data array from db, and than I pass that data to my view, and that looks like this:

 function deleteAccount($data){
    $this->load->model('model_admin');
    $dataFromModel = array();
    if($query = $this->model_admin->retrieveAccount ($data)){
        $dataFromModel['records'] = $query;
        $this->load->view('admin/test',$dataFromModel);
    }  else {
        echo "No data was returned";
    }

}

and in my view i manage to retrieve that data and to echo it like this:

<?php
echo "TEST page<br/>";


if(isset($records)) {
    foreach ($records as $row) {

        echo $row->username;    
    }
}  else {
    echo "No data";
}
?>

But I am certain that if I retrieve data from db it is either one line or no lines at all, so in my view page i would like to echo that data without foreach loop on some way, for example like this:

echo $records->username;

or:

echo $records[0]; //because it is the first argument in line

but in both ways it throws an error :

1 - first case: Trying to get property of non-object

2 - second case: Object of class stdClass could not be converted to string

This is my model:

function retrieveAccount($data){ 
$query = $this->db->get_where('table_users', array('username' => $data[2]));     

return $query->result(); }

Can someone help me, and show me how to retrieve that data.

  • 写回答

1条回答 默认 最新

  • doq91130 2015-09-07 10:34
    关注
    1. If you want to use $records->username this means $records must contain only one value. In this case instead of using $query->result(), use $query->row() in your model code.

    2. You cannot echo stdClass object. $records[0] is stdClass object thus Php is giving this error. Instead this would work

      echo $records[0]->username

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘