dongzhan5246 2016-10-05 08:38
浏览 77
已采纳

在获取数据库中的值后,在codeigniter 3中获取未定义的索引返回日期?

I am fetching values from database and the values are successfully shown when i use print_r but i get an error when i try to echo the values out it gives me an error of Message:

Undefined index: return_date

my code for controller is as follows

public function latebooks(){
    $now = date('m/d/Y');
    $this->load->model('Time');
    $id=$this->session->userdata('userid');
    $this->load->model('Department');
    $table=$this->Department->selecttable($id);
    foreach($table as $q){}
    $table = $q->department_name;
    $table = strtolower($table);
    $run=$this->Time->timecalculations($table);
    print_r($run);
    $this->load->view('Books/datetime',['query'=>$run]);
}

and my code for my model is as follows:

public function timecalculations($table){
    $query= $this->db->get_where('issue_books',array('department_id'=>$table));
    return $query->result_array();
}

and the code in the view is as follows

    <?php

/**
 * Created by PhpStorm.
 * User: workspace
 * Date: 05-10-2016
 * Time: 13:46
 */
include 'header.php';
?>
<br /><br />
<?php
echo $query['return_date'];
?>
<?php
include 'footer.php';
?>

please forgive as i am novice here!Thanks in advance

  • 写回答

4条回答 默认 最新

  • du0173 2016-10-05 08:43
    关注

    You are using result() function in your MODEL for getting records, this will return data into object form not in array.

    This:

    echo $query['return_date'];
    

    Should be:

    echo $query->return_date;
    

    After your modification in your question, you must need to check what are you getting in print_r($run).

    If you have data in multi-dimensional array than you can use loop here, this will print all data either having one record or multiple.

    <?php
    foreach ($query as $key => $value) {
        echo $value->return_date;
    }
    ?>
    

    If you want to use result_array() function than you can use like:

    <?php
    foreach ($query as $key => $value) {
        echo $value['return_date'];
    }
    ?>
    

    This post will help you to understand both functions: codeigniter, result() vs. result_array()

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 使用VH6501干扰RTR位,CANoe上显示的错误帧不足32个就进入bus off快慢恢复,为什么?
  • ¥15 大智慧怎么编写一个选股程序
  • ¥100 python 调用 cgps 命令获取 实时位置信息
  • ¥15 两台交换机分别是trunk接口和access接口为何无法通信,通信过程是如何?
  • ¥15 C语言使用vscode编码错误
  • ¥15 用KSV5转成本时,如何不生成那笔中间凭证
  • ¥20 ensp怎么配置让PC1和PC2通讯上
  • ¥50 有没有适合匹配类似图中的运动规律的图像处理算法
  • ¥15 dnat基础问题,本机发出,别人返回的包,不能命中
  • ¥15 请各位帮我看看是哪里出了问题