-2

This is my data table1. I want this table change to as a table2. I have to use PHP for this. I wrote the some code using foreach but that is not working properly. C

table1

|-------+------------+--------------+---------+--------|
|Seq No | Student Id | Subject Name |  Exams  | Marks  |
|-------+------------+--------------+---------+--------|
|  1    | 200301     |   maths      |  Exam 1 |  25    |
|  2    | 200301     |   maths      |  Exam 2 |  45    |
|  3    | 200301     |   art        |  Exam 1 |  76    |
|  4    | 200301     |   art        |  Exam 2 |  42    |
|  5    | 200302     |   maths      |  Exam 1 |  71    |
|  6    | 200302     |   maths      |  Exam 2 |  78    |
|  7    | 200302     |   art        |  Exam 1 |  35    |
|  8    | 200302     |   art        |  Exam 2 |  61    |
|-------+------------+--------------+---------+--------|

I want to create like table2 using PHP.

table 2

|-------+------------+-----------------+-----------------+
|       |            |     maths       |      art        |  
|Seq No | Student Id |-----------------|-----------------|
|       |            | Exam 1 | Exam 2 | Exam 1 | Exam 2 |
|-------+------------+-----------------+-----------------+
|  1    | 200301     |   25   |   45   |  76    |  42    |
|  2    | 200302     |   71   |   78   |  35    |  61    |
|-------+------------+--------+--------+--------+--------|

**I wrote some like this. but this is not working properly. **

$output .= "<table border=1>
          <tr bgcolor=#ffffff><td>SeqNo</td>
          <td>Student Id</td>
          <td>maths</td>
          <td>art</td>
          <td>Exam 1</td>
          <td>Exam 2</td></tr>";

          $studentCounter = 0;

foreach($result as $item)
            {
            $output .= "<tr><td>" . ++$studentCounter . " </td>  
                                        <td>" . $item[STUDENT_ID] . "</td>
                                        <td>" . $item[MATHS] . "</td> 
                                        <td>" . $item[ART] . "</td>
                                        <td>" . $item[EXAM_1] . "</td>
                                        <td>" . $item[EXAM_2] . "</td></tr>";

            }
 $output .= "</table>";

this is my array

Array([1] => Array([STUDENT_ID] => 200301
               [SUBJECT_NAME] => maths
               [ASSIGNMENT_TITLE] => exam_1
               [MARKS] => 25 )
  [2] => Array([STUDENT_ID] => 200301
               [SUBJECT_NAME] => maths
               [ASSIGNMENT_TITLE] => exam_2
               [MARKS] => 45 )
  [3] => Array([STUDENT_ID] => 200301
               [SUBJECT_NAME] => art
               [ASSIGNMENT_TITLE] => exam_1
               [MARKS] => 76 )
  [4] => Array([STUDENT_ID] => 200301
               [SUBJECT_NAME] => art
               [ASSIGNMENT_TITLE] => exam_2
               [MARKS] => 42 )
  [5] => Array([STUDENT_ID] => 200302
               [SUBJECT_NAME] => maths
               [ASSIGNMENT_TITLE] => exam_1
               [MARKS] => 71 )
  [6] => Array([STUDENT_ID] => 200302
               [SUBJECT_NAME] => maths
               [ASSIGNMENT_TITLE] => exam_2
               [MARKS] => 78 )
  [7] => Array([STUDENT_ID] => 200302
               [SUBJECT_NAME] => art
               [ASSIGNMENT_TITLE] => exam_1
               [MARKS] => 35 )
  [8] => Array([STUDENT_ID] => 200302
               [SUBJECT_NAME] => art
               [ASSIGNMENT_TITLE] => exam_2
               [MARKS] => 61 )
 )

This is my array data. I have to create like table2 using this array. any one can help me.

0

6 Answers 6

0

If I understand correctly you want the table 2 to be as shown in the illustration so based on that I'm going to answer your question.

You can use the below HTML which will look like the table you wanted.

