我的代码可以按特定的列排序一次,但我无法让它循环到asc、desc和default。现在,我可以点击头一次,它对它进行排序,每次点击之后都没有任何作用。我希望第一次单击排序desc,然后单击第二次单击排序asc,然后再单击返回到默认值(按ID排序),然后循环执行这些命令。到目前为止,有人能帮我改进分类吗?我不完全理解这是如何工作的,但我似乎无法将$sort设置为desc。不过,我不知道如何在第三次单击时将其重置为默认设置。
$field='ID';
$sort='ASC';
$sortOrder='';
$sortISBN='ISBN';
$sortAuthor='Author';
$sortTitle='Title';
if(isset($_GET['sorting']))
{
    if($_GET['sorting']=='ASC')
    {
        $sort='DESC';
        $sortOrder=' ↓';
    }
    else 
    {
        $sort='ASC';
        $sortOrder=' ↑';
    }
}
if(isset($_GET['field']))
{
    if($_GET['field']=='ISBN')
    {
        $field='ISBN';
        $sortISBN='ISBN ' . $sortOrder;
    }
    elseif($_GET['field']=='Author')
    {
        $field='Author';
        $sortAuthor='Author ' . $sortOrder;
    }
    elseif($_GET['field']=='Title')
    {
        $field='Title';
        $sortTitle='Title ' . $sortOrder;
    }
}下面是代码的其他相关部分:
<?php
$query=mysql_query("select * from Books order by $field $sort")  or die(mysql_error());
echo'<table border="1">';
?>
<tr><th colspan="5">Your book list</th>
    <td>
        <form name="New" action="new.php" method="POST">
            <input type="submit" name="New" value="New" title="Add a new entry"/>
            </form>
    </td>
</tr>
<tr>
    <th></th>
    <th><a href="index.php?sorting='.$sort.&field=ISBN"><?php echo $sortISBN; ?></th>
    <th><a href="index.php?sorting='.$sort.&field=Author"><?php echo $sortAuthor; ?></th>
    <th><a href="index.php?sorting='.$sort.&field=Title"><?php echo $sortTitle; ?></th>
    <th></th><th></th>
</tr>
<?php发布于 2016-01-15 19:07:13
问题是因为下面三行,
<th><a href="index.php?sorting='.$sort.&field=ISBN"><?php echo $sortISBN; ?></th>
<th><a href="index.php?sorting='.$sort.&field=Author"><?php echo $sortAuthor; ?></th>
<th><a href="index.php?sorting='.$sort.&field=Title"><?php echo $sortTitle; ?></th>这不是在HTML中使用PHP变量的方式。以上一行应如下所示:
// your code
<th><a href="index.php?sorting=<?php echo $sort ?>&field=ISBN"><?php echo $sortISBN; ?></th>
<th><a href="index.php?sorting=<?php echo $sort ?>&field=Author"><?php echo $sortAuthor; ?></th>
<th><a href="index.php?sorting=<?php echo $sort ?>&field=Title"><?php echo $sortTitle; ?></th>
// your codeSidenote:请不要使用mysql_数据库扩展,它们在PHP5.5中被取消,并且在PHP7.0中被完全删除。使用mysqli或PDO扩展。 functions。
https://stackoverflow.com/questions/34817651
复制相似问题