我正在使用 CodeIgniter,并且正在尝试填充 <table>
并且只获得相同的行 4 次。
我的模型:
$sql2=
"SELECT *
FROM puntos
WHERE checklist_idchecklist='$idchecklist'";
$qry2=$this->db->query($sql2);
$puntos=$qry2->row();
return $puntos
此查询返回 2 个数组,每个数组有 4 个属性,我在 phpmyadmin 上执行 SQL 查询对其进行了测试。
我的 Controller :
function verpuntos() {
$this->load->model('checklistm');
$puntos=$this->checklistm->obtenerpuntos();
$this->load->view('plantilla/headerguardia');
$this->load->view('checklist/lista', $puntos);
$this->load->view('plantilla/footer');
}
我的看法:
$punto=array(
'descripcion' => $descripcion,
'lugar' => $lugar,
'tipo' => $tipo,
'urgencia' => $urgencia);
<table class="table">
<thead>
<tr>
<th>Descripcion</th>
<th>Lugar</th>
<th>Tipo</th>
<th>Urgencia</th>
</tr>
</thead>
<tbody>
<?php foreach($punto as $row) {
echo '<tr>';
echo '<td>'.$descripcion.'</td>';
echo '<td>'.$lugar.'</td>';
echo '<td>'.$tipo.'</td>';
echo '<td>'.$urgencia.'</td>';
echo '</tr>';
}
?>
<tbody>
</table>
这就是我得到的,同一行 4 次:
最佳答案
在 codeigniter row()
中以对象格式获取第一行。$puntos=$qry2->row();
此语句仅获取所获取的第一行对象 fromat 中的数据。所以不需要使用 foreach 循环。就这样尝试一下..
echo '<tr>';
echo '<td>'.$puntos->descripcion.'</td>';
echo '<td>'.$puntos->lugar.'</td>';
echo '<td>'.$puntos->tipo.'</td>';
echo '<td>'.$puntos->urgencia.'</td>';
echo '</tr>';
更多信息请参见此处 https://www.codeigniter.com/userguide3/database/results.html
关于php - foreach 返回同一行 4 次 codeigniter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41672366/