繁体   English   中英

对于相同的值,for 循环一次又一次地进行

[英]For loop is going again and again for the same values

我不确定出了什么问题。 但是系统一次又一次地打印相同的值,它应该只在传递表中与 student_id 匹配的地方回显该值一次。 我已按要求编辑的完整代码。 这不是在执行查询,而是能够显示表标题。

代码是:

    <?php $min = $this->db->get_where('academic_settings' , array('type' =>'minium_mark'))->row()->description;?>
<?php $running_year = $this->db->get_where('settings' , array('type' => 'running_year'))->row()->description; ?>

<div class="content-w">
      <div class="conty">
          <?php include 'fancy.php';?>
          <div class="header-spacer"></div>
     <div class="content-i">
    <div class="content-box">
            <div class="row">   

                                                                                          
                                                    <div class="table-responsive">
                                                    <table width="100%" class="table table-striped table-lightfont">
<tbody>

 
                <tr>
                    <td style="color:black !important; text-transform:uppercase; text-align:center;">
                    <?php echo get_phrase('Subject_Name');?>&nbsp;:&nbsp;
                        <span><?php echo $row['name'];?></span>
                    </td>
                </tr>
                <tr>
                    <td>
                                <table class="table table-padded">
                                <thead style="background-color:#90be2e; color:white;">
                                    <tr style="padding-bottom:4px; padding-top:4px;">
                                <th style="color:white;"><?php echo get_phrase('Publish Date');?></th>
                                <th style="color:white;"><?php echo get_phrase('Assignment');?></th>
                                <th style="color:white;"><?php echo get_phrase('Faculty');?></th>
                                <th style="color:white;"><?php echo get_phrase('Last Date');?></th>
                                <th style="color:white;"><?php echo get_phrase('Submitted On');?></th>
                                <th style="color:white;"><?php echo get_phrase('Marks');?></th>
                                <th style="color:white;"><?php echo get_phrase('Feedback');?></th>
                                    </tr>
                                </thead>
                            <tbody>
                            
<?php 
     $uploader_id_student = $this->db->get_where('student', array('student_id' => $this->session->userdata('login_user_id')))->row()->student_id;
     $invoices = $this->db->get_where('deliveries', array('student_id' => $uploader_id_student))->result_array(); 
                
                foreach($invoices as $row2): 
 ?> 
                    <tr>
                        <td>
                            <?php echo $this->db->get_where('homework' , array('homework_code'=>$row2['homework_code']))->row()->upload_date;?> 
                        </td> 
                        <td>
                            <?php 
                                $get_homework_data = $this->db->get_where('homework' , array('homework_code'=>$row2['homework_code']))->row();
                                echo wordwrap($get_homework_data->title,15,"<br>\n");
                               ?>
                        </td>
                        <td>
                            <?php 
                                echo $this->db->get_where('teacher' , array('teacher_id'=>$get_homework_data->uploader_id))->row()->first_name;
                            ?>&nbsp; 
                            <?php 
                                echo $this->db->get_where('teacher' , array('teacher_id'=>$get_homework_data->uploader_id))->row()->last_name;
                             ?>                         
                        </td>
                        <td>
                            <?php 
                                $sampleDate1 = $this->db->get_where('homework' , array('homework_code'=>$row2['homework_code']))->row()->date_end;
                                $convertDate1 = date("d-m-Y", strtotime($sampleDate1));
                                echo $convertDate1;

                            ?>                          
                         </td>
                         <td>
                            <?php
                                $sampleDate = $row2['date'];
                                $convertDate = date("d-m-Y", strtotime($sampleDate));
                                echo $convertDate;
                            ?>
                        </td> 
                        <td style="text-align:center;">
                            <?php echo $row2['mark'];?>
                        </td> 
                        <td>
                            <?php echo wordwrap($row2['teacher_comment'],25,"<br>\n");?>
                        </td> 

                  </tr>
                            
            <?php endforeach;?>
                                                        
    <!--<?php endforeach;?> -->
                            </tbody>
                            </table>
                </td>
                        
            </tr>               
                                                    </tbody>
                                                </table>

                                         </div>
                                       
                                            </div>
                                            </div>
                                                </div>  
                                            </div>
                                            
                                               
        </div>

附件是表格Deliveries的截图有没有Deliveries图片下载

这是您更新的代码,以修复 Arnold Daniels 提到的性能问题。

<?php
$uploader_id_student = $this->session->userdata('login_user_id');
$invoices = $this->db->get_where('deliveries', array('student_id' => $uploader_id_student))->result_array(); 
foreach($invoices as $row2) {
  $get_homework_data = $this->db->get_where('homework' , array('homework_code'=>$row2['homework_code']))->row();
  $teacher = $this->db->get_where('teacher' , array('teacher_id'=>$get_homework_data->uploader_id))->row();
?>
  <tr>
    <td>
      <?= $get_homework_data->upload_date ?> 
    </td> 
    <td>
      <?= wordwrap($get_homework_data->title,25,"<br>\n") ?>
    </td>
    <td>
      <?= $teacher->first_name . ' ' . $teacher->last_name ?>
    </td>
</tr>
<?php
}
?>

据我测试,它似乎工作正常。 您发布的代码片段中是否缺少任何有助于重现您的问题的内容?

编辑:

所以完整的代码是:

<?php
$min = $this->db->get_where('academic_settings' , array('type' =>'minium_mark'))->row()->description;
$running_year = $this->db->get_where('settings', array('type' => 'running_year'))->row()->description;
?>
<div class="content-w">
  <div class="conty">
    <?php include 'fancy.php'; ?>
    <div class="header-spacer"></div>
    <div class="content-i">
      <div class="content-box">
        <div class="row">
          <div class="table-responsive">
            <table width="100%" class="table table-striped table-lightfont">
              <tbody>
                <tr>
                  <td style="color:black !important; text-transform:uppercase; text-align:center;">
                    <?php echo get_phrase('Subject_Name'); ?>&nbsp;:&nbsp;
                    <span><?php echo $row['name']; ?></span>
                  </td>
                </tr>
                <tr>
                  <td>
                    <table class="table table-padded">
                      <thead style="background-color:#90be2e; color:white;">
                        <tr style="padding-bottom:4px; padding-top:4px;">
                          <th style="color:white;"><?php echo get_phrase('Publish Date'); ?></th>
                          <th style="color:white;"><?php echo get_phrase('Assignment'); ?></th>
                          <th style="color:white;"><?php echo get_phrase('Faculty'); ?></th>
                          <th style="color:white;"><?php echo get_phrase('Last Date'); ?></th>
                          <th style="color:white;"><?php echo get_phrase('Submitted On'); ?></th>
                          <th style="color:white;"><?php echo get_phrase('Marks'); ?></th>
                          <th style="color:white;"><?php echo get_phrase('Feedback'); ?></th>
                        </tr>
                      </thead>
                      <tbody>
                        <?php
                        $uploader_id_student = $this->db->get_where('student', array('student_id' => $this->session->userdata('login_user_id')))->row()->student_id;
                        $invoices = $this->db->get_where('deliveries', array('student_id' => $uploader_id_student))->result_array();
                        foreach ($invoices as $row2) :
                          $homework_data = $this->db->get_where('homework', array('homework_code' => $row2['homework_code']))->row();
                          $teacher = $this->db->get_where('teacher', array('teacher_id' => $get_homework_data->uploader_id))->row();
                        ?>
                          <tr>
                            <td>
                              <?php echo $homework_data->title->upload_date; ?>
                            </td>
                            <td>
                              <?php
                              echo wordwrap($homework_data->title, 15, "<br>\n");
                              ?>
                            </td>
                            <td>
                              <?php
                              echo $teacher->first_name;
                              ?>&nbsp;
                              <?php
                              echo $teacher->last_name;
                              ?>
                            </td>
                            <td>
                              <?php
                              $sampleDate1 = $homework_data->date_end;
                              $convertDate1 = date("d-m-Y", strtotime($sampleDate1));
                              echo $convertDate1;
                              ?>
                            </td>
                            <td>
                              <?php
                              $sampleDate = $row2['date'];
                              $convertDate = date("d-m-Y", strtotime($sampleDate));
                              echo $convertDate;
                              ?>
                            </td>
                            <td style="text-align:center;">
                              <?php echo $row2['mark']; ?>
                            </td>
                            <td>
                              <?php echo wordwrap($row2['teacher_comment'], 25, "<br>\n"); ?>
                            </td>
                          </tr>
                        <?php endforeach; ?>
                      </tbody>
                    </table>
                  </td>
                </tr>
              </tbody>
            </table>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

我需要查看包含文件“fancy.php”的内容才能确定。 有一个额外的endforeach语句,但我猜它来自您更新的代码段的 scope 之外的某个地方。 我需要查看文件的全部内容和包含的脚本的内容,以确定错误的真实性质。

<?--<;php endforeach??> -->

删除此行

暂无
暂无

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

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