我正在为某人做一个库存系统。我希望能够单击表头中的链接并使其按 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>";
最佳答案
我的天啊。
看看这段大大缩短的代码:
$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>
关于php - Order BY - 使用相同的链接改变方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13725511/