简体   繁体   English

foreach返回同一行4次codeigniter

[英]foreach returning the same row 4 times codeigniter

I'm using CodeIgniter and I'm trying to populate a <table> and just getting the same row 4 times. 我正在使用CodeIgniter,并且试图填充<table>并仅获得4次相同的行。 My model: 我的模特:

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

This query returns 2 arrays of 4 attributes each, i tested it doing the SQL query on phpmyadmin. 该查询返回2个数组,每个数组包含4个属性,我在phpmyadmin上进行了SQL查询,对其进行了测试。

My 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');
}

My view: 我的观点:

$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>

And this is what I'm getting, the same row 4 times: 这就是我得到的,同一行4次:
这就是我得到的,同一行4次

<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>

In codeigniter row() fetch the first row in object format. 在codeigniter中, row()以对象格式获取第一行。 $puntos=$qry2->row(); This statement fetch only first row of your fetched data in object fromat. 该语句仅从fromat对象中获取已获取数据的第一行。 SO no need to use foreach loop. 因此,无需使用foreach循环。 Just try like this.. 像这样尝试

          echo '<tr>';
          echo '<td>'.$puntos->descripcion.'</td>';
          echo '<td>'.$puntos->lugar.'</td>';
          echo '<td>'.$puntos->tipo.'</td>';
          echo '<td>'.$puntos->urgencia.'</td>';
          echo '</tr>';

For more see here https://www.codeigniter.com/userguide3/database/results.html 有关更多信息,请参见此处https://www.codeigniter.com/userguide3/database/results.html

try like this 这样尝试

       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 = index name of array returned item_1_name =返回的数组索引名称

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM