I'm trying to sort a restaurant menu by type of meal. So appetizers first, main dish second, desert last.
When adding it to the database I use "soort" which gives it a type of meal.
When I run this code it wil show me the main dish, desert, appetizer which is the wrong order.
I can't figure out how to sort it the way I want it.
<?php
// vul de variable in die gebruikt worden om een connectie te maken (inlog gegevens)
$servername = "localhost";
$username = "root";
$password = "";
$naam = $soort = $prijs = $bijzonderheden = "";
$foutmelding1 = $foutmelding2 = $foutmelding3 = $foutmelding4 = "";
try {
$pdo = new PDO("mysql:host=$servername;dbname=restaurant", $username, $password);
// set the PDO error mode to exception
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
}
// connectie is mislukt
catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
?>
<html>
<head>
</head>
</body>
<table>
<tr>
<th> Gerecht </th> <th> Soort </th> <th> Prijs </th> <th> Bijzonderheden </th>
</tr>
<?php
$sth = $pdo->prepare('select * from gerechten ORDER BY Soort ');
$sth->execute();
while($row = $sth->fetch())
{
echo "<tr> ";
echo "<td>" . $row['Naam'] . "</td>";
echo "<td>" . $row['Soort'] . "</td>";
echo "<td> " . $row['Prijs'] . "</td>";
echo "<td> " . $row['Bijzonderheden'] . "</td>";
echo "</tr> ";
}
// de update knoppen doorverwijzen met een href --> en dan een form maken voor 1 rij <-----------
?>
</table>
<style type="text/css">
table {
border-collapse: ;
border: 1px solid black;
padding: 5px;
}
td {
/* border: 1px solid black;
width: 25px; */
text-align: center;
padding: 15px;
}
</style>
</html>
This is the code that inserts the data in the database
<?php
// vul de variable in die gebruikt worden om een connectie te maken (inlog gegevens)
$servername = "localhost";
$username = "root";
$password = "";
$naam = $soort = $prijs = $bijzonderheden = "";
$foutmelding1 = $foutmelding2 = $foutmelding3 = $foutmelding4 = "";
try {
$pdo = new PDO("mysql:host=$servername;dbname=restaurant", $username, $password);
// set the PDO error mode to exception
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
}
// connectie is mislukt
catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
// maakt een query
if(isset($_POST['submit']))
{
// begin waarde van de fout check variable
$fout = FALSE;
// POST bijzonderheden mag hier wel blijven, want dat veld is niet verplicht om in te vullen.
$bijzonderheden = $_POST['bijzonderheden'];
// checkt of het veld 'naam' gevult is, zo niet dan geeft hij de melding vul het veld naam in
if (!empty($_POST['naam'])) {
$naam = $_POST['naam'];
}
else { $foutmelding1 = "vul het veld naam in";
$fout = TRUE;
}
// checkt of het veld 'soort' gevult is, zo niet dan geeft hij de melding vul het veld naam in
if (!empty($_POST['soort'])) {
$soort = $_POST['soort'];
}
else { $foutmelding2 = "vul het veld soort in";
$fout = TRUE;
}
// checkt of het veld 'prijs' gevult is, zo niet dan geeft hij de melding vul het veld naam in
if (!empty($_POST['prijs'])) {
$prijs = $_POST['prijs'];
}
else { $foutmelding3 = "vul het veld prijs in";
$fout = TRUE;
}
// checkt of het veld 'bijzonderheden' gevult is, zo niet dan geeft hij de melding vul het veld naam in
// if (!empty($_POST['bijzonderheden'])) {
// $bijzonderheden = $_POST['bijzonderheden'];
// }
// else { $foutmelding4 = "vul het veld bijzonderheden in";
// $fout = TRUE;
// }
if ($fout == FALSE)
{
// array aanmaken voor de parameters.
// Een array wordt gebruikt om de gegevens beveiligd te houden.
$parameters = array(':naam'=>$naam,
':soort'=>$soort,
':prijs'=>$prijs,
':bijzonderheden'=>$bijzonderheden);
// maakt een query die iets in de database gaat zetten
$sth = $pdo->prepare('INSERT INTO gerechten ( Naam, Soort, Prijs, Bijzonderheden) VALUES (:naam, :soort, :prijs, :bijzonderheden)');
$sth->execute($parameters);
echo "toevoegen gelukt";
header("Refresh:3");
}
}
$sth = $pdo->prepare('select * from gerechten');
$sth->execute();
//while($row = $sth->fetch())
//{
// echo '<br />' . $row['Naam'].'<br />'.'<br />';
//}
?>
<html>
<body>
<h2>Gerecht toevoegen aan de database</h2>
<form action="" method="post">
Naam: <input type="text" name="naam" value="<?php echo $naam ?>"> <?php echo $foutmelding1; ?> <br>
Soort:
<select id="soort" name="soort">
<option value="voorgerecht" <?php if($soort == "voorgerecht") echo "selected" ?>>Voorgerecht</option>
<option value="hoofdgerecht" <?php if($soort == "hoofdgerecht") echo "selected" ?>>Hoofdgerecht</option>
<option value="nagerecht" <?php if($soort == "nagerecht") echo "selected" ?> >Nagerecht</option>
</select> <?php echo $foutmelding2; ?><br>
Prijs <input type="number" min="1" step="any" " name="prijs" value="<?php echo $prijs ?>"> <?php echo $foutmelding3; ?><br>
Bijzonderheden <input type="text" name="bijzonderheden" value="<?php echo $bijzonderheden ?>"><br>
<input type="submit" name="submit" value="verzenden"><br>
</form>
</body>
</html>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…