2012-10-23 68 views
1

我試圖按價格對PHP生成的表單進行排序。我收到一條錯誤消息'Notice:Undefined index:PriceDesc/Asc'。我瞭解錯誤信息,但無法通過搜索找到任何相關信息,所以我想問問。我是新來的PHP,到目前爲止,這是我的代碼有:按價格對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與最初提交的數據工作正常。

回答

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); 

注意的變化,你需要用空()檢查,如果表單提交併檢查它的值。然後執行查詢一次Order by SQL。

+0

你在哪裏使用'$ orderby'?你需要把它放在查詢中。 – mpen

+0

錯誤消息已消失。然而,數據仍然沒有適當的排序。在phpMyAdmin中,'price'列的格式爲'£ xx.xx'。這會影響它嗎? – Ciaran

+0

@mark已更正 – xelber

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"; 
} 
+0

我仍然得到同樣的錯誤消息「的通知:未定義指數:PriceAsc」通知:未定義指數:PriceDesc」 – Ciaran

+0

如果網址中省略這些變量,這是通知ÿ你會得到。使用'array_key_exists'或'isset'來避免警告。 – mpen

+0

你確定你沒有錯過代碼中的東西嗎?因爲你甚至從未將訂單附加到查詢中,所以mysql無法獲取這些信息。 –

0

是在$排序追加到$數據如上

,你會需要這些查詢字符串的URL

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

正確地進來,你會需要比較它正確

if ($_GET['PriceAsc']=='true'){ 
相關問題