如何在逻辑上使用PHP显示数据库表的所有可能值?
因此,我创建了一个接口,它接受下拉框中的输入。。 然后在一个HTML表中显示来自三个不同数据库表的数据,其中包含符合条件的所有条目。 该表显示在上图的底部 我的问题是如何使用PHP,使用循环或其他方式,重新编写代码并创建一个贯穿每个事件的巨大HTML页面?AKA我需要生成126个表: 但我不知道该怎么做。我最初的想法是使用一个循环,将代码放在其中生成一个表,但我不知道如何设置停止的条件,也不知道如何循环使用下拉列表中的不同选项。我不是要求任何人为我创建代码,而是为我指明使用何种逻辑的方向。。在那之后,我可能可以自己解决。谢谢大家。:) 下面是我的代码,我使用它生成每个表,并以注释形式进行注释:如何在逻辑上使用PHP显示数据库表的所有可能值?,php,html,mysql,html-table,Php,Html,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>
//检查是否有结果。如果有结果,将显示它们:
名称
房子
分数
//如果没有,则显示没有结果。
因为您已经有了生成一个表的代码,所以您可以使用该代码生成所有表
你所需要做的就是循环浏览表单提供的所有可能性
为了构建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);
}
}
}
以下是根据您提供的代码提供的完整示例:
<?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>
名称
房子
分数
我重新创建了您的数据库结构。使用左连接,我能够检索到您需要的按事件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 | 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..)
}
因此,这篇文章有很多内容需要接受,但您是否试图同时生成大量表?这就是我从阅读下半部分中得到的吗?@Jek是的,基本上我试图一次生成很多表。很抱歉,不清楚,我将编辑这篇文章以使其清晰。@Jek现在有意义吗?与我看到的许多so文章相比,你在这里做了很多正确的事情:你有错误报告,你有正确的字符集,你有PDO,你在使用对象数据库接口。您正在使用try/catch块。。。到目前为止干得好:)@Martin先生,你不知道这对我有多重要。非常感谢。嗨,保罗,我理解这个foreach()方法背后的道理。然而,对于如何在表单的操作中实现这一点,我感到非常困惑:“$year=$\u POST['year'];$gender=$\u POST['gender'];$event=$\u POST['event'];”我必须更改此部分,对吗?您不再需要表单,因为所有可能性(表格)都将被打印出来。您可以在数据库查询中使用变量
$e$g和$y
。见更新的答案。嗯。。这样地?我很困惑,对不起,我似乎错过了一些重要的东西…基本上是的。你试过了吗?结果的表声明也应该在循环中。乍一看,我觉得不错。是的,我试过了,但没有成功。这就是我问的原因。我得到了以下错误;“注意:未定义变量:第18行{dir}中的事件”和“警告:为第18行{dir}中的foreach()提供的参数无效”。第18行如下:“foreach($event as$e){”但是我不应该为每个foreach()都得到一个错误吗方法?你好,好先生。成绩不是数据库中的一个表:)它只是表的外部部分,因为我正在创建的系统意在创建可以打印出来的页面,以便可以手工书写页面,也就是说表的空白列是空白的,因此可以亲自填写。请进一步解释您的答案?我不明白它是如何回答我的问题的,因为我对PHP非常缺乏经验,恐怕我可能误解了您的回答。谢谢您,nadir:)也许我误解了您的问题。您正在尝试构建一个html页面,其中列出了每个事件的表,并且在每个表上您应该显示我关于参与这些活动的学生的信息,是吗?是的。:)但我需要为每个活动提供一个单独的表,似乎您的输出是一个单独的表?我在底部添加了一个示例,以演示如何拆分输出并为每个活动获取一个数组。