I am totally new to MySQL and PHP. I am making a website which displays MySQL results and then there is a select option allowing the viewer to choose how he wants the results sorted and then he is taken to another page with sorted results.
Here is my code for select function:
<div id="query">
<label style="font-weight:bold;display:inline;">Sort By:</label>
<select id="selection">
<option value="">Choose to sort...</option>
<option value="?sort=ranking">Ranking</option>
<option value="?sort=price_asc">Price: Low to high</option>
<option value="?sort=price_desc">Price: High to low</option>
<option value="?sort=title_asc">Title: A to Z</option>
<option value="?sort=title_desc">Title: Z to A</option>
</select>
<script>
document.getElementById("selection").onchange = function() {
if (this.selectedIndex!==0) {
window.location.href = this.value;
}
};
</script>
</div>
And here is the code for alteration of the Query:
$Query = "SELECT * FROM `AhopData` WHERE `Item` = 999";
"For some reason this isn't working. Any help would be really appreciated. Thank You"
Try with LIMIT after ORDER BY Clause
$Query = "SELECT * FROM `GamesData` WHERE `Ranking` <=20";
if ($_GET['sort'] == 'ranking')
{
$Query .= " ORDER BY Ranking ASC";
}
elseif ($_GET['sort'] == 'price_asc')
{
$Query .= " ORDER BY Price ASC";
}
elseif ($_GET['sort'] == 'price_desc')
{
$Query .= " ORDER BY Price DESC";
}
elseif($_GET['sort'] == 'title_asc')
{
$Query .= " ORDER BY Title ASC";
}
elseif($_GET['sort'] == 'title_desc')
{
$Query .= " ORDER BY Title DESC";
}
$Query .= " LIMIT $limit";