一张记录在表中打印两次

时间:2019-03-03 06:04:29

标签: php codeigniter

我是Codeigniter框架的新手,并在其中执行一些查询。
当我从数据库中获取记录时,它会返回双记录。我正在尝试遵循以下代码

这是我的控制器代码:

c.NotebookApp.allow_remote_access=True

这是我的模型代码:

public function fetchDue(){

      $due         = $this->input->post('due_to');
      $br          = $this->session->userdata('user_branch');
      $modelResult = $this->DDModel->fetchDue($due,$br);


      ?>
      <form role="form">
        <div class="row">
        <table id="cash_memo_table" class="table table-bordered table-striped table-sm" style="display:block; overflow: auto; ">

                    <tr>
                      <th>mr_no</th>
                      <th>bilty_no</th>
                      <th>Bilty Date</th>
                      <th>Weight</th>
                      <th>Total</th> 
                      <th>Create/Update</th>
                    </tr>


      <?php foreach ($modelResult as $due) : 

       for($i=0; $i< count($modelResult); $i++){
          $vfDueResult = $this->DDModel->verifyFetchDue($modelResult[$i]->mr_no);
           if($vfDueResult==1){
            ?>

            <?php

          }else if($vfDueResult==0){
              $verifyRemainingFetchDue  = $this->DDModel->verifyRemainingFetchDue($modelResult[$i]->mr_no);
              if($verifyRemainingFetchDue==1){
                $modelResult = $this->DDModel->fetchDDRRecord($modelResult[$i]->mr_no);

                ?>
                    <tr>

                      <td><?php echo $due->mr_no; ?></td>
                        <input type="hidden" value="<?php echo $due->mr_no; ?>" name="mr_no[]">
                      <td><?php echo $due->bilty_no; ?></td>
                        <input type="hidden" value="<?php echo $due->bilty_no; ?>" name="bilty_no[]">
                      <td><?php echo $due->lr_date; ?></td>
                        <input type="hidden" value="<?php echo $due->lr_date; ?>" name="lr_date[]">
                      <td><?php echo $due->lr_actual_weight ; ?></td> 
                        <input type="hidden" value="<?php echo $due->lr_actual_weight; ?>" name="lr_actual_weight[]">
                      <td><input type="hidden" class="cal" name="total[]" value="<?php echo $due->total; ?>"><?php echo $due->total; ?></td>
                      <td><button type="button" class="btn btn-success btn-update" id="update">Update</button></td>
                    </tr> 
                    <?php

                }else{

                   ?>
                    <tr>
                      <td><?php echo $due->mr_no; ?></td>
                        <input type="hidden" value="<?php echo $due->mr_no; ?>" name="mr_no[]">
                      <td><?php echo $due->bilty_no; ?></td>
                        <input type="hidden" value="<?php echo $due->bilty_no; ?>" name="bilty_no[]">
                      <td><?php echo $due->lr_date; ?></td>
                        <input type="hidden" value="<?php echo $due->lr_date; ?>" name="lr_date[]">
                      <td><?php echo $due->lr_actual_weight ; ?></td> 
                          <input type="hidden" value="<?php echo $due->lr_actual_weight; ?>" name="lr_actual_weight[]">
                      <td><input type="hidden" class="cal" name="total[]" value="<?php echo $due->total; ?>"><?php echo $due->total; ?></td>
                      <td><button type="button" class="btn btn-success btn-insert" id="insert">Insert</button></td>
                    </tr> 
            <?php 
               }//else end
          }
       }
      ?>
                </table>
            </div>
        </form> 
    <?php
    }

我正在为此执行某些条件,以验证记录是否存在,如果存在则给我记录,但它也会进入其他部分并打印其他部分。

您能告诉我代码中哪里有问题吗?

1 个答案:

答案 0 :(得分:0)

您在for循环中使用了foreach循环,因此也难怪它返回了双重结果。您可以删除for循环:

public function fetchDue(){

  $due         = $this->input->post('due_to');
  $br          = $this->session->userdata('user_branch');
  $modelResult = $this->DDModel->fetchDue($due,$br);


  ?>
  <form role="form">
    <div class="row">
    <table id="cash_memo_table" class="table table-bordered table-striped table-sm" style="display:block; overflow: auto; ">

                <tr>
                  <th>mr_no</th>
                  <th>bilty_no</th>
                  <th>Bilty Date</th>
                  <th>Weight</th>
                  <th>Total</th> 
                  <th>Create/Update</th>
                </tr>


  <?php foreach ($modelResult as $i => $due) { // <---------------------- changed here
      $vfDueResult = $this->DDModel->verifyFetchDue($modelResult[$i]->mr_no);
       if($vfDueResult==1){
        ?>

        <?php

      }else if($vfDueResult==0){
          $verifyRemainingFetchDue  = $this->DDModel->verifyRemainingFetchDue($modelResult[$i]->mr_no);
          if($verifyRemainingFetchDue==1){
            $modelResult = $this->DDModel->fetchDDRRecord($modelResult[$i]->mr_no);

            ?>
                <tr>

                  <td><?php echo $due->mr_no; ?></td>
                    <input type="hidden" value="<?php echo $due->mr_no; ?>" name="mr_no[]">
                  <td><?php echo $due->bilty_no; ?></td>
                    <input type="hidden" value="<?php echo $due->bilty_no; ?>" name="bilty_no[]">
                  <td><?php echo $due->lr_date; ?></td>
                    <input type="hidden" value="<?php echo $due->lr_date; ?>" name="lr_date[]">
                  <td><?php echo $due->lr_actual_weight ; ?></td> 
                    <input type="hidden" value="<?php echo $due->lr_actual_weight; ?>" name="lr_actual_weight[]">
                  <td><input type="hidden" class="cal" name="total[]" value="<?php echo $due->total; ?>"><?php echo $due->total; ?></td>
                  <td><button type="button" class="btn btn-success btn-update" id="update">Update</button></td>
                </tr> 
                <?php

            }else{

               ?>
                <tr>
                  <td><?php echo $due->mr_no; ?></td>
                    <input type="hidden" value="<?php echo $due->mr_no; ?>" name="mr_no[]">
                  <td><?php echo $due->bilty_no; ?></td>
                    <input type="hidden" value="<?php echo $due->bilty_no; ?>" name="bilty_no[]">
                  <td><?php echo $due->lr_date; ?></td>
                    <input type="hidden" value="<?php echo $due->lr_date; ?>" name="lr_date[]">
                  <td><?php echo $due->lr_actual_weight ; ?></td> 
                      <input type="hidden" value="<?php echo $due->lr_actual_weight; ?>" name="lr_actual_weight[]">
                  <td><input type="hidden" class="cal" name="total[]" value="<?php echo $due->total; ?>"><?php echo $due->total; ?></td>
                  <td><button type="button" class="btn btn-success btn-insert" id="insert">Insert</button></td>
                </tr> 
        <?php 
           }//else end
      }
   }
  ?>
            </table>
        </div>
    </form> 
<?php
}