1

我正在尝试按价格对 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,并且初始表格数据工作正常。

4

3 回答 3

0

您从未将$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可用的!你正在尝试$_GET一些不存在的东西。

你需要做的是:

 IF ($_GET['sort'] == 'Price: Highest First'){
                    $data1 .=" ORDER BY price ASC";
                } else { 
                    $data1 .=" ORDER BY price DESC";
}
于 2012-10-23T23:49:16.450 回答
0

代替

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

if ( !empty($_GET['sort']) && $_GET['sort'] == 'PriceAsc' ) {
    $orderby=" ORDER BY price ASC";
} 
if ( !empty($_GET['sort']) && $_GET['sort'] == 'PriceDesc' ) {
    $orderby=" ORDER BY price DESC";
}

$data = mysql_query("SELECT * FROM products".$orderby);

注意更改,您需要使用 empty() 检查表单是否已提交并检查其值。并执行一次查询 Order by 附加到 SQL。

于 2012-10-23T23:54:19.410 回答
0

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

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

http://www/mypage.php?PriceAsc=true

你需要正确比较它

 if ($_GET['PriceAsc']=='true'){
于 2012-10-23T23:55:58.507 回答