1

我正在为某人做一个库存系统。我希望能够单击表头中的链接并使其按 PN 和 ASC 排序。然后,如果我再次单击 PN,按 DESC。但我也想通过描述订购它并做同样的事情。到目前为止,这是我的代码。我无法弄清楚如何让它轻松交换方向(ASC,DESC)。

            if (!isset($cd))
        {
            $cd = 0;
        }
        if (isset($_SESSION['direction']) && $cd == 1)
        {
            if ($_SESSION['direction'] == 'DESC')
            {
                $_SESSION['direction'] = 'ASC';
                $cd = 0;
            } elseif ($_SESSION['direction'] == 'ASC') 
            {
                $_SESSION['direction'] = 'DESC';
                $cd = 0;
            }
        } else
        {
            $_SESSION['direction'] = 'ASC';
        }
        if (isset($_REQUEST['sort']))
        {
            if ($_REQUEST['sort'] == 'pn')
            {
                $sql=mysql_query("select * from inventory ORDER BY pn {$_SESSION['direction']}");
            } elseif ($_REQUEST['sort'] == 'description') {
                $sql=mysql_query("select * from inventory ORDER BY description {$_SESSION['direction']}");
            } elseif ($_REQUEST['sort'] == 'wholesale') {
                $sql=mysql_query("select * from inventory ORDER BY wholesale {$_SESSION['direction']}");
            } elseif ($_REQUEST['sort'] == 'list') {
                $sql=mysql_query("select * from inventory ORDER BY list {$_SESSION['direction']}");
            } elseif ($_REQUEST['sort'] == 'stock') {
                $sql=mysql_query("select * from inventory ORDER BY stock {$_SESSION['direction']}");
            } elseif ($_REQUEST['sort'] == 'location') {
                $sql=mysql_query("select * from inventory ORDER BY location {$_SESSION['direction']}");
            }
        } else {
            $sql=mysql_query("select * from inventory ORDER BY pn {$_SESSION['direction']}");
        }      
        echo "<center><table class=\"myTable\">
        <th><a href=\"inventory.php?mode=list&sort=pn\">PN</a></th><th><a href=\"inventory.php?mode=list&sort=description\">Description</a></th><th><a href=\"inventory.php?mode=list&sort=wholesale\">Wholesale</th><th><a href=\"inventory.php?mode=list&sort=list\">List</th><th><a href=\"inventory.php?mode=list&sort=stock\">Stock</th><th><a href=\"inventory.php?mode=list&sort=location\">Location</th><th>Links</th>";
        while ($result=mysql_fetch_array($sql))
        {
            echo "<tr><td>{$result['pn']}</td><td>{$result['description']}</td><td>{$result['wholesale']}</td><td>{$result['list']}</td><td>{$result['stock']}</td><td>{$result['location']}</td><td>[<a href='inventory.php?mode=edit&id={$result['id']}'>Edit</a>]  [<a href='inventory.php?mode=delete&id={$result['id']}'>Delete</a>]  [<a href='orders.php?mode=list_c&id={$result['id']}'>View Orders</a>]</tr>";
        }
        echo "</table></center>";
4

4 回答 4

1

我不会使用会话进行排序。

只需在您的查询字符串中添加一个方向,然后在您的 php 中针对白名单进行检查。如果不在白名单中,请使用您的默认值。

在您的链接中,您可以使用其他值,例如:

<th><a href="inventory.php?mode=list&sort=pn&sort_direction=<?php echo ($sort_order === 'ASC') ? 'DESC' : 'ASC'; ?>">PN</a></th>

你真的应该切换到 PDO / mysqli 和准备好的语句以避免 sql 注入问题。

于 2012-12-05T14:45:35.977 回答
1

我的天啊。

看看你所拥有的这个大大缩短的代码:

$sort_column = 'description'; // the default
$sort_dir = 'DESC'; // the default

$columns = array('pn', 'description', 'wholesale', 'name', 'list', 'stock', 'location');

if (isset($_GET['dir']) && in_array($_GET['dir'], array('ASC', 'DESC'))) {
    $sort_dir = $_GET['dir'];
}
if (isset($_GET['sort']) && in_array($_GET['sort'], $columns))
    $sort_column = $_GET['sort'];
}

$sql = "select * from inventory ORDER BY {$sort_column} {$sort_dir}";

$result = mysql_query($sql);

?>

