2017-01-16 95 views
0

我使用的CodeIgniter,我试图填充<table>,只是获得相同的行4次。 我的模型:foreach返回同一行4次codeigniter

$sql2= 
    "SELECT * 
    FROM puntos 
    WHERE checklist_idchecklist='$idchecklist'"; 
    $qry2=$this->db->query($sql2); 
    $puntos=$qry2->row(); 
    return $puntos 

此查询返回2个阵列的4个属性的每个,我测试它做的phpmyadmin上的SQL查询。

我的控制器:

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次:
And this is what I'm getting, the same row 4 times

回答

0
<table class="table"> 
<tr> 
<th>Descripcion</th> 
<th>Lugar</th> 
<th>Tipo</th> 
<th>Urgencia</th> 
</tr> 
</thead> 
<tbody> 
    <?php 
     echo '<tr>'; 
     foreach($punto as $row) 
     { 
     echo '<td>'.$row.'</td>'; 
     } 
     echo '</tr>'; 
    ?> 
</tbody> 
<table> 
0

在codeigniter row()获取目标中的第一行ct格式。 $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

0

尝试像数组这样

 echo '<tr>'; 
     echo '<td>'.$punto['item_1_name'].'</td>'; 
     echo '<td>'.$punto['item_2_name']..'</td>'; 
     echo '<td>'.$punto['item_3_name']..'</td>'; 
     echo '<td>'.$punto['item_4_name']..'</td>'; 
     echo '</tr>'; 

item_1_name =索引名返回