Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7811255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T04:05:34+00:00 2026-06-02T04:05:34+00:00

I have the following tables in my database. The tables midterm and finalterm contain

  • 0

I have the following tables in my database. The tables midterm and finalterm contain marks of students.

enter image description here

Now I want to display a marks sheet of all the students in single page like following:

enter image description here

I know how to query in single table but I am facing problem in this case as there are three tables.

I am using Codeigniter, would you please kindly help me with what to code in my model and view file?

Thanks in advance 🙂

This is my Controller:

         $this->load->model('Mod_student');
     $data['records1']= $this->Mod_student->check1();
         $this->load->view('view_student',$data);

Updated part

This is my view:

    <?php if(count($records1) > 0) { ?>

      <?php foreach ($records1 as $row){ ?>



   <table>
   <tr> <td><?php echo $row['StudentName']; ?></td></tr>

     <tr>

      <td><?php $midDate   =   explode(',',$row['MidDate']);
     foreach($midDate as $md)
         {
         echo $md;
         echo '<br />'  ;
         }
    ?>
         </td> 

       <td><?php $midDate   =   explode(',',$row['MidSubject']);
     foreach($midDate as $md)
         {
         echo $md;
         echo '<br />'  ;
         }
    ?>
      </td> 

       <td><?php $midDate   =   explode(',',$row['MidMarks']);
     foreach($midDate as $md)
         {
         echo $md;
         echo '<br />'  ;
         }
    ?>
       </td> 



       </tr>





     </table>




<?php  } ?>

<?php } else { echo "No Record Found";} ?>

I have changed my view file and now it is working fine 🙂 But there are some problems that I am facing. If I run the above script I get the following output

enter image description here

If you notice my table midterm, you would see that the student whose id is 3 has got the same marks in both English and Maths. But in my output it displays mark for only one subject in the middle. Is it happening because you used distinct in your query? I have tried removing the distinct but then it does not show exact data.

Would you please kindly tell me how to fix the problem?

And thanks again for your help.:)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T04:05:37+00:00Added an answer on June 2, 2026 at 4:05 am

    I have found a solution for you. It will fetch a single record for each student contining all of his information. I have used here GROUP_CONCAT of mysql. Therefore, on the php end you will have to explode the value and then access it in the loop. By the way here is the query you can run it like this.

     $query= "select
              s.id   as SID,
              s.name as StudentName,
              (select group_concat(distinct midterm.date) from midterm where midterm.student_id = SID) as MidDate,
              (select group_concat(distinct midterm.Subject) from midterm where midterm.student_id = SID) as MidSubject,
              (select group_concat(midterm.marks) from midterm where midterm.student_id = SID) as MidMarks,
              (select group_concat(distinct finalterm.date) from finalterm where finalterm.student_id = SID) as FinalDate,
              (select group_concat(distinct finalterm.marks) from finalterm where finalterm.student_id = SID) as FinalMarks,
              (select distinct sum(midterm.marks) from midterm where midterm.student_id = SID) as MidTotal,
              (select distinct sum(finalterm.marks) from finalterm where finalterm.student_id = SID) as FinalTotal
              from students as s
              left join midterm as m on m.student_id = s.id 
              left join finalterm as f on f.student_id = s.id
              and f.subject = m.subject
              group by s.name;";
    
        $query  =   $this->db->query($query);
    

    OR Active Record Version

    $data   =   array(
                        's.id   as SID',
                        's.name as StudentName',
                        '(select group_concat(distinct midterm.date) from midterm where midterm.student_id = SID) as MidDate',
                        '(select group_concat(distinct midterm.Subject) from midterm where midterm.student_id = SID) as MidSubject',
                        '(select group_concat(midterm.marks) from midterm where midterm.student_id = SID) as MidMarks',
                        '(select group_concat(distinct finalterm.date) from finalterm where finalterm.student_id = SID) as FinalDate',
                        '(select group_concat(distinct finalterm.marks) from finalterm where finalterm.student_id = SID) as FinalMarks',
                        '(select distinct sum(midterm.marks) from midterm where midterm.student_id = SID) as MidTotal',
                        '(select distinct sum(finalterm.marks)from finalterm where finalterm.student_id = SID) as FinalTotal'               
                        );
        $this->db->select($data);
        $this->db->group_by('s.name');
        $this->db->group_by('s.name');
        $this->db->from('students as s');
        $this->db->join('midterm as m','m.student_id = s.id','left');
        $this->db->join('finalterm as f','f.student_id = s.id','left');
        $this->db->group_by('s.name');
    

    With distinct keyword. Here are some instructions to use this.
    1. Every group concat should be exploded to be used at the php end. Like this

    <?php echo $row['StudentName']?>
    

    And Group Concat Item form query

     $midDate   =   explode(',',$row['MidDate']);
     foreach($midDate as $md)
     {
     echo $md;
     echo '<br />'  ;
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have the following tables in my database: Now all my queries depend
I have the following database tables and columns: students +------------+-------------------+------------------+ | student_id | student_firstname
I have the following tables in my database: Products ID_PRODUCT PRODUCTNAME PRICE Customers ID_CUSTOMER
I have the following tables in a database (i'll only list the important attributes):
I have the following (simplified) database tables: Products - ProductID, int, PK - Name,
I have the following tables, with these keys in my database: bookings session_id sessions
I have the following two tables in my mysql database. Table Name: groups id
I have three mysql table from same database Db1. Three tables have following columns.
I have a database with the following tables, Member, Paper, Topic, Interest, Associated. I
I have two database tables with the following structure: actions: action_id int(11) primary key

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.