使用PHP检查创建表,使用之前的值和当前值(Array)

时间:2017-09-06 04:57:46

标签: php mysql

这是我的数据表1。我希望这个表更改为table2。我必须使用PHP。我使用foreach编写了一些代码,但这样做不正常。 ç

表1

location = / {
        proxy_pass http://0.0.0.0:5000;
        proxy_http_version 1.1;
        proxy_set_header Connection keep-alive;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
}

location / {
        proxy_pass http://0.0.0.0:5000;
        proxy_http_version 1.1;           
        proxy_set_header Connection "";  # <--- this will disable header
        proxy_set_header Host $host;           
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

我想使用PHP创建类似|-------+------------+--------------+---------+--------| |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 | |-------+------------+--------------+---------+--------| 的内容。

表2

table2

**我写了一些这样的。但这不能正常工作。 **

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

这是我的数组

$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>";

这是我的数组数据。我必须使用此数组创建类似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 ) ) 的内容。任何人都可以帮助我。

6 个答案:

答案 0 :(得分:0)

如果我理解正确,你希望表格2如图所示那么基于我将回答你的问题。

您可以使用下面的HTML,它看起来像您想要的表格。

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

想要查看jsFiddle here

你没有发布你的SQL,你不需要使用forwach你可以while循环它,如下所示。

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

请注意,这不是您的SQL,因此请根据您的需要进行调整,并且位于PDO

答案 1 :(得分:0)

这将是您的桌面设计:(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>

现在用php显示数据你可以做类似的事情:

$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>";

现在取决于$result的样子。

答案 2 :(得分:0)

你可以通过group_concat mysql函数使用sql生成结果

{
"SchemaList": {
    "type": "array",
    "additionalItems": false,
    "items": {
        "$ref": "#/definitions/Schema1"
    }
},
"Schema1": {
    "type": "object",
    "properties": {
        "description": {
            "type": "string"
        },
        "name": {
            "type": "string"
        }
    }
},
"required": ["name"]
}

此查询将生成类似这样的结果

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

答案 3 :(得分:0)

Rowspan告诉单元格占用多少行,colspan有多少列。

$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 :(得分:0)

尝试一次我没有检查输出我假设数组如下

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>";

答案 5 :(得分:0)

在我看来,您使用的表结构不正确。

这是您正在寻找的表格布局的正确表格结构。

<div class="form-group">
  <label for="course">Course:</label>
  <select class="form-control" id="course" [(ngModel)]="course" name="course" #name required>
    <option *ngFor="let course of courses" [value]="course">{{course}}</option>
  </select>
</div>