Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/64.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 - Fatal编程技术网

按价格对PHP生成的表单进行排序

按价格对PHP生成的表单进行排序,php,Php,我正在尝试按价格对PHP生成的表单进行排序。我收到一条错误消息“注意:未定义索引:PriceDesc/Asc”。我理解错误信息,但无法通过搜索找到任何相关信息,因此我想我会询问。我是PHP新手,到目前为止,我有以下代码: <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

我正在尝试按价格对PHP生成的表单进行排序。我收到一条错误消息“注意:未定义索引:PriceDesc/Asc”。我理解错误信息,但无法通过搜索找到任何相关信息,因此我想我会询问。我是PHP新手,到目前为止,我有以下代码:

<!DOCTYPE html>
<html lang="en">
 <head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <!--[if IE]>
  <link href="blueprint/ie.css" type="text/css" rel="stylesheet">
  <![endif]-->
  <link href="blueprint/screen.css" type="text/css" rel="stylesheet">
  <link href="style.css" type="text/css" rel="stylesheet">

  <title>Enygma Peripherals</title>
</head>

<body>
<div id="wrapper" class="container">
    <div id="top" class="span-24">
        <div id="logo" class="span-5">
            <a href="index.php"><img src="images/logo.png" alt="Enygma"></a>
        </div>
        <div id="nav" class="span-11 last">
            <ul>
                <li><a href="index.php">HOME</a></li>
                <li><a href="products.php">PRODUCTS</a></li>
                <li><a href="about.php">ABOUT</a></li>
                <li><a href="contact.php">CONTACT</a></li>
            </ul>
        </div>

    </div>
    <div id="mainContent" class="span-24">
        <h2>Products</h2>
        <div id="left" class="span-8">
        </div>
        <div id="right" class="span-15 last">
            <form method="get" name="sort">
                <select name="sort" id="sort">
                    <option value="">--Select--</option>
                    <option value='PriceAsc'>Price: Highest First</option>
                    <option value='PriceDesc'>Price: Lowest First</option> 
                </select>
                <input type="submit" value="Sort"/>
            </form>
            <?php
                include("connection.php");

                $data = mysql_query("SELECT * FROM products");
                IF ($_GET['PriceAsc']){
                    $orderby=" ORDER BY price ASC";
                } 
                IF ($_GET['PriceDesc']){
                    $orderby=" ORDER BY price DESC";
                }
                Print "<table border cellpadding=10>"; 
                while($info = mysql_fetch_array( $data )) 
                { 
                Print "<tr>"; 
                Print "<th>Product Number:</th> <td>".$info['product_no'] . "</td> "; 
                Print "<th>Product:</th> <td>".$info['product_name'] . "</td> "; 
                Print "<th>Type:</th> <td>".$info['type'] . "</td> "; 
                Print "<th>Price:</th> <td>".$info['price'] . "</td> "; 
                Print "<th>Availability:</th> <td>".$info['availability'] . " </td></tr>"; 
                } 
                Print "</table>"; 
                ?>
        </div>      
    </div>
    <div id="footer" class="span-24">
        <a href="acknowledgements.html">Acknowledgments</a>
    </div>
</div>

作为记录,我将PhpMyAdmin用于我的后端d/b,初始表格数据工作正常。

您从未将$orderby变量附加到查询。试试这个:

            $data1 ="SELECT * FROM products";
            IF ($_GET['PriceAsc']){
                $data1 .=" ORDER BY price ASC";
            } 
            IF ($_GET['PriceDesc']){
                $data1 .=" ORDER BY price DESC";
            $data = mysql_query($data1);
.=将字符串追加到$data的末尾

您得到未定义索引错误的原因是因为没有可用的PriceAsc!你试图得到一些不存在的东西

您需要做的是:

 IF ($_GET['sort'] == 'Price: Highest First'){
                    $data1 .=" ORDER BY price ASC";
                } else { 
                    $data1 .=" ORDER BY price DESC";
}
替换

$data = mysql_query("SELECT * FROM products");
            IF ($_GET['PriceAsc']){
                $orderby =" ORDER BY price ASC";
            } 
            IF ($_GET['PriceDesc']){
                $orderby =" ORDER BY price DESC";
            }


注意更改,您需要使用empty检查表单是否已提交,并检查其值。然后按追加到SQL的顺序执行一次查询。

是将$sort追加到$data,如上所述

您需要这些查询字符串与url一起正确输入

http://www/mypage.php?PriceAsc=true
你需要正确地比较它

 if ($_GET['PriceAsc']=='true'){

我仍然会收到相同的错误消息“Notice:Undefined index:PriceAsc”Notice:Undefined index:PriceDesc”如果您从URL中省略这些变量,您将得到通知。使用array_key_exists或isset可避免警告。您确定代码中没有遗漏某些内容吗?因为您甚至从未将order by附加到查询,所以mysql无法获取tht信息。@JonahKatz:根据这段代码,$data将是mysql结果,而不是查询字符串。将order by SQL附加到该变量中是不起作用的。@注意,我忽略了他在该变量中运行mysql_查询,您在哪里使用$orderby?您需要将其放入查询中。错误消息已消失。然而,数据仍然没有正确排序。在phpMyAdmin中,“价格”列的格式为“£;xx.xx’。这会影响它吗?
 if ($_GET['PriceAsc']=='true'){