【问题标题】:How can I convert this untidy table header sorting code to well compact function?如何将这个凌乱的表头排序代码转换为紧凑的函数?
【发布时间】:2016-05-09 23:12:17
【问题描述】:

我已经编写了一个代码 sn-p 来在 PHP/MySQL 中对我的表头进行排序。表头的 HTML 以及 PHP 嵌入代码由以下给出:

<tr>
    <th><a href="?orderby=id<?php if($orderby == '`post_id`' && $order == 'asc') echo '&order=desc'; else echo '&order=asc'; ?><?php echo $q; ?>" title="<?php echo $title_th1; ?>">ID</a>&nbsp;<?php echo $icon_th1; ?></th>
    <th><a href="?orderby=name<?php if($orderby == '`post_name`' && $order == 'asc') echo '&order=desc'; else echo '&order=asc'; ?><?php echo $q; ?>" title="<?php echo $title_th2; ?>">Name</a>&nbsp;<?php echo $icon_th2; ?></th>
    <th><a href="?orderby=url<?php if($orderby == '`post_url`' && $order == 'asc') echo '&order=desc'; else echo '&order=asc'; ?><?php echo $q; ?>" title="<?php echo $title_th3; ?>">URL</a>&nbsp;<?php echo $icon_th3; ?></th>
    <th><a href="?orderby=title<?php if($orderby == '`p`.`post_title`' && $order == 'asc') echo '&dir=desc'; else echo '&dir=asc'; ?><?php echo $q; ?>" title="<?php echo $title_th4; ?>">Title</a>&nbsp;<?php echo $icon_th4; ?></th>
</tr>

处理传入的 GET 请求并确定正确的表排序顺序列和排序方向(即 asc 或 desc)的 PHP 代码如下所示:

// Sorting
if(isset($_GET['orderby']))
{
    $orderby = $_GET['orderby'];

    switch($orderby)
    {
        case "id":
        $orderby = "post_id";
        break;
        case "name":
        $orderby = "post_name";
        break;
        case "url":
        $orderby = "post_url";
        break;
        case "title":
        $orderby = "post_title";
        break;
        default:
        $orderby = "post_name";
    }
}

// Sort direction
if(isset($_GET['order']) && in_array($_GET['order'], array('asc', 'desc')))
{
    $order = $_GET['order'];
}

// Get dynamic sort direction icon and anchor title
if($orderby == 'post_id')
{
    if($order == 'asc')
    {
        $icon_th1 = '<img src="images/arrow_up.png" class="arrow_dir">';
        $title_th1 = 'Click to sort results descending';
    }
    else
    {
        $icon_th1 = '<img src="images/arrow_down.png" class="arrow_dir">';
        $title_th1 = 'Click to sort results ascending';
    }
    $icon_th2 = '';
    $title_th2 = 'Click to sort results ascending';
    $icon_th3 = '';
    $title_th3 = 'Click to sort results ascending';
    $icon_th4 = '';
    $title_th4 = 'Click to sort results ascending';
}

elseif($orderby == 'post_name')
{
    if($order == 'asc')
    {
        $icon_th2 = '<img src="images/arrow_up.png" class="arrow_dir">';
        $title_th2 = 'Click to sort results descending';
    }
    else
    {
        $icon_th2 = '<img src="images/arrow_down.png" class="arrow_dir">';
        $title_th2 = 'Click to sort results ascending';
    }
    $icon_th1 = '';
    $title_th1 = 'Click to sort results ascending';
    $icon_th3 = '';
    $title_th3 = 'Click to sort results ascending';
    $icon_th4 = '';
    $title_th4 = 'Click to sort results ascending';
}

elseif($orderby == 'post_url')
{
    if($order == 'asc')
    {
        $icon_th3 = '<img src="images/arrow_up.png" class="arrow_dir">';
        $title_th3 = 'Click to sort results descending';
    }
    else
    {
        $icon_th3 = '<img src="images/arrow_down.png" class="arrow_dir">';
        $title_th3 = 'Click to sort results ascending';
    }
    $icon_th1 = '';
    $title_th1 = 'Click to sort results ascending';
    $icon_th2 = '';
    $title_th2 = 'Click to sort results ascending';
    $icon_th4 = '';
    $title_th4 = 'Click to sort results ascending';
}

