2013-01-15 26 views
-1

我基本上是創建一個網站,它是一個銷售手機的網站。我需要找到一種使用PHP創建下拉框的方式,可以按照型號,品牌,屏幕大小等對手機進行分類(字段已存在於我的SQL數據庫中)。這個最簡單的方法是什麼?我對PHP很陌生,不太清楚如何去做。PHP使用下拉框排序

+4

[你有什麼試過嗎?](http://mattgemmell.com/2008/12/08/what-have-you-tried/) –

+0

你使用的是PHP-GTK還是你的意思是「使用PHP創建一個下拉框「? – feeela

+0

我有一個數據庫表中的數據通過PHPMyAdmin,我有一個網站,顯示這些數據。但是,我需要創建一個下拉框,可以按品牌,價格(升序/降序)等對產品進行分類。 – user2052241

回答

1

您處理排序在你的MySQL查詢:

SELECT * FROM <<table>> ORDER BY 'Brand' DESC 

,然後調用你的頁面的結果;由於查詢稱爲服務器端,因此無法通過javascript(用戶端)重新排序查詢。

1

您可以使用$_GET變量來實現這一點。這樣

<?php 
    // Let's say that my products category is a $_GET variable 
    $category_id = isset($_GET['category_id']) && is_numeric($_GET['category_id']) ? $_GET['category_id'] : 1; // where 1 is a default category to show! 
?> 

<select onchange="if(this.value != '') document.location = '/my-phones-category.php?category_id=<?php echo $category_id; ?>&order_by=' + this.value"> 
    <option value="">Choose an option</option> 
    <option value="model"<?php if(isset($_GET['order_by']) && $_GET['order_by'] == 'model') echo ' selected="selected"'; ?>>Model</option> 
    <option value="brand"<?php if(isset($_GET['order_by']) && $_GET['order_by'] == 'brand') echo ' selected="selected"'; ?>>Brand</option> 
</select> 

然後,我們看到的東西,我們怎樣才能使我們的查詢,添加正確的順序

<?php 
    switch($_GET['order_by']) { 
    case 'model': 
     $order_by = " ORDER BY products.product_model"; 
     break; 
    case 'brand': 
     $order_by = " ORDER BY products.product_brand"; 
     break; 
    default: 
     $order_by = " ORDER BY products.product_price"; 
    } 

    // Now we have the order and we can make the query 

    $sql = $mysqli->query("SELECT product_name, product_price 
         FROM products 
         WHERE category_id='" . $category_id . "' 
         $order_by"); 
?> 

祝你好運!