duanmengsuo9302 2010-03-20 14:27
浏览 78
已采纳

ORDER BY column_name help(通过HTML表视图中的链接)(PHP MySQL

My output for my table in HTML has several columns such as userid, name, age, dob.

The table heading is simply the title of the column name, I want this to be a link, and when clicked, the selected column is sorted in order, ASC, and then DESC (on next click). I thought this was pretty straight forward but I'm having some difficulty.

So far, I have produced this, and no output is taken, apart from the URL works by displaying 'users.php?orderby=userid'

<?php
          if(isset($_GET['orderby'])){ 
$orderby = $_GET['orderby']; 
$query_sv = "SELECT * FROM users BY ".mysql_real_escape_string($orderby)." ASC"; 
} 
//default query 
else{ 
$query_sv = "SELECT * FROM users BY user_id DESC"; 
} 
?>


              <tr>
                <th><a href="<?php echo $_SERVER['php_SELF']."?orderby=userid";?>">User ID</a></th>

Hoefully if I get this working, I can sort the users by D.O.B. next also using the same principles. Does anyone have any ideas?

  • 写回答

3条回答 默认 最新

  • dsxmwin86342 2010-03-20 14:45
    关注
    $orders=array("name","price","qty");
    $key=array_search($_GET['orderby'],$orders));
    $orderby=$orders[$key];
    $query="SELECT * FROM `table` ORDER BY $orderby";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大