基于switch语句的Php顺序

时间:2015-06-30 10:14:04

标签: php sorting

对于作业,(永远不会到达实时网络所以我不关心使用mysql_语句)我必须做一个商店。我从数据库中获取了所有音乐,并从教程中获得了购物车,但我现在仍然坚持如何更改输出表的排序顺序。我尝试过使用这段代码

 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>&nbsp;<a href="cart.php?album">[Sort]</a></td><td><b>Artist</b>&nbsp;<a href="cart.php?artist">[Sort]</a></td><td><b>Genre</b>&nbsp;<a href="cart.php?genre">[Sort]</a></td><td><b>Price</b>&nbsp;<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;

使用每种排序的案例,使用music_id使用默认值,但是当点击<a href="cart.php?album">[Sort]</a>时,它既不会重定向回music.php,也不会更改排序。有没有。更好的方法来执行此任务或b。我可以用这种方式工作吗?

EDIT 新代码,继承了整个功能。这会在实际的音乐页面上调用,这可能就是排序不起作用的原因吗?

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>&nbsp;<a href="cart.php?order=music_album">[Sort]</a></td><td><b>Artist</b>&nbsp;<a href="cart.php?order=music_artist">[Sort]</a></td><td><b>Genre</b>&nbsp;<a href="cart.php?order=music_genre">[Sort]</a></td><td><b>Price</b>&nbsp;<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>';
    }
}
    }

1 个答案:

答案 0 :(得分:0)

从您提供的代码段中,可能是函数products()由于某种原因未在页面重新加载时调用。

此外,您实际上并不需要switch()语句。更好的方法是直接在页面上提供订单变量的价值。

1)将“cart.php?album”中的链接修复为“cart.php?order = album”(当然还有其他订单)

然后像这样做sonething而不是switch():

function products() 
{
    $possible_orders=['music_album', 'music_artist', ... ];
    $order=$_GET['order'];
    if (! in_array($order, $possible_orders))
       $order='music_id'; // rudimentary safety check

    $get=mysql_query(.... ' ORDER BY '.$order);
...