如何在逻辑上使用PHP来显示数据库表的所有可能值?

时间:2016-04-11 13:18:49

标签: php html mysql html-table

所以我创建了一个接受来自下拉框输入的接口。 enter image description here

然后在HTML表格中显示三个不同数据库表格中的数据,其中所有条目都符合条件。 enter image description here

表格显示在上图的底部。

我的问题是我如何使用PHP,使用循环或其他方式来重新编写代码并创建一个包含每个单个事件的巨大HTML页面? AKA我需要生成126个表:

enter image description here

但我不确定如何处理这个问题。我最初的想法是使用一个循环并只是将代码放在其中生成一个单独的表,但我不知道该条件是什么让它停止,我也不知道如何循环下拉列表中的不同选项。我并没有要求任何人为我创建代码,而是指向我使用什么逻辑的方向......之后,我可以自己解决这个问题。谢谢大家。 :)

下面是我的代码,我用它来生成每个表,注释形式的注释:

<?php 

error_reporting(E_ALL);

$dbhost     = "localhost"; //logs into my localhost server
$dbname     = "sportsDay";
$dbuser     = "root";
$dbpass     = "...";

$year=$_POST['Year'];  //gets variables from the drop-downs in the form displayed above
$gender=$_POST['Gender']; 
$event=$_POST['Event']; 

$result[]=0;
try
{

    $conn = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $conn->exec("SET CHARACTER SET utf8mb4");

    $sql = "SELECT Students.lName, Students.fName, Students.house 
    FROM Entries INNER JOIN Events ON Entries.ev1ID = Events.ID   
    JOIN Students ON Students.stID = Entries.stID
    WHERE (Entries.ev1ID = :event or Entries.ev2ID = :event2) and (Students.year = :year) 
    AND (Students.gender = :gender)
    ORDER BY Students.house ASC"; 
  //my SQL code that matches up the values from the drop-downs to the values in the database tables

    $stmt = $conn->prepare($sql);

    $stmt->bindValue(':event', $event);
    $stmt->bindValue(':event2', $event);
    $stmt->bindValue(':year', $year);
    $stmt->bindValue(':gender', $gender);

    $stmt->execute();
    $result = $stmt->fetchAll();
    $count = $stmt->rowCount();
}

catch(PDOException $e)
{
    echo $e->getMessage();
}
     ?>
     <html>
     <body>
     <?php if ($count > 0): ?> //checks to see if there are results. if there are results, it displays them:
    <table border="1" >
        <tr>
            <th>Name</th>
            <th>House</th>
            <th>Score</th>
        </tr>
        <?php foreach ($result as $row) { 
            ?>
        <tr>
            <td><?php echo $row['fName']. ' '.$row['lName'] ?></td>
            <td><?php echo $row['house'] ?></td> <td></td>
        </tr>
        <?php } ?>
    </table>
<?php else: echo "No results." ?> //if not, it displays that there are no results.
<?php endif ?>
     </body>
     </html>

2 个答案:

答案 0 :(得分:1)

由于您已经拥有生成一个表的代码,因此您可以使用它来生成所有表。

您需要做的就是循环浏览表单提供的所有可能性。

为了构建HTML表单,您必须有一个可能的选项列表,只需在嵌套的foreach循环中使用这些选项列表。

clip

以下是根据您提供的代码提供的完整示例:

foreach ($event as $e) {
  foreach ($gender as $g) {
    foreach ($year as $y) {
      // Use $e, $g and $y for your query and table construction.
      $sql = ...; // Query stays the same.

      $stmt->bindValue(':event', $e);
      $stmt->bindValue(':event2', $e);
      $stmt->bindValue(':year', $y);
      $stmt->bindValue(':gender', $g);
    }
  }
}

答案 1 :(得分:0)

我重新创建了你的数据库结构。使用左连接,我能够检索您需要按事件ID排序的所有数据。

我的数据库娱乐:

学生表

+------+---------+-----------+-------+
| stID | fName   | lName     | house |
+------+---------+-----------+-------+
|    1 | Nadir   | Roman     | east  |
|    2 | Jesus   | Lopez     | west  |
|    3 | Ioannis | Chalkadis | west  |
|    4 | Adry    | Pepes     | east  |
|    5 | David   | Caretas   | west  |
+------+---------+-----------+-------+

活动表

+----+-----------+---------------+
| ID | name      | location      |
+----+-----------+---------------+
|  1 | 100m      | Track         |
|  2 | 400m      | Track         |
|  3 | High Jump | High Jump Pit |
+----+-----------+---------------+

得分表

+------+-------+-------+
| stID | ev1ID | ev2ID |
+------+-------+-------+
|    1 |     1 |     2 |
|    2 |     2 |     3 |
|    3 |     1 |     3 |
|    4 |     1 |     2 |
|    5 |     1 |     3 |
+------+-------+-------+

查询:

mysql> SELECT Events.ID, Events.name, location, Scores.stID, fName, lName, house FROM Scores LEFT JOIN (Students, Events) ON (Students.stID = Scores.stID AND (Events.ID = Scores.ev1ID OR Events.ID = Scores.ev2ID)) ORDER BY ID;

输出是:

+------+-----------+---------------+------+---------+-----------+-------+
| ID   | name      | location      | stID | fName   | lName     | house |
+------+-----------+---------------+------+---------+-----------+-------+
|    1 | 100m      | Track         |    1 | Nadir   | Roman     | east  |
|    1 | 100m      | Track         |    5 | David   | Caretas   | west  |
|    1 | 100m      | Track         |    4 | Adry    | Pepes     | east  |
|    1 | 100m      | Track         |    3 | Ioannis | Chalkadis | west  |
|    2 | 400m      | Track         |    2 | Jesus   | Lopez     | west  |
|    2 | 400m      | Track         |    1 | Nadir   | Roman     | east  |
|    2 | 400m      | Track         |    4 | Adry    | Pepes     | east  |
|    3 | High Jump | High Jump Pit |    2 | Jesus   | Lopez     | west  |
|    3 | High Jump | High Jump Pit |    5 | David   | Caretas   | west  |
|    3 | High Jump | High Jump Pit |    3 | Ioannis | Chalkadis | west  |
+------+-----------+---------------+------+---------+-----------+-------+

您可以将此输出拆分为不同的数组,每个事件1个:

$result = $stmt->fetchAll();
$mappedEvents = [];

array_map(function($entry)
{
    if(!isset($mappedEvents[$entry['ID']]))
    {
       $mappedEvents[$entry['ID']] = [];
    }
    $mappedEvents[$entry['ID'][] = $entry;
}, $result);

foreach($mappedEvents as $eventID => $data)
{
        // $eventID = event ID
        // $data = Array with all info about the event (students, location, etc..)
}