I have written a php script to display a database, here is my code:
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://ift.tt/mOIMeg">
<html xmlns="http://ift.tt/lH0Osb" xml:lang="en" lang="en">
<head>
<title>Home Page</title>
<link href="shopcss.css" type="text/css" rel="stylesheet"/>
</head>
<body>
<div id="header">
<?php
include_once('sessionstart.php');
require ('connection.php');
if (isset($_SESSION['username'])){
echo "Welcome " . $_SESSION['username'] . ", <a href='logout.php'>logout</a>";
}
$sort_price = isset($_GET['price']) ? $_GET['price'] : '';
$sort_genre = isset($_GET['genre']) ? $_GET['genre'] : '';
$sort_pricecheck = isset($_GET['pricecheck']) ? $_GET['pricecheck'] : '';
if ($sort_genre == "None") {
$sort_genre = null;
}
if($sort_price == "") {
$sort_price = "0";
}
if(!isset($sort_pricecheck)) {
$sort_pricecheck = ">";
}
$get_data = pg_query($conn, "SELECT * FROM videogames WHERE price $sort_pricecheck=$sort_price ORDER BY ref ASC");
$get_data_genre = pg_query($conn, "SELECT * FROM videogames WHERE price $sort_pricecheck=$sort_price AND genre='$sort_genre' ORDER BY ref ASC");
?>
<p>Online Games Shop</p>
</div>
<div id="nav">
<p><a href= " ">FPS</a></p>
<hr/>
<p><a href= " ">Open World</a></p>
<hr/> <!--Creates a line between the different sections of the nav bar-->
<p><a href=" ">Sports</a></p>
<hr/>
<p><a href=" ">Shopping Basket</a></p>
<hr/>
<p>
<a href="http://ift.tt/NiMmmw"><img
src="http://ift.tt/O87MBE" alt="Valid XHTML 1.0 Strict" height="31" width="88" /></a>
<a href="http://ift.tt/q7xgZB">
<img style="border:0;width:88px;height:31px"
src="http://ift.tt/1kha0fq"
alt="Valid CSS!" />
</a>
</p>
</div>
<div id="main">
<div>
<form action="gamestore.php" method="get">
View game genre
<select name="genre">
<option value="None">None</option>
<option value="Action/shooter">Action/shooter</option>
<option value="Roleplay">Roleplay</option>
<option value="Children">Children</option>
<option value="Adventure">Adventure</option>
<option value="Racing">Racing</option>
<option value="Fighting">Fighting</option>
<option value="Other">Other</option>
</select>
Select a price range(£):
<input type="number" name="price" id="checkPrice" />
<select name="pricecheck">
<option value=">">Higher</option>
<option value="<">Lower</option>
</select>
<input type="submit" value="Filter Results"/>
</form>
</div>
<table border = "1">
<tr>
<th>Add to basket</th>
<th>Title</th>
<th>Platform</th>
<th>Publisher</th>
<th>Genre</th>
<th>Pegi</th>
<th>Description</th>
<th>Price</th>
</tr>
<?php
if (isset($sort_genre)){
while ($a = pg_fetch_array ($get_data_genre)) {
$ref = $a['ref'];
$title = $a['title'];
$plat = $a['platform'];
$pub = $a['publisher'];
$genre = $a['genre'];
$pegi = $a['pegi'];
$desc = $a['description'];
$price = $a['price'];
echo "<tr>";
echo "<td><input type='checkbox' name = 'BasketAdd[]' value = '$ref' /></td><td>" . $title . "</td><td> " . $plat . "</td><td>" . $pub . "</td><td>" . $genre . "</td><td>" . $pegi . "</td><td>" . $desc . "</td><td>" . $price . "</td>"; echo "</tr>";
}
echo "<input type='submit' value='Add To Basket'/>";
echo "</form>";
}
else {
while ($a = pg_fetch_array ($get_data)) {
$ref = $a['ref'];
$title = $a['title'];
$plat = $a['platform'];
$pub = $a['publisher'];
$genre = $a['genre'];
$pegi = $a['pegi'];
$desc = $a['description'];
$price = $a['price'];
echo "<tr>";
echo "<td><input type='checkbox' name = 'BasketAdd[]' value = '$ref' /></td><td>" . $title . "</td><td> " . $plat . "</td><td>" . $pub . "</td><td>" . $genre . "</td><td>" . $pegi . "</td><td>" . $desc . "</td><td>" . $price . "</td>"; echo "</tr>";
}
echo "<input type='submit' value='Add To Basket'/>";
echo "</form>";
}
?>
</table>
</div>
</body>
</html>
My problem is that when a user doesn't select a genre and price, the database doesn't display. For example, on my website nothing comes up, except for when I enter a genre and price range at which point the correct items are displayed. The else condition at the bottom of my page is meant to display the database normally if no fields (genre and price) have been selected. Can anyone see the problem here?
Thanks in advance! :)
Aucun commentaire:
Enregistrer un commentaire