2012-10-28 54 views
0

希望有人可以幫我解決MySQL/PHP排序順序問題。Ajax/PHP下拉菜單中的MySQL排序順序問題

的我在做什麼的簡要概述如下:

我有三個(鏈)的選擇框。第一個框的輸出是「固定的」(它包含我店中的所有頂級類別)。隨後的兩個盒子被填充,取決於在前一個盒子中做出的選擇......標準,簡單的東西,我聽到你說!

這一切都很好,最後一個框中的選擇(字符串)被附加到「搜索結果」頁面 - 正是我所需要的。我試圖組織第二個和第三個盒子的排序順序時出現問題。

這裏是我的代碼的示例:

 public function ShowType() 
    { 
     $sql = "SELECT categories.*, categories_description.categories_name FROM categories, categories_description WHERE categories.categories_id = categories_description.categories_id AND categories.parent_id = $_POST[category] ORDER BY categories_name"; 
     $res = mysql_query($sql,$this->conn); 
     $type = '<option value="0">Select...</option>'; 
     while($row = mysql_fetch_array($res)) 
     { 
     $type .= '<option value="' . $row['categories_name'] . '">' . $row['categories_id'] . '</option>'; 
     } 
     return $type; 
    } 

Ajax的替換(灰色的,非可點擊)

<option value="0">Select...</option> 

部分

<option value="' . $row['categories_name'] . '">' . $row['categories_id'] . '</option> 

一次的選擇(在前面的框中)已經完成。

我的問題是一旦激活盒子時顯示的數組不是按字母順序排列。我想在categories_name的選擇框中對輸出進行排序,但無論我在MySQL查詢中嘗試什麼,都不會有!

由於Ajax更新的方式,輸出到我的「動態」標籤未排序嗎?我是否需要使用PHP重新排序數組,如果有的話,是否有人可以將我指向初學者級別(我討厭稱自己爲中級!)請教如何處理它?

我希望我不會像上次那樣引發更多爭論! :-)

乾杯,

Andy。

+0

[**請勿使用'mysql_ *'功能在新代碼**](http://bit.ly/phpmsql)中,它們不再被維護,並且[deprecation process](http://j.mp/Rj2iVR)已經開始了,參見[**紅框**](http://j.mp/Te9zIL)?瞭解[*準備語句*](http://j.mp/T9hLWi),並使用[PDO](http:// php。 net/pdo)或[MySQLi](http://php.net/mysqli) - [這篇文章](http://j.mp/QEx8IB)將幫助你決定哪個。如果你選擇PDO,[這裏是一個好教程](http://j.mp/PoWehJ)。 – hakre

+0

http://dev.mysql.com/doc/refman/5.0/en/select.html的哪一部分不會引導你成功?查詢數據庫時的值。 – hakre

+0

謝謝你的支持油墨,Hakre。我將着眼於如何替換'mysql_ *'函數。我嘗試對MySQL查詢中的值進行排序(我嘗試了許多不同的SORT BY值,但沒有任何內容似乎做任何事情!),但我會從第二篇文章中閱讀您的鏈接。 – user1742819

回答

0

在您的SQL查詢您有:

ORDER BY categories_name

然而,在用於填充下拉代碼,您有:

<option value="' . $row['categories_name'] . '">' . $row['categories_id'] . '</option>

您顯示categories_idcategories_name排序,所以下拉菜單不會顯示給用戶訂購。

+0

感謝您的評論,MitchS。以前我曾看過這個問題,並且在提出問題之前,我已經交換了你提到的兩個值,但它並沒有改變下拉的結果。你是在暗示'$ row ['xxxxx_xxxxx']'條目之一是多餘的,或者我應該在其中一箇中有另一個值? – user1742819

0

使用它: ORDER BY categories_id

+0

謝謝你的建議,Akhilesh,但這並沒有任何效果......即使這樣做,我也希望按字母順序排列結果,並且ID不是按字母順序排列的,而是它們的創建順序!實際上,無論我如何嘗試對查詢中的列表進行排序,這都是項目顯示的順序!我試過'ORDER BY categories_id''ORDER BY categories_name''ORDER BY categories_description.categories_name',以及其他一切!填充時沒有任何更改下拉菜單的順序!我想我將不得不改變數據庫中的類別ID。 – user1742819

0

嘗試這些查詢: 「SELECT *類別,categories_description.categories_name FROM類別,categories_description WHERE categories.categories_id = categories_description.categories_id AND categories.parent_id =「$ _ POST [類別]」 ORDER BY categories_name 「

」 SELECT類別。*,categories_description。categories_name FROM categories,categories_description WHERE categories.categories_id = categories_description.categories_id AND categories.parent_id ='「。$ _ POST [category]。''ORDER BY categories_name」