从result()函数访问返回值

时间:2014-04-28 07:16:05

标签: php codeigniter

尝试获取查询结果将其发送回控制器并将其返回到视图并在那里访问它。我似乎无法在我的视图中显示该值,因此我尝试回显出控制器的结果。它一直声明未定义的偏移1 ...请告诉我如何正确访问模型返回值

模型

$output = $this->db->query("SELECT * from incoming ORDER BY incomingId LIMIT 20");
return $output->result();

控制器

$data = $this->search_form->searchIdIncoming($searchQuery);
echo $data[0][1];
$this->load->view("searchIncoming", $data);

查看

if(isset($incomingId))
    echo "Primary key is available";

3 个答案:

答案 0 :(得分:0)

尝试

echo $data[0]["Your Database field name"]; instead of echo $data[0][1];

喜欢

echo $data[0]["id"];

答案 1 :(得分:0)

如果要在视图中显示模型的结果,最合适的方法是这样的:

1)将结果记录从模型返回为如下数组:

$output = $this->db->query("SELECT * from incoming ORDER BY incomingId LIMIT 20");
return $output->result();

2)通过调用此模型查询的函数来控制Controller中的这个值(因为你没详细说明,我不确定你使用的是哪个函数),

$data['details'] = $this->search_form->searchIdIncoming($searchQuery);  // store the result in an array 

并将数组传递给视图

$this->load->view("searchIncoming", $data);

3)在视图文件中,您可以使用

显示记录
if(!empty($details))
    {
      foreach($details as $row)
      {
        if($row->incomingId!=0)
          echo "Primary key is available";
        ...........
      }
   }

答案 2 :(得分:0)

我终于通过在codeigniter中使用result_array()函数替换result()函数来添加一些其他东西以正确获取表函数,但这是我得到的结果:

$rows[] = array();
        $rows2[] = array();
        $rows3[] = array();

        $i = 0;
        $companyName = $this->db->query("SELECT id, name from company");
        foreach($companyName->result_array() as $row2){
            $rows2[$i]['id'] = $row2['id'];
            $rows2[$i]['name'] = $row2['name'];
            $i++;
        }
        //get all company names

        $i = 0;
        $staffName = $this->db->query("SELECT id, surname, firstName, middleInitial from employee");
        foreach($staffName->result_array() as $row3){
            $rows3[$i]['id'] = $row3['id'];
            $rows3[$i]['name'] = $row3['surname'].", ".$row3['firstName']." ".$row3['middleInitial'];
            $i++;
        }
        //get all employee names

        $i= 0;
        $output = $this->db->query("SELECT * from incoming ORDER BY incomingId LIMIT 20");
        if ($output->num_rows() > 0) {
        foreach($output->result_array() as $row){
            $count = 0;
            $j = 0;
            $rows[$i]['incomingId'] = $row['incomingId'];
            $rows[$i]['referenceNo'] = $row['referenceNo'];
            $rows[$i]['documentTypeId'] = $row['documentTypeId'];
            $rows[$i]['documentDate'] = $row['documentDate'];
            $rows[$i]['dateReceived'] = $row['dateReceived'];
            $rows[$i]['sender'] = $row['sender'];

            while($count < sizeof($rows2)){
                if($rows2[$j]['id'] != $row['companyId']){
                    $j++;
                }else{
                    $rows[$i]['companyName'] = $rows2[$j]['name'];
                    break;
                }
                $count++;
            }
            $j= 0;
            $count = 0;
            while($count < sizeof($rows3)){
                if($rows3[$j]['id'] != $row['responsibleStaffId']){
                    $j++;
                }else{
                    $rows[$i]['responsibleStaffName'] = $rows3[$j]['name'];
                    break;
                }
                $count++;
            }

            $rows[$i]['subject'] = $row['subject'];
            $rows[$i]['actionDone'] = $row['actionDone'];

            $rows[$i]['track'] = $row['track'];
            $rows[$i]['completed'] = $row['completed'];
            $rows[$i]['remarks'] = $row['remarks'];
            $i++;
        }

        return $rows;
    }
    return false;