我正在为某人做库存系统。我希望能够单击表头中的链接并使其按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>";
答案 0 :(得分: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注入问题。
答案 1 :(得分:1)
OMG。
请看这个缩短的代码:
$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>
答案 2 :(得分: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>";
}
答案 3 :(得分: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