duancai7568 2011-07-13 04:45
浏览 84
已采纳

如果在SQL中没有想要的排序字段时如何对jqGrid进行排序?

When we use jgGrid, like this:

get_items.php =>

$SQL = "SELECT * FROM items ORDER BY $sidx $sord LIMIT $start , $limit"; 
//$sidx is the index row.  
//ie. $sidx = 'name';

$result = $mysql->query( $SQL ); 

header("Content-type: text/xml;charset=utf-8");
$s = "<?xml version='1.0' encoding='utf-8'?>";
...
//balah balah
...

while ( $row = $mysql->fetch_array( $result ) ) {
...
//Note the getName method.
$s .= "<cell>". getName($row['name']))."</cell>";
...
};

echo $s;


function getName ( name ) {
   if ( name == 'Lane' ) return 'Brian Lane';
   if ( name == 'Kerwin' ) return 'Diane Kerwin';
   ...
   ...
}

Now the question is :

if I sort the jqGrid by ASC, it will show like Diane Kerwin, Brian Lane as Kerwin is before Lane.

How to sort the fields like Brian Lane, Diane Kerwin by ASC?

  • 写回答

1条回答 默认 最新

  • dsa4d4789789 2011-07-13 04:48
    关注

    Preferred solution

    It looks like only the last names are stored in the database. To be able to sort by first name in your SQL query, you'll need to update the name fields in the items table to contain both first and last names. This would be much better — keeping a separate data set embedded in your code will probably cause problems at some point later on.


    If you must:

    If this isn't a possibility and you must keep the if statements for each name, you could map over the result set to fetch the names and then sort. (The example below requires PHP >= 5.3.)

    $SQL = "SELECT * FROM items ORDER BY $sidx $sord LIMIT $start , $limit";
    $result = $mysql->query($SQL);
    
    $rows = array();
    while ( $row = $mysql->fetch_array($result) ) {
        switch ( $row['name'] ) {
            case 'Lane': $row['name'] = 'Brian Lane';
            case 'Kerwin': $row['name'] = 'Diane Kerwin';
        }
    
        array_push($rows, $row);
    }
    
    usort($rows, function($a, $b) { return strcmp($a['name'], $b['name']); });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作