Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 按顺序-使用相同链接更改方向_Php_Mysql - Fatal编程技术网

Php 按顺序-使用相同链接更改方向

Php 按顺序-使用相同链接更改方向,php,mysql,Php,Mysql,我正在为某人做一个库存系统。我希望能够单击表标题中的链接,并使其按PN和ASC排序。然后,如果我再次按说明单击PN,但我也希望按说明进行排序,并执行相同的操作。这是到目前为止我的代码。我不知道如何让它轻松地交换方向(ASC、DESC) if(!isset($cd)) { $cd=0; } 如果(isset($_会话['direction'])&&$cd==1) { 如果($_会话['direction']=='DESC') { $_会话['direction']='ASC'; $cd=0; }e

我正在为某人做一个库存系统。我希望能够单击表标题中的链接,并使其按PN和ASC排序。然后,如果我再次按说明单击PN,但我也希望按说明进行排序,并执行相同的操作。这是到目前为止我的代码。我不知道如何让它轻松地交换方向(ASC、DESC)

if(!isset($cd))
{
$cd=0;
}
如果(isset($_会话['direction'])&&$cd==1)
{
如果($_会话['direction']=='DESC')
{
$_会话['direction']='ASC';
$cd=0;
}elseif($\会话['direction']=='ASC')
{
$\会话['direction']='DESC';
$cd=0;
}
}否则
{
$_会话['direction']='ASC';
}
如果(isset($_请求['sort']))
{
如果($_请求['sort']=='pn')
{
$sql=mysql_查询(“按pn{$_会话['direction']}从库存订单中选择*);
}elseif($\u请求['sort']=='description'){
$sql=mysql_查询(“按说明从库存订单中选择*{$_会话['direction']}”);
}elseif($\u请求['sort']=='wholesale'){
$sql=mysql_查询(“按批发从库存订单中选择*{$_会话['direction']}”);
}elseif($\u请求['sort']=='list'){
$sql=mysql_查询(“按列表从库存订单中选择*{$_会话['direction']}”);
}elseif($\请求['sort']=='stock'){
$sql=mysql_查询(“按库存从库存订单中选择*{$_会话['direction']}”);
}elseif($\u请求['sort']=='location'){
$sql=mysql_查询(“按位置从库存订单中选择*{$_会话['direction']}”);
}
}否则{
$sql=mysql_查询(“按pn{$_会话['direction']}从库存订单中选择*);
}      
回声“
]  []  []";
}
回声“;

我不会将会话用于排序顺序

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

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

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

您应该切换到PDO/mysqli和prepared语句,以避免sql注入问题。

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>
$sort_column='description';//默认值
$sort_dir='DESC';//默认值
$columns=数组('pn','description','wholesale','name','list','stock','location');
if(isset($_GET['dir'])和&in_数组($_GET['dir'],数组('ASC','DESC')){
$sort_dir=$_GET['dir'];
}
if(isset($\u GET['sort'])和&in_数组($\u GET['sort'],$columns))
$sort\u column=$\u GET['sort'];
}
$sql=“通过{$sort\u column}{$sort\u dir}从库存订单中选择*”;
$result=mysql\u查询($sql);
?>
]  []  []

但是你不应该使用
mysql.*
函数,学习或者至少我采纳了你所有的建议,并将它们混合在一起!这是我的新代码。谢谢

            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>";
            }
if(isset($\u请求['direction']))
{
$direction=$_请求['direction'];
}否则
{
$direction='ASC';
}
如果(isset($_请求['sort']))
{
$sort=$_请求['sort'];
}否则
{
$sort='pn';
}
$sql=mysql_查询(“按{$sort}{$direction}从库存订单中选择*);
回声“;
如果($direction=='DESC')
{
回声“
批发
列表
股票
位置
链接”;
}
$\u会话['sort']=isset($\u请求['sort'])$_请求['sort']:'DESC';
$sort=$\会话['sort'];
if(isset($sort)&&$sort=='DESC')
{
$columns->name=“”;
echo$columns->name;
} 
其他的
{
$columns->name=“”;
echo$columns->name;
}
//   kamalj27@gmail.com

我的狗告诉我这里有很多代码气味。哇!气味更像是臭气……为什么方向在会议中?还有你的链接是什么样子的。我想一页一页地传递会更容易。。在桌子的底部。你没听说吗?谢谢!我将对准备好的报表做一些研究。。。这是我很快就会调查的事情。这是什么?在($sort_order=='ASC')?'DESC':'ASC';。。。我从未见过..@alexander7567这是一个三元运算符,它是if/else语句的简写,所以基本上:
if($sort_order=='ASC'){echo'DESC';}else{echo'ASC';}
现在开始学习mysqli!附言我还想知道是什么原因?也就是说,这叫做内部运算符。它是if($sort_order=='ASC')回显'DESC'的缩写;否则回声“ASC”。谷歌搜索
php内部运算符
,了解更多信息。。。
            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>";
            }
    $_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