<table border=1>
  <tr bgcolor=#ffffff>
    <td>SeqNo</td>
    <td>Student Id</td>
    <td colspan = "2">maths</td>
    <td colspan = "2">art</td>
    </tr>
    <tr>
    <td colspan = "2"></td>
    <td>Exam 1</td>
    <td>Exam 2</td>
    <td>Exam 1</td>
    <td>Exam 2</td>
  </tr>
      <tr>
    <td></td>
    <td></td>
    <td>Exam 1</td>
    <td>Exam 2</td>
    <td>Exam 1</td>
    <td>Exam 2</td>
  </tr>
</table>

Want see jsFiddle here

You didn't post your SQL you don't need to use a forwach you can while loop it like shown below.

<table border=1>
  <tr bgcolor=#ffffff>
    <td>SeqNo</td>
    <td>Student Id</td>
    <td colspan = "2">maths</td>
    <td colspan = "2">art</td>
    </tr>
    <tr>
    <td colspan = "2"></td>
    <td>Exam 1</td>
    <td>Exam 2</td>
    <td>Exam 1</td>
    <td>Exam 2</td>
  </tr>
<?PHP while($row = $stmt -> fetch(PDO::FETCH_ASSOC){ ?>
  <tr>
    <td>$row["seqNo"]</td>
    <td>$row["studen_id"]</td>
    <td>$row["Exam1"]</td>
    <td>$row["Exam2"]</td>
    <td>$row["Exam1"]</td>
    <td>$row["Exam2"]</td>
  </tr>
<?php } ?>
</table>

Keep in mind this is with out your SQL so adjust it to fit your need and is in PDO.

Sign up to request clarification or add additional context in comments.

4 Comments

Please check table1. there are same student id will repeat. then this code not working properly.
As I told you I gave you an example you didn't post any SQL or any other needed code so I was just going on a hunch. Take the idea and tweak it till it works. My code is not a copy paste and run it is an example.
how to check repeat student, after that i have to go to another record.
GROUP the records by student id.
0

This would be your table design: (https://jsfiddle.net/vg3whzvq/2/)

<table border=1>
      <tr bgcolor=#ffffff>
        <td rowspan="2">SeqNo</td>
        <td rowspan="2">Student Id</td>
        <td colspan = "2">maths</td>
        <td colspan = "2">art</td>
        </tr>
        <tr>
        <td>Exam 1</td>
        <td>Exam 2</td>
        <td>Exam 1</td>
        <td>Exam 2</td>
      </tr>
          <tr>
        <td>1</td>
        <td>200301</td>
        <td>51</td>
        <td>25</td>
        <td>64</td>
        <td>45</td>
      </tr>
    </table>

Now to display the data with php you can do something like that:

$output .= "<table border=1>
          <tr bgcolor=#ffffff>
            <td rowspan="2">SeqNo</td>
            <td rowspan="2">Student Id</td>
            <td colspan = "2">maths</td>
            <td colspan = "2">art</td>
            </tr>
            <tr>
            <td>Exam 1</td>
            <td>Exam 2</td>
            <td>Exam 1</td>
            <td>Exam 2</td>
          </tr>";

          $studentCounter = 0;

foreach($result as $item)
            {
            $output .= "<tr>
                          <td>" . ++$studentCounter . " </td>  
                          <td>" . $item[STUDENT_ID] . "</td>
                          <td>" . $item[MATHS_EXAM_1] . "</td> 
                          <td>" . $item[MATH_EXAM_2] . "</td>
                          <td>" . $item[ART_EXAM_1] . "</td>
                          <td>" . $item[ART_EXAM_2] . "</td>
                        </tr>";
            }

 $output .= "</table>";

It now depends on how your $result looks like.

7 Comments

Please check table1. there are same student id will repeat. then this code not working properly.
Is there a way to adjust your $result? Are there always 2 entries for one StudentID?
always not 2 entries, sometimes 3 entries for one studentID.
Can you adjust your SQL? You should have Math and Art Exams in one row. Then you can use foreach to get your desired design. If not you would have to sort your array first and then go through it with foreach()
$QI = DBQuery($sql); $result = DBGet($QI,array('STUDENT_ID'=>'student_id','MATHS'=>'maths',‌​'ART'=>'art','EXAM_1‌​'=>'exan_1','EXAM_2'‌​=>'EXAM_2')); $columns = array('STUDENT_ID'=>'student id','MATHS'=>'maths','ART'=>'art','EXAM_1'=>'Exam 1','EXAM_2'=>'Exam 1'); This is my sql array
|
0

you can generate result using sql by group_concat mysql function

SELECT * FROM table1 GROUP_CONCAT(student_id) ORDER BY subject_name DESC

this query will generate result something like this

student id=>1,subject=>math,exam1=>15,exam2=>20,subject=>art,exam1=>17,exam2=>19

Comments

0

Rowspan tells a cell how many rows to occupy, colspan how many columns.

$output .= "<table border=1>
    <tr bgcolor=#ffffff>
        <td rowspan="2">SeqNo</td>
        <td rowspan="2">Student Id</td>
        <td colspan="2">maths</td>
        <td colspan="2">art</td>
    </tr>
    <tr>
        <td>Exam 1</td>
        <td>Exam 2</td>
        <td>Exam 1</td>
        <td>Exam 2</td>
    </tr>";

$studentCounter = 0;

foreach($result as $item) {
    $output .= "<tr>
        <td>" . $studentCounter++ . " </td>  
        <td>" . $item["STUDENT_ID"] . "</td>
        <td>" . $item["MATHS"] . "</td> 
        <td>" . $item["ART"] . "</td>
        <td>" . $item["EXAM_1"] . "</td>
        <td>" . $item["EXAM_2"] . "</td></tr>";
}

$output .= "</table>";

4 Comments

I want create PHP code. I have more than 1000 records.not HTML code, I want PHP code
you have to check table 1. there are Student Id repeated. Then I have to check and after that go to the check another subject name. How I can do that ?
Have you even written any queries yet ?
You may want to add your current sql to the OP.
0

Try this once i didn't check the output i am assuming array like follow

array(
     [
      "STUDENT_ID" => "1", 
     'MATHS' => 
             array( 
                 "EXAM_1" => 'test 1', 
                 "EXAM_2" => 'test2'), 
     'ART' => 
             array( 
                 "EXAM_1" => 'test 1', 
                  "EXAM_2"=> 'test2')
      ],
     [
      "STUDENT_ID" => "2", 
     'MATHS' => 
             array( 
                 "EXAM_1" => 'test 1', 
                 "EXAM_2" => 'test2'), 
     'ART' => 
             array( 
                 "EXAM_1" => 'test 1', 
                  "EXAM_2"=> 'test2')
      ]
     );

`

$output .= "<table border=1>
      <tr bgcolor=#ffffff><td>SeqNo</td>
      <td>Student Id</td>
      <td colspan="2">maths</td>
      <td colspan="2">art</td>
      ";

     $studentCounter = 0;

     foreach($result as $item)
        {
        $output .= "<tr><td>" . ++$studentCounter . " </td>  
                       <td>" . $item[STUDENT_ID] . "</td>
                       <td>" . $item[MATHS][EXAM_1] . "</td> 
                       <td>" . $item[MATHS][EXAM_2] . "</td>
                       <td>" . $item[ART][EXAM_1] . "</td>
                       <td>" . $item[ART][EXAM_2] . "</td></tr>";
                     "

        }
   $output .= "</table>";

2 Comments

you have to check table 1. there are Student Id repeated. Then I have to check and after that go to the check another subject name. How I can do that ?
if possible use something like Group Concat to group all subjects mark for same student, please refer this
0

It seems to me that the table structure you are using is incorrect.

Here is a correct table structure of the table layout you are looking for.

<table border=1>
    <thead>
        <tr>
            <th rowspan="2">Seq N0</th>
            <th rowspan="2">Student Id</th>
            <th colspan="2">maths</th>
            <th colspan="2">art</th>
        </tr>
        <tr>
            <td>Exam 1</td>
            <td>Exam 2</td>
            <td>Exam 1</td>
            <td>Exam 2</td>  
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>1</td>
            <td>1</td>
            <td>82</td>
            <td>90</td>
            <td>60</td>
            <td>82</td>
        </tr>
        <tr>
            <td>1</td>
            <td>1</td>
            <td>82</td>
            <td>90</td>
            <td>60</td>
            <td>82</td>
        </tr>
    </tbody>
</table>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.