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 使用ESP8266连接阿里云出现问题
  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角