I’m trying to use a switch statement to execute a section of code to run a query to generate an html list. I would like use the get value from the URL to correlate with the switch statement IE list.php?=types would run the query and script to generate the html table for types. But I can’t quite figure out how it make it work. Any help would be appreciated.
list.php
<?php include_once 'includes/dbh.inc.php'; __GET = $list switch ($list) { case 'roads': $sql = "SELECT * FROM roads"; $stmt = $mysqli->prepare($sql); //$stmt->bind_param("i", $roadID); $stmt->execute(); $result = $stmt->get_result(); echo "<table border='1'>"; echo "<thead>"; echo "<td></td>"; echo "<td>Option</td>"; while ($row = $result->fetch_assoc()) { echo "<tr>"; echo "<td>". $row['roadName']." (". $row['reportingMark']. ")</td>"; echo "<td>"; } echo "</tr>"; echo "</table>"; break; case 'types': $sql = "SELECT * FROM types"; $stmt = $mysqli->prepare($sql); //$stmt->bind_param("i", $roadID); $stmt->execute(); $result = $stmt->get_result(); echo "<table border='1'>"; echo "<thead>"; echo "<td></td>"; echo "<td>Option</td>"; while ($row = $result->fetch_assoc()) { echo "<tr>"; echo "<td>". $row['typeName']."</td>"; echo "<td>"; } echo "</tr>"; echo "</table>"; break; case 'packs': $sql = "SELECT * FROM packs"; $stmt = $mysqli->prepare($sql); //$stmt->bind_param("i", $roadID); $stmt->execute(); $result = $stmt->get_result(); echo "<table border='1'>"; echo "<thead>"; echo "<td></td>"; echo "<td>Option</td>"; while ($row = $result->fetch_assoc()) { echo "<tr>"; echo "<td>". $row['packName']. "</td>"; echo "<td>"; } echo "</tr>"; echo "</table>"; break; case 'origin': $sql = "SELECT * FROM origins"; $stmt = $mysqli->prepare($sql); //$stmt->bind_param("i", $roadID); $stmt->execute(); $result = $stmt->get_result(); echo "<table border='1'>"; echo "<thead>"; echo "<td></td>"; echo "<td>Option</td>"; while ($row = $result->fetch_assoc()) { echo "<tr>"; echo "<td>". $row['origin']. "</td>"; echo "<td>"; } echo "</tr>"; echo "</table>"; break; default: echo 'Not found'; break }
Advertisement
Answer
Just use the $_GET superglobal. https://www.php.net/manual/en/reserved.variables.get.php
?type=roads
if (isset($_GET['type'])) { switch($_GET['type']) { //etc } }