如何将这种不整洁的表头排序代码转换为紧凑的函数


How can I convert this untidy table header sorting code to well compact function?

我已经编写了一段代码片段来对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个<th>参与排序。但是,如果我们拥有的数量超过了7、8个左右呢?因为几乎所有行中都有相同的代码重复,所以我想知道我们能否将其转换为一些函数,这些函数可以在我们想要使用排序的任何地方调用。

其次,我还考虑使用一些数组和循环将<tr><th>...</th></tr> HTML代码从静态转换为动态。有可能吗?如果没有,那么没问题,我的主要关注点是解决上面讨论的第一个问题。

看看这是否是你所想的,它可能很接近:

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;