【发布时间】: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> <?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> <?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> <?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> <?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 代码从静态转换为动态。可能吗?如果没有,那没问题,我的主要重点是解决我刚才讨论的第一个查询。
【问题讨论】: