Skip to content
Advertisement

For loop is going again and again for the same values

I am not sure what is going wrong. But the system is printing the same values again and again, where it should echo the value only once where it is matching student_id in the delivery table. The full code I have edited as requested. This is not executing the query but able display the Table headings.

The code is :

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

attached is screenshot of table Deliveries is there Deliveries Picture Download

Advertisement

Answer

Here is your code updated to fix the performance issues mentioned by 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
}
?>

It appears to be working fine as far as I could test it. Is there anything missing from the snippet you posted that could help reproduce your issue?

Edit:

So the complete code would be:

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

I would need to see the contents of the included file “fancy.php” to be sure. There was an extra endforeach statement, but I’m guessing that it came from somewhere beyond the scope of the snippet you updated. I would need to see the whole content of the file and the content of the included script in order to determine the true nature of the error.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement