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

blu*_*sky 5 html php mysql html-table

所以我创建了一个接受来自下拉框输入的接口. 在此输入图像描述

然后显示HTML表中三个不同数据库表的数据,其中所有条目都符合条件. 在此输入图像描述

该表显示在上图的底部.

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

在此输入图像描述

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

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

<?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>
Run Code Online (Sandbox Code Playgroud)

Pau*_*aul 1

由于您已经有了生成一张表的代码,因此您可以使用它来生成所有表,这是正确的。

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

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

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);
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

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

<?php 

error_reporting(E_ALL);

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

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

?>
<html>
<body>
<?php

// Lists of possible DB values
$event = array("100m", "100m relay", "High Jump", ...); // a list of all events
$gender = array("F", "M"); // a list of all genders
$year = array(7, 8, 9, 10, 11, 12); // a list of all classes

foreach ($event as $e) {
  foreach ($gender as $g) {
    foreach ($year as $y) {

      $result[] = 0;
      try {
          $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"; 

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

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

          $stmt->execute();
          $result = $stmt->fetchAll();
          $count = $stmt->rowCount();
      }
      catch (PDOException $e) {
          echo $e->getMessage();
      }

     if ($count > 0) {
?>
    <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 for $e ($g, $y).";
      }
    }
  }
}
?>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)