为什么不显示foreach的结果?

时间:2017-11-12 12:22:58

标签: php sql codeigniter

我的模型中有以下查询

public function buscar_horario($date_start, $date_end, $rut_usu){

 $query = $this->db->query("select * from (
 SELECT  hrs_ini ,hrs_ter , lunes, martes, miercoles, jueves, viernes, sabado,
 fecha_registro, id_hr
 FROM horario
 INNER JOIN usuarios ON horario.rut_usu = usuarios.rut_usu
 WHERE usuarios.rut_usu= '$rut_usu' AND date(horario.fecha_ini) BETWEEN date('$date_start') AND date('$date_end') AND 
 fecha_registro = (SELECT MAX(fecha_registro) FROM horario where rut_usu = '$rut_usu' ) ORDER BY id_hr DESC LIMIT 14
 ) tmp order by tmp.id_hr asc  ");

 return $query;

}

我希望在另一个视图中显示结果,因此控制器以这种方式完成

public function buscar_horario(){

$rut_usu = $this->input->get('rut_usu');
$date_start = $this->input->get('date_start');
$date_end = $this->input->get('date_end');

$data['query'] = $this->M_Consultar_Horarios->buscar_horario($rut_usu,$date_start, $date_end);
$this->load->view('usuarios/consultar_horario.php',$data);

}

在视图中,我通过foreach显示它,但它没有显示结果,如果我在数据库中使用相同的参数执行查询,它会正确显示。

foreach($query->result_array() as $row){

$html .= '<tr>
      <td style=" background-color:#f2f2f2;">&nbsp;&nbsp;&nbsp; '.$row->hrs_ini.'</td>
      <td style="text-align:center; background-color:#f2f2f2;">'.$row->hrs_ter.'</td>
      <td style="text-align:center; background-color:#f2f2f2;">'.$row->lunes.' </td>
      <td style="text-align:center; background-color:#f2f2f2;">'.$row->martes.' </td>
      <td style="text-align:center; background-color:#f2f2f2;">'.$row->miercoles.' </td>
      <td style="text-align:center; background-color:#f2f2f2;">'.$row->jueves.' </td>
      <td style="text-align:center; background-color:#f2f2f2;">'.$row->viernes.' </td>
      <td style="text-align:center; background-color:#f2f2f2;">'.$row->sabado.' </td>
      </tr>'; }

搜索参数正确传递,因为我在结果视图中打印它们

1 个答案:

答案 0 :(得分:0)

修复你需要在foreach循环之前定义$ html的方法:

<?php
  $html='';
  foreach($query->result_array() as $row){   
    $html .= '<tr>
                <td style=" background-color:#f2f2f2;">&nbsp;&nbsp;&nbsp; '.$row->hrs_ini.'</td>
              // etc...
             </tr>'; 
  }
?>

并输出如下:

<?php
  echo $html;
?>