我有一个html表,其中包含从此sql语句中检索到的数据:
$sql = "SELECT log_date, log_id, log_time, network_protocal, client_name, client_ip FROM log_table";
HTML是:
<div class="container">
<table class="table table-striped">
<tr>
<td>Date</td>
<td>Time</a></td>
<td>Network Protocol</td>
<td>Client Name</td>
<td>Client IP</td>
</tr>
<?php foreach ($logs as $log) { ?>
<tr>
<td><?php if (isset($log->log_date)) echo (string)$log->log_date; ?></td>
<td><?php if (isset($log->log_time)) echo (string)$log->log_time; ?></td>
<td><?php if (isset($log->network_protocal)) echo (string)$log->network_protocal; ?></td>
<td><?php if (isset($log->client_name)) echo (string)$log->client_name; ?></td>
<td><?php if (isset($log->client_ip)) echo (string)$log->client_ip; ?></td>
</tr>
<?php } ?>
</table>
我想通过点击表格标题来设置升序和降序排序功能,但我似乎找不到一个简单的解决方案。有人能指出我正确的方向吗?
答案 0 :(得分:1)
here the solution hope this will help you
<?php
$sql = "SELECT log_date, log_id, log_time, network_protocal, client_name, client_ip
FROM log_table $sort";
$sort = "";
if(isset($_GET['sort'])) {
switch ($_GET['sort'] ) {
case 0:
$sort = " ORDER BY log_date ASC";
break;
case 1:
$sort = " ORDER BY log_date DESC";
break;
case 2:
$sort = " ORDER BY log_time ASC";
break;
case 3:
$sort = " ORDER BY log_time DESC";
break;
case 4:
$sort = " ORDER BY network_protocal ASC";
break;
case 5:
$sort = " ORDER BY network_protocal DESC";
break;
case 6:
$sort = " ORDER BY client_name ASC";
break;
case 7:
$sort = " ORDER BY client_name DESC";
break;
case 8:
$sort = " ORDER BY client_ip ASC";
break;
case 9:
$sort = " ORDER BY client_ip DESC";
break;
}
}
?>
<div class="container">
<table class="table table-striped">
<tr>
<td>Date</td>
<td>Time</a></td>
<td>Network Protocol</td>
<td>Client Name</td>
<td>Client IP</td>
</tr>
<?php foreach ($logs as $log) { ?>
<tr>
<td>
<a href="<?php echo $_SERVER['PHP_SELF'] . "?sort=0";?>" >▲</a>
<?php if (isset($log->log_date)) echo (string)$log->log_date; ?>
<a href="<?php echo $_SERVER['PHP_SELF'] . "?sort=1";?>" >▼</a>
</td>
<td>
<a href="<?php echo $_SERVER['PHP_SELF'] . "?sort=2";?>" >▲</a>
<?php if (isset($log->log_time)) echo (string)$log->log_time; ?>
<a href="<?php echo $_SERVER['PHP_SELF'] . "?sort=3";?>" >▼</a>
</td>
<td>
<a href="<?php echo $_SERVER['PHP_SELF'] . "?sort=4";?>" >▲</a>
<?php if (isset($log->network_protocal)) echo (string)$log->network_protocal; ?>
<a href="<?php echo $_SERVER['PHP_SELF'] . "?sort=5";?>" >▼</a>
</td>
<td>
<a href="<?php echo $_SERVER['PHP_SELF'] . "?sort=6";?>" >▲</a>
<?php if (isset($log->client_name)) echo (string)$log->client_name; ?>
<a href="<?php echo $_SERVER['PHP_SELF'] . "?sort=7";?>" >▼</a>
</td>
<td>
<a href="<?php echo $_SERVER['PHP_SELF'] . "?sort=8";?>" >▲</a>
<?php if (isset($log->client_ip)) echo (string)$log->client_ip; ?>
<a href="<?php echo $_SERVER['PHP_SELF'] . "?sort=9";?>" >▼</a>
</td>
</tr>
<?php } ?>
</table>