dongzj2015 2015-06-30 10:14
浏览 94

基于switch语句的Php顺序

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>&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;

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>&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条回答 默认 最新

  • duandaodao6951 2015-06-30 10:37
    关注

    From the snippet you provided, it could be the function products() is not called on page reload for some reason.

    Additionally, you don't really need a switch() statement there. What would be ways better is to provide the value of order in order variable directly at page.

    1) fix the links from 'cart.php?album' to 'cart.php?order=album' (and of course the rest of the orderings)

    then do sonething like this instead of 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);
    ...
    
    评论

报告相同问题?

悬赏问题

  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大