0

我目前在我的网站上有一个表格,但我需要在表格中类别的文本旁边有一个图标或其他内容,以使值按字母顺序升序,我不知道该怎么做,我已经看过了。

在此处输入图像描述

这是我的代码-(是的,我知道它不安全 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>  
4

3 回答 3

2

如果您的表有大量行,请不要使用 javascript 排序。使用服务器端排序。服务器端排序的参考链接 - http://forums.phpfreaks.com/topic/276091-help-with-search-sorting-and-pagination/

于 2013-06-05T07:03:27.927 回答
1

更改您的身份...您可以根据字段名称对数据进行排序..

order by fieldname ASC--- OR --- order by fieldname 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 ";
于 2013-06-05T06:58:38.527 回答
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";

还请记住,这只是为了向您展示想法,您应该考虑使用mysqlipdo与绑定参数和准备好的语句。

于 2013-06-05T06:49:46.973 回答