For an assignment, (that will never reach the live web so I'm not concerned about using mysql_ statements) I have to make a store. I'v got all the music to display from the database, and got the cart working from a tutorial, but I'm now stuck on how to change the sort order of the output table. I've tried using this code
function products() {
$order='music_id';
if (isset($_GET['album'])){
$order='music_album' ;
header('Location: '. $page);
}
switch($order){
case 'music_album':
$get = mysql_query('SELECT music_id, music_artist, music_album, music_genre, music_price FROM music WHERE music_stock > 0 ORDER BY music_album');
if (mysql_num_rows($get) == 0) {
echo "There are no products avaliable, sorry.";
}
else {
echo '<table width="100%" border="0px" class="msc"><tr><td><b>Album</b> <a href="cart.php?album">[Sort]</a></td><td><b>Artist</b> <a href="cart.php?artist">[Sort]</a></td><td><b>Genre</b> <a href="cart.php?genre">[Sort]</a></td><td><b>Price</b> <a href="cart.php?price">[Sort]</a></td></tr>';
while ($get_row = mysql_fetch_assoc ($get)) {
echo '<tr> <td> <i>'. $get_row['music_album'] . '</i></td><td>' .$get_row['music_artist'] . '</td><td>' . $get_row['music_genre']. '</td><td>$' . $get_row['music_price'] .'</td><td> <a class="btn" href="cart.php?add='. $get_row['music_id'] . ' ">Add<a></td></tr>';
}}
break;
using a case for each sort, and a default using music_id, but when the<a href="cart.php?album">[Sort]</a>
is clicked it neither redirects back to music.php, or changes the sort. Is there a. a better way to perform this task or b. a way I can get this to work?
EDIT New code, heres the whole function. This gets called on the actual music page, could that be why the sort isn't working?
function products() {
$possible_orders= ['music_album', 'music_artist', 'music_genre', 'music_price'];
if (isset ($_GET['order'])){
if (! in_array($order, $possible_orders)){
$order=$_GET['order'];
}
header('Location: '. $page);
}
else{
$order='music_id';
}
$get = mysql_query('SELECT music_id, music_artist, music_album, music_genre, music_price FROM music WHERE music_stock > 0 ORDER BY '.$order);
if (mysql_num_rows($get) == 0) {
echo "There are no products avaliable, sorry.";
}
else {
echo '<table width="100%" border="0px" class="msc"><tr><td><b>Album</b> <a href="cart.php?order=music_album">[Sort]</a></td><td><b>Artist</b> <a href="cart.php?order=music_artist">[Sort]</a></td><td><b>Genre</b> <a href="cart.php?order=music_genre">[Sort]</a></td><td><b>Price</b> <a href="cart.php?order=music_price">[Sort]</a></td></tr>';
while ($get_row = mysql_fetch_assoc ($get)) {
echo '<tr> <td> <i>'. $get_row['music_album'] . '</i></td><td>' .$get_row['music_artist'] . '</td><td>' . $get_row['music_genre']. '</td><td>$' . $get_row['music_price'] .'</td><td><hr> <a class="btn" href="cart.php?add='. $get_row['music_id'] . ' ">Add<a><hr></td></tr>';
}
}
}