<center><table class="myTable">
    <th><a href="inventory.php?mode=list&sort=pn<?php if($sort_column == 'pn' && $sort_dir == 'DESC') echo '&dir=ASC'; ?>">PN</a></th>
    <th><a href="inventory.php?mode=list&sort=description<?php if($sort_column == 'description' && $sort_dir == 'DESC') echo '&dir=ASC'; ?>">Description</a></th>
    <th><a href="inventory.php?mode=list&sort=wholesale<?php if($sort_column == 'wholesale' && $sort_dir == 'DESC') echo '&dir=ASC'; ?>">Wholesale</th>
    <th><a href="inventory.php?mode=list&sort=list<?php if($sort_column == 'list' && $sort_dir == 'DESC') echo '&dir=ASC'; ?>">List</th>
    <th><a href="inventory.php?mode=list&sort=stock<?php if($sort_column == 'stock' && $sort_dir == 'DESC') echo '&dir=ASC'; ?>">Stock</th>
    <th><a href="inventory.php?mode=list&sort=location<?php if($sort_column == 'location' && $sort_dir == 'DESC') echo '&dir=ASC'; ?>">Location</th><th>Links</th>

<?php while ($result=mysql_fetch_array($sql)) { ?>
    <tr>
        <td><?=$result['pn']?></td>
        <td><?=$result['description']?></td>
        <td><?=$result['wholesale']?></td>
        <td><?=$result['list']?></td>
        <td><?=$result['stock']?></td>
        <td><?=$result['location']?></td>
        <td>[<a href="inventory.php?mode=edit&id=<?=$result['id']?>">Edit</a>]  [<a href="inventory.php?mode=delete&id=<?=$result['id']?>">Delete</a>]  [<a href="orders.php?mode=list_c&id=<?=$result['id']?>">View Orders</a>]</td>
    </tr>
<?php } ?>

</table></center>

但是你不应该使用mysql_*函数,学习PDO或至少mysqli_*

于 2012-12-05T14:57:56.643 回答
0

我接受了你所有的建议,并将它们合二为一!这是我的新代码。谢谢!

            if (isset($_REQUEST['direction']))
        {
            $direction = $_REQUEST['direction'];
        } else
        {
            $direction = 'ASC';
        }
        if (isset($_REQUEST['sort']))
        {
            $sort = $_REQUEST['sort'];
        } else
        {
            $sort = 'pn';
        }
        $sql=mysql_query("select * from inventory ORDER BY {$sort} {$direction}");
        echo "<center><table class=\"myTable\">";
            if ($direction == 'DESC')
            {
                echo "<th><a href=\"inventory.php?mode=list&sort=pn&direction=ASC\">PN</a></th>
                      <th><a href=\"inventory.php?mode=list&sort=description&direction=ASC\">Description</a></th>
                      <th><a href=\"inventory.php?mode=list&sort=wholesale&direction=ASC\">Wholesale</th>
                      <th><a href=\"inventory.php?mode=list&sort=list&direction=ASC\">List</th>
                      <th><a href=\"inventory.php?mode=list&sort=stock&direction=ASC\">Stock</th>
                      <th><a href=\"inventory.php?mode=list&sort=location&direction=ASC\">Location</th>
                      <th>Links</th>";

            } else 
            {
                echo "<th><a href=\"inventory.php?mode=list&sort=pn&direction=DESC\">PN</a></th>
                      <th><a href=\"inventory.php?mode=list&sort=description&direction=DESC\">Description</a></th>
                      <th><a href=\"inventory.php?mode=list&sort=wholesale&direction=DESC\">Wholesale</th>
                      <th><a href=\"inventory.php?mode=list&sort=list&direction=DESC\">List</th>
                      <th><a href=\"inventory.php?mode=list&sort=stock&direction=DESC\">Stock</th>
                      <th><a href=\"inventory.php?mode=list&sort=location&direction=DESC\">Location</th>
                      <th>Links</th>";
            }
于 2012-12-05T15:16:16.290 回答
0
    $_SESSION['sort'] = isset($_REQUEST['sort']) ? $_REQUEST['sort'] : 'DESC';
    $sort = $_SESSION['sort'];

    if (isset($sort) && $sort == 'DESC')
    {
        $columns->name = "<a href='?sort=ASC&pageno=$pageno'> $columns->name </a>";
        echo $columns->name;
    } 
    else
    {
        $columns->name = "<a href='?sort=DESC&pageno=$pageno'> $columns->name </a>";
        echo $columns->name;

    }


//   kamalj27@gmail.com
于 2015-08-01T08:53:21.223 回答