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

Php 如何将这个不整洁的表头排序代码转换为紧凑的函数?,php,mysql,sorting,Php,Mysql,Sorting,我已经编写了一个代码片段来对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; ?>" ti

我已经编写了一个代码片段来对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';
}
?>
//排序
if(isset($\u GET['orderby']))
{
$orderby=$_GET['orderby'];
交换机($orderby)
{
案例“id”:
$orderby=“post\u id”;
打破
案例“名称”:
$orderby=“post\u name”;
打破
案例“url”:
$orderby=“post\u url”;
打破
案例“标题”:
$orderby=“post\u title”;
打破
违约:
$orderby=“post\u name”;
}
}
//排序方向
if(isset($_GET['order'])和&in_数组($_GET['order'],数组('asc','desc'))
{
$order=$_获取['order'];
}
//获取动态排序方向图标和定位标题
如果($orderby=='post_id')
{
如果($order=='asc')
{
$icon_th1='';
$title_th1='单击以按降序对结果排序';
}
其他的
{
$icon_th1='';
$title_th1='单击以按升序排序结果';
}
$icon_th2='';
$title_th2='单击以按升序排序结果';
$icon_th3='';
$title_th3='单击以按升序排序结果';
$icon_th4='';
$title_th4='单击以按升序排序结果';
}
elseif($orderby=='post\u name')
{
如果($order=='asc')
{
$icon_th2='';
$title_th2='单击以按降序对结果排序';
}
其他的
{
$icon_th2='';
$title_th2='单击以按升序排序结果';
}
$icon_th1='';
$title_th1='单击以按升序排序结果';
$icon_th3='';
$title_th3='单击以按升序排序结果';
$icon_th4='';
$title_th4='单击以按升序排序结果';
}
elseif($orderby=='post\u url')
{
如果($order=='asc')
{
$icon_th3='';
$title_th3='单击以按降序排序结果';
}
其他的
{
$icon_th3='';
$title_th3='单击以按升序排序结果';
}
$icon_th1='';
$title_th1='单击以按升序排序结果';
$icon_th2='';
$title_th2='单击以按升序排序结果';
$icon_th4='';
$title_th4='单击以按升序排序结果';
}
elseif($orderby=='post_title')
{
如果($order=='asc')
{
$icon_th4='';
$title_th4='单击以按降序排序结果';
}
其他的
{
$icon_th4='';
$title_th4='单击以按升序排序结果';
}
$icon_th1='';
$title_th1='单击以按升序排序结果';
$icon_th2='';
$title_th2='单击以按升序排序结果';
$icon_th3='';
$title_th3='单击以按升序排序结果';
}
?>
这段代码没有任何问题。但我想的是把它转换成更整洁紧凑的东西,也可能转换成某种功能。因为代码长度随着表头数量和排序过程中需要使用的列数量的增加而增加。目前我们只有4个
参与排序。但是,如果我们有超过可能是7,8或更多呢?因为几乎所有行中都有相同的代码重复,所以我想知道我们是否可以将其转换为一些函数,在我们想要使用排序的任何地方都可以调用这些函数


其次,我还考虑使用一些数组和循环将我的
..
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;
函数渲染按钮($raw\u值,$title\u值)
{
//获取原始列名并添加前缀,以便以后进行比较
$to_orderby='post_'。$raw_值;
//检查orderby是否为空,分配它或分配默认值
$orderby=(!empty($\u-GET['orderby'])?'post.$\u-GET['orderby']:'post\u-id';
//检查asc或desc是否为默认asc,如果两者均为默认asc
$order=(!empty($_-GET['order'])和&in_-array($_-GET['order'],array('asc','desc'))?$_-GET['order']:'asc';
//点菜
$icn_dir=($order='asc')?'up':'down';
//点菜
$aTitle=($order='asc')?“升序”:“降序”;
//将字符串与变量组合
$icn='';
//将字符串与变量组合
$title='单击以对结果进行排序。$aTitle;
//做一个比较
$matched=($orderby==$to_orderby);
//创建缓存字符串
ob_start();
?>

是的,但它似乎有点复杂。如何复杂?它是可扩展的,我去掉了96行左右。它从110行左右变成了17行。为了清晰起见,我做了记号,希望你能更容易地解读它(我假设复杂,你的意思是脚本在逻辑上更复杂?).说到复杂性,我的意思是函数使用了一些不规则的东西,比如输出缓冲和常量PHP_EOL。我们真的需要它们吗?哦,我刚刚注意到你在那里放了一些有用的注释。谢谢!