Since you already have code to create one table, you are right that you can use them to generate all of them.
All you have to do is to see all the possibilities that your form provides.
You should have a list of options for creating an HTML form, just use these lists of options in a nested foreach loop.
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); } } }
Here is a complete example according to the code you provided:
<?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>
source share