简体   繁体   English

按XXX排序,按ASC或DESC排序,动态排序,mysql…

[英]order by XXX sort by ASC or DESC , dynamic ordering, mysql…

this is more of a query than a problem. 这更多的是查询而不是问题。 I would like to create dynamic sorting for my mysql database results. 我想为我的mysql数据库结果创建动态排序。 I currently output the results with a query. 我目前通过查询输出结果。 I would like to create a 2 links which would sort the results by that selected link, click again and the link ASC or DSEC the results. 我想创建一个2链接,该链接将按所选链接对结果进行排序,再次单击,然后将链接ASC或DSEC转换为结果。

I have been searching but am unable to find the right way about doing this. 我一直在搜索,但无法找到正确的方法。

I even downloaded a framework to see how this is done but no to success. 我什至下载了一个框架,以了解如何完成此任务,但没有成功。 the example is like follows: 示例如下:

TITLE TOTAL 标题总计

As simple as this sounds I am unable to find a dynamic example online. 听起来很简单,但我无法在线找到动态示例。

ANyone else finding google supplies more results to outdated and forums than actual helpful pages? 还有其他人发现Google提供给过时的论坛和论坛的结果要多于实际的有用页面吗? Even if you sort by last year and relevance. 即使您按去年和相关性进行排序。 Hope someone could give me some advice on this, thanks 希望有人可以给我一些建议,谢谢

<?php

    $order = ($_GET['order'] == 'asc') ? 'asc' : 'desc';

    $sql = "SELECT .... FROM ... WHERE ... ORDER BY TITLE $order";
    mysql_query($sql) or die(mysql_error());
    ...

     $new_order = ($order == 'asc' ) ? 'desc' : 'asc';
?>

<table>
<thead><tr>
    <th><a href="scriptname.php?order=<?php echo $new_order ?>">TITLE</a></th>
    <th>Total</th>
</tr></thead>
etc....
<?php
    // build the basis for the query
    $sql = '
        SELECT 
            `id`,
            `title`,
            `total`
        FROM 
            `my_table`
    ';

    // check for sort field
    $sort_by = isset($_GET['s']) ? $_GET['s'] : false;
    // validate the sort field (avoid Bobby Tables!) and provide default
    switch ($sort_by) {
        case 'title':
        case 'id':
        case 'total':
            break;
        default:
            $sort_by = 'id';
    }

    $sql .= ' ORDER BY '.$sort_by.' ';

    // get the direction, or use the default
    $direction = isset($_GET['d']) ? $_GET['d'] : false;
    if ($direction != 'ASC' && $direction != 'DESC')
        $direction = 'DESC';
    $sql .= $direction;

    // execute query, get results
    $res = mysql_query($sql);
    $results = array();
    if ($res) {
        while ($r = mysql_fetch_assoc($res)) {
            $results[] = $r;
        }
    }

    // used in table heading to indicate sort direciton
    $sort_arrow = ($direction == 'ASC' ? '<img src="up_arrow.png" />' : '<img src="down_arrow.png" />');

    // used to build urls to reverse the current sort direction
    $reverse_direction = ($direction == 'DESC' ? 'ASC' : 'DESC');
?>

<table>
    <thead>
        <th scope="col" class="<?php echo $sort_by == 'id' ? 'sortColumn' : ''; ?>">
            <a href="myscript.php?s=id&d=<?php echo $reverse_direction; ?>">ID</a>
            <?php echo $sort_by == 'id' ? $sort_arrow : ''; ?>
        </th>
        <th scope="col" class="<?php echo $sort_by == 'id' ? 'sortColumn' : ''; ?>">
            <a href="myscript.php?s=title&d=<?php echo $reverse_direction; ?>">Title</a>
            <?php echo $sort_by == 'title' ? $sort_arrow : '';  ?>
        </th>
        <th scope="col" class="<?php echo $sort_by == 'id' ? 'sortColumn' : ''; ?>">
            <a href="myscript.php?s=total&d=<?php echo $reverse_direction; ?>">Total</a>
            <?php echo $sort_by == 'total' ? $sort_arrow : '';  ?>
        </th>
    </thead>
    <tbody>
        <?php
            if (count($results) > 0) {
                foreach ($results as $r) {
                    print '<tr>';
                    print '<th scope="row">'.$r['id'].'</th>';
                    print '<td>'.$r['title'].'</td>';
                    print '<td>'.$r['total'].'</td>';
                    print '</tr>';
                }
            } else {
                print '<tr><td colspan=3>No results found</td></tr>';
            }
        ?>  
    </tbody>
</table>

this is how I did it in C#, you can use session instead of viewstate rest of it will be same. 这就是我在C#中所做的,您可以使用会话代替viewstate,其余的将是相同的。

if (Convert.ToString(ViewState["sortField"]) == SortExpression){
if (ViewState["sortDir"].ToString() == "ASC")
        { ViewState["sortDir"] = "DESC"; }
    else
        { ViewState["sortDir"] = "ASC"; }
    }
else
{ ViewState["sortDir"] = "ASC"; }
ViewState["sortField"] = SortExpression;

You could take the sort variable from the URL with $_GET['sort'] eg. 您可以使用$ _GET ['sort']例如来自URL的sort变量。 site.php?sort=ASC and pass it to the query, remember about sanity checks! site.php?sort = ASC并将其传递给查询,请记住健全性检查!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM