我目前在我的网站上有一个表格,但我需要在表格中的类别旁边有一个图标或其他内容,以使值按字母顺序提升,我不知道如何做到这一点我看了
这是我的代码 - (是的我知道它的不安全w / mysql_connect)
<?php
$username="dfgdfg";
$password="dfgdfg";
$database="dfg";
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
if (isset($_POST['search']))
{
$columbname = $_POST['selectitem'];
$searchterm = $_POST['searchterm'];
$query="SELECT * FROM transactions WHERE agentclient = '$agentclient' AND $columbname LIKE '%$searchterm%'";
$result = mysql_query ($query) or die(mysql_error());
}
else
{
$query="SELECT * FROM transactions WHERE agentclient = '$agentclient'";
$result = mysql_query ($query) or die(mysql_error());
}
$num=mysql_numrows($result);
mysql_close();
?>
<table width="98%" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr valign="bottom" bgcolor="#000000">
<td width="24"><span class="style1b"><strong>No.</strong></span></td>
<td width="105"><span class="style1b"><strong>Date</strong></span></td>
<!--<td width="57"><span class="style1b"><strong>Agent/client</strong></span></td>-->
<td width="120"><span class="style1b"><strong>Property/Description</strong></span></td>
<td width="199"><span class="style1b"><strong>Transaction type </strong></span></td>
<td width="235"><span class="style1b"><strong>Applicable document </strong></span></td>
<td width="58"><span class="style1b"><strong>Received</strong></span></td>
<td width="58"><span class="style1b"><strong>Paid</strong></span></td>
</tr>
<?php
$i=0;
while ($i < $num) {
$f1=mysql_result($result,$i,"id");
$f2=mysql_result($result,$i,"date");
//$f3=mysql_result($result,$i,"agentclientt");
$f4=mysql_result($result,$i,"propertydescription");
$f5=mysql_result($result,$i,"transactiontype");
$f6=mysql_result($result,$i,"applicabledocument");
$f7=mysql_result($result,$i,"received");
$f8=mysql_result($result,$i,"paid");
?>
<tr valign="top" bgcolor="#FFFFFF">
<td><?php echo $f1; ?></td> <!--id-->
<td><?php echo $f2; ?></td> <!--date-->
<!-- <td><?php //echo $f3; ?></td> --><!--agent/client-->
<td><?php echo $f4; ?></td> <!--desc-->
<td><?php echo $f5; ?></td> <!--type-->
<td><a href="/manage/clientdoc/<?php echo $f6; ?>"><?php echo $f6; ?></a></td> <!--document-->
<td><?php echo $f7; ?></td> <!--recived-->
<td><?php echo $f8; ?></td> <!--paid-->
</tr>
<?php
$i++;
}
?>
</table>
答案 0 :(得分:2)
如果你的表有大量的行,不要使用javascript排序。使用服务器端排序。 用于服务器端排序的参考链接 - http://forums.phpfreaks.com/topic/276091-help-with-search-sorting-and-pagination/
答案 1 :(得分:1)
将您的更改为...您可以根据字段名称对数据进行排序..
按字段名ASC排序---或---按字段名DESC订购 注意:默认排序是加入
"SELECT * FROM transactions WHERE agentclient = '$agentclient' AND $columbname LIKE '%$searchterm%' ORDER BY agentclient ASC";
$query="SELECT * FROM transactions WHERE agentclient = '$agentclient' ORDER BY agentclient ASC ";
答案 2 :(得分:0)
当您要检查哪个字段以及他在查询中选择和使用的排序类型时,获取图标添加参数的链接
ORDER BY ASC
或ORDER BY DESC
例如
SELECT * FROM transactions WHERE agentclient = '$agentclient' ORDER BY ASC
你提交它的方式是通过POST提交的,所以最好添加带字段的选择框和带排序类型的收音机/选择框
<select name="sortby">
<option value="agentClient">Client agent</option>
<option value="name">Name</option>
</select>
<select name="orderby">
<option value="desc" selected>Descending</option>
<option value="asc">Ascending</option>
</select>
然后在帖子中添加查询
$sorted_fields_avaiable = array('agentClient', 'name');
$sorting = "";
if(in_array($_POST['sortby'], $sorted_fields_avaiable)
{
$sorting .= "ORDER BY ".$_POST['sortby'];
if($_POST['orderby'] == 'asc') $sorting .= " ASC";
else $sorting .= ' DESC';
}
$query = "SELECT * FROM transactions WHERE agentclient = '$agentclient' $sorting";
请记住,这只是为了向您展示想法,您应该考虑将 mysqli 或 pdo 与绑定参数和预备语句一起使用。