Click here to Skip to main content
15,440,884 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hello,

Below is my query, I want to display the resulting data in a form like the image below using php foreach statement and html table tag.

SQL
select M.student_id,ST.name as studentname,M.obtain_total_mark,M.exam_total_mark,S.name AS subject 
        from marks AS M          
        left join subjects AS S ON S.id = M.subject_id  
        left join students AS ST ON ST.id = M.student_id  
        left join grades AS G ON G.id = M.grade_id  
        where M.academic_year_id = 6 AND M.class_id=1 AND M.section_id=1 AND M.exam_id = 2
        ORDER BY M.student_id,S.name


The output is below:
markssheet.png - Google Drive[^]

And I want the output like this:
markssheet2.png - Google Drive[^]

I have two arrays.
1. subjectnames which contains English and Urdu
2 subjects which contains student_id, Enlish, Urdu marks and student names.

What I have tried:

PHP
<table id="datatable-responsive" class="table table-striped table-bordered dt-responsive nowrap" cellspacing="0" width="100%">
               
                     
               <!--    <th rowspan="2"><?php //echo $this->lang->line('sl_no'); ?></th> -->
               <?php    if (isset($subjectnames) && !empty($subjectnames)) { ?>
                
                <tr>
                          <?php  foreach ($subjectnames as $sub) { ?>
                            <th colspan="2"><?php echo $sub->subject; ?>
                            <table id="datatable-responsive" class="table table-striped table-bordered dt-responsive nowrap" cellspacing="0" width="100%">
                            <?php  foreach ($subjects as $obj) { ?>
                            <?php  if($obj->subject == $sub->subject) { ?>  
                                <tr>
                                <td><?php echo $obj->studentname; ?></td>                               
                                    <td><?php echo $obj->exam_total_mark; ?></td>
                                    <td><?php echo $obj->obtain_total_mark; ?></td>                            
                                    </tr> 
                                <?php } } ?>
                                </table>
                            </th>
                            <?php }  ?>   
                        </tr>
                      <?php } 
             else { ?>
           
                       <tr>
                           <td align="center"><?php echo $this->lang->line('no_data_found'); ?></td>
                       </tr>
            <?php } ?> 
       </table>
Posted
Updated 26-Mar-21 22:22pm
v2
Comments
NotTodayYo 12-Mar-21 13:10pm    
What is the problem?
nyt1972 12-Mar-21 23:14pm    
if you check the images, the problem is that I want the output like in the image number 2.
Richard Deeming 15-Mar-21 8:45am     CRLF
No, that is your requirement. You need to explain the problem - ie: what you have tried, and where you are stuck.
nyt1972 27-Mar-21 4:22am    
ok, I posted my solution.
nyt1972 14-Mar-21 10:44am    
After using if statement resolved the problem.

1 solution

Here is my solution:
PHP
<table id="datatable-responsive" class="table table-striped table-bordered dt-responsive nowrap" cellspacing="0" width="100%">

             <thead>
            <?php    if (isset($subjectnames) && !empty($subjectnames)) { ?>

             <tr>
                       <th>Name</th>
                       <?php $i = 1;
                       foreach ($subjectnames as $sub) {
                         ?>
                         <th><?php echo $sub->subject. ' ['.$sub->exam_total_mark.']'; ?></th>
                         <?php } ?>
             </tr>
             </thead>
             <tbody>
                     <?php
                     $x = 0;
                     foreach ($subjects as $obj) {
                             $i = 1;
                             if($x == count($subjectnames))
                             {  echo ' <tr>'; }  ?>

                         <?php if($x == 0) { ?>
                         <td><?php echo $obj->studentname; ?></td>  <?php } ?>
                         <?php foreach ($subjectnames as $sub) {
                         if($obj->subject == $sub->subject) {
                             $x++;
                             ?>
                                 <td><?php echo $obj->obtain_total_mark; ?></td>
                             <?php  } }
                             if($x == count($subjectnames))
                             {
                                 echo '</tr>';
                                 $x=0;
                             }
                             ?>
                         <?php $i++;  }  ?>
                   <?php }
          else { ?>

                    <tr>
                        <td align="center"><?php echo $this->lang->line('no_data_found'); ?></td>
                    </tr>
         <?php } ?>
         </tbody>
    </table>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900