首页 > 解决方案 > 一个记录在表中打印两次

问题描述

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

这是我的控制器代码:

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
    }

这是我的模型代码:

  function fetchDue($due,$br){

             $usertype= $this->db->select('user_reg_type')->from('ts_users')->where('user_id',$due)->get()->result_array(); 
             $user = $usertype[0]['user_reg_type'];

             if($user == "consignor"){

          return   $this->db->select('*')
                        ->from('crossing_cash_memo')
                        ->where('lr_from',$br)
                        ->where('consignor_name',$due)
                        ->get()->result();

             }elseif($user == "consignee"){

                $this->db->select('*');
                $this->db->from('crossing_cash_memo');
                $this->db->where('lr_from',$br);
                $this->db->where('consignee_name',$due);

                  $query = $this->db->get();
                  return $query->result();
             }
        }

 function verifyFetchDue($mRNo){
      $query = $this->db->select('*')
               ->from('delivery_due_received')
               ->where('mr_no',$mRNo)
               ->where('g_total=0')
               ->get()
               ->num_rows();
            return $query;
    }


    function verifyRemainingFetchDue($mRNo){
      $query = $this->db->select('*')
               ->from('delivery_due_received')
               ->where('mr_no',$mRNo)
               ->where('g_total != 0')
               ->get()
               ->num_rows();
            return $query;
    }

     function fetchDDRRecord($mr_no){

                    return   $this->db->select('*')
                    ->from('delivery_due_received')
                    ->where('mr_no',$mr_no)
                    ->get()->result_array();
                }

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

你能告诉我我的代码哪里错了吗?

标签: phpcodeigniter

解决方案


您在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
}

推荐阅读