elseif($orderby == 'post_title')
{
    if($order == 'asc')
    {
        $icon_th4 = '<img src="images/arrow_up.png" class="arrow_dir">';
        $title_th4 = 'Click to sort results descending';
    }
    else
    {
        $icon_th4 = '<img src="images/arrow_down.png" class="arrow_dir">';
        $title_th4 = 'Click to sort results ascending';
    }
    $icon_th1 = '';
    $title_th1 = 'Click to sort results ascending';
    $icon_th2 = '';
    $title_th2 = 'Click to sort results ascending';
    $icon_th3 = '';
    $title_th3 = 'Click to sort results ascending';
}
?>

此代码可以正常工作。但我在想的是把它转换成更整洁紧凑的东西,也可能是一些功能。因为代码长度随着表头数量的增加和我们需要在排序过程中使用的列数而增加。目前我们只有 4 个&lt;th&gt; 参与排序。但是,如果我们拥有的数量超过 7 个、8 个左右呢?因为几乎所有行中都重复了相同的代码,所以我想知道我们是否可以将其转换为可以在任何我们想要使用排序的地方调用的函数。

其次,我还在考虑使用一些数组和循环将我的&lt;tr&gt;&lt;th&gt;...&lt;/th&gt;&lt;/tr&gt; HTML 代码从静态转换为动态。可能吗?如果没有,那没问题,我的主要重点是解决我刚才讨论的第一个查询。

【问题讨论】:

    标签: php mysql sorting


    【解决方案1】:

    看看这是不是你的想法,可能很接近:

    function renderButton($raw_value,$title_value)
        {
            // Take the raw column name and prepend for later-comparison
            $to_orderby =   'post_'.$raw_value;
            // Check if orderby is not empty, assign it or assign a default
            $orderby    =   (!empty($_GET['orderby']))? 'post_'.$_GET['orderby'] : 'post_id';
            // Check if asc or desc, if neither, default asc
            $order      =   (!empty($_GET['order']) && in_array($_GET['order'], array('asc', 'desc')))?  $_GET['order'] : 'asc';
            // Choose by order
            $icn_dir    =   ($order == 'asc')? 'up' : 'down';
            // Choose by order
            $aTitle     =   ($order == 'asc')? 'ascending' : 'decending';
            // Combine string with variable
            $icn        =   '<img src="images/arrow_'.$icn_dir.'.png" class="arrow_dir">';
            // Combine string with variable
            $title      =   'Click to sort results '.$aTitle;
            // Do a comparison
            $matched    =   ($orderby == $to_orderby);
            // Create a cached string
            ob_start();
    ?>
            <a href="?orderby=<?php echo $raw_value; echo ($matched && $order == 'asc')? '&order=desc' : '&order=asc'; ?><?php //echo $q; ?>" title="<?php if($matched) echo $title; ?>"><?php echo $title_value; ?></a>&nbsp;<?php if($matched) echo $icn; ?>
    <?php
            $data   =   ob_get_contents();
            ob_end_clean();
            // Return the cached string
            return $data;
        }
    
    // Render all the buttons
    echo renderButton('id','ID').PHP_EOL;
    echo renderButton('one','One').PHP_EOL;
    echo renderButton('two','Two').PHP_EOL;
    echo renderButton('three','3').PHP_EOL;
    

    【讨论】:

    • 是的,但它似乎有点复杂。
    • 复杂怎么办?它是可扩展的,我摆脱了大约 96 行。它从 110 左右变成了 17。
    • 为了清楚起见,我已经注释了,希望您能够更容易地破译它(我假设复杂,您的意思是脚本在逻辑上更复杂?)。
    • 复杂性是指该函数使用了一些不规则的东西,例如输出缓冲和常量 PHP_EOL。我们真的需要它们吗?
    • 哦,我刚刚注意到您在其中放置了几个有用的 cmets。谢谢!
    猜你喜欢
    • 2015-01-26
    • 1970-01-01
    • 2016-03-23
    • 1970-01-01
    • 2021-05-11
    • 2018-05-20
    • 2011-04-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多