【问题标题】:Accessing Array Element in php codeigniter does not work在 php codeigniter 中访问数组元素不起作用
【发布时间】:2016-10-21 15:55:33
【问题描述】:

在 codeigniter 中访问数组元素“id”不起作用。找不到失败的可能原因。如果有人可以展示正确的方法会有所帮助,

我的观点

<form name ="userinput" onsubmit="return test()" action="<?php echo base_url(); ?>index.php/patient/saveNewExaminationChart/<?php echo $pd['id']; ?>" method="post">

&lt;?php echo $pd['id']; ?&gt; 对于这部分,我想获取控制器传递的 id 号以查看。

我的控制器

public function editPeriodicalDetails(){

        $pid = $this->uri->segment(3);
        $this->load->model('patient_model');
        $patient['pd'] = $this->patient_model->getPeriodicalChart($pid);


        $this->load->helper('url');
        $this->load->view('Header');
        $this->load->view('Patient/editPeriodicalExam',$patient);
        $this->load->view('Footer');
}

我的模型

public function getPeriodicalChart($id){
        $this->load->database();
        $this->db->select('*');
        $this->db->from('periodical_examination a');
        $this->db->join('patient p','p.id = a.pid');
        $this->db->where('a.pid',$id);

        $query = $this->db->get();
        return $query->result();

    }

【问题讨论】:

  • 您现在有很多解决方案,您可以对其进行测试并将其标记为已接受,否则人们会认为这是一个悬而未决的问题。

标签: php arrays codeigniter


【解决方案1】:

在模型中

return $query->result_array();

在控制器中

$result= $this->patient_model->getPeriodicalChart($pid);
$patient = $result[0]['pd']; # zero indexed array pointer 

【讨论】:

  • 兄弟也可以分享object的其他解决方案
【解决方案2】:

result() 是递归的,因为它返回一个 std 类对象,而 result_array() 只返回一个纯数组,因此 result_array() 将是性能方面的选择。不过速度差别很小。任何有 php 基础知识的人都会发现代码很容易阅读

而不是这行

$query = $this->db->get();
 return $query->result();

(用这个真的很容易理解)

return $this->db->get()->result_array();

【讨论】:

  • 真的好懂怎么办? diff bw result 和 result_array 是什么?
  • result() 是递归的,因为它返回一个 std 类对象,其中 result_array() 只返回一个纯数组,因此 result_array() 将是性能方面的选择。不过速度差别很小。任何有php基础知识的人都会发现代码很容易阅读
  • 在你的答案中添加这个会对其他人有所帮助
【解决方案3】:

它应该是一个对象。试试这个

<?php echo $pd->id; ?>

如果有更多的结果集,那么试试这个

<?php echo $pd[0]->id; ?>

【讨论】:

    【解决方案4】:
    $query->result();
    

    是一个对象而不是一个数组..要返回一个你应该使用的数组

    $query->result_array();
    

    【讨论】:

      【解决方案5】:

      在这里,您在模型中使用了return $query-&gt;result();,并通过控制器将所有表发送到视图。 这是很棒的工作。一切都很好。现在你只需在你的视图页面中使用下面的代码echoid

      查看 (editPeriodicalExam)

      <?php
       foreach($pd as $item){
          echo $item['id'];
       } 
      ?>
      

      【讨论】:

        猜你喜欢
        • 2016-06-09
        • 2014-12-20
        • 1970-01-01
        • 2017-07-24
        • 2010-09-28
        • 1970-01-01
        • 2015-09-25
        • 1970-01-01
        相关资源
        最近更新 更多