2011-08-24 77 views
1

這是一個比查詢更多的問題。我想爲我的mysql數據庫結果創建動態排序。我目前用查詢輸出結果。我想創建一個2鏈接,它將按照所選鏈接排序結果,再次單擊並鏈接ASC或DSEC結果。排序由XXX按ASC或DESC排序,動態排序,mysql ...

我一直在尋找,但無法找到正確的方式來做到這一點。

我甚至下載了一個框架,看看如何完成,但沒有成功。 的例子就像如下:

TITLE TOTAL

像這樣簡單的聲音我無法在網上找到一個動態的例子。

還有其他的發現谷歌提供更多的結果過時和論壇比實際有用的頁面? 即使按照去年和相關性排序。希望有人能夠給我這方面的一些建議,謝謝

+0

您的表模式/查詢在哪裏?你的PHP代碼在哪裏?向我們展示一些代碼,這是您的嘗試,所以我們可以幫助您解決困難。不要帶我們回到我們的代碼夢想... – Shef

回答

3
<?php 
    // build the basis for the query 
    $sql = ' 
     SELECT 
      `id`, 
      `title`, 
      `total` 
     FROM 
      `my_table` 
    '; 

    // check for sort field 
    $sort_by = isset($_GET['s']) ? $_GET['s'] : false; 
    // validate the sort field (avoid Bobby Tables!) and provide default 
    switch ($sort_by) { 
     case 'title': 
     case 'id': 
     case 'total': 
      break; 
     default: 
      $sort_by = 'id'; 
    } 

    $sql .= ' ORDER BY '.$sort_by.' '; 

    // get the direction, or use the default 
    $direction = isset($_GET['d']) ? $_GET['d'] : false; 
    if ($direction != 'ASC' && $direction != 'DESC') 
     $direction = 'DESC'; 
    $sql .= $direction; 

    // execute query, get results 
    $res = mysql_query($sql); 
    $results = array(); 
    if ($res) { 
     while ($r = mysql_fetch_assoc($res)) { 
      $results[] = $r; 
     } 
    } 

    // used in table heading to indicate sort direciton 
    $sort_arrow = ($direction == 'ASC' ? '<img src="up_arrow.png" />' : '<img src="down_arrow.png" />'); 

    // used to build urls to reverse the current sort direction 
    $reverse_direction = ($direction == 'DESC' ? 'ASC' : 'DESC'); 
?> 

<table> 
    <thead> 
     <th scope="col" class="<?php echo $sort_by == 'id' ? 'sortColumn' : ''; ?>"> 
      <a href="myscript.php?s=id&d=<?php echo $reverse_direction; ?>">ID</a> 
      <?php echo $sort_by == 'id' ? $sort_arrow : ''; ?> 
     </th> 
     <th scope="col" class="<?php echo $sort_by == 'id' ? 'sortColumn' : ''; ?>"> 
      <a href="myscript.php?s=title&d=<?php echo $reverse_direction; ?>">Title</a> 
      <?php echo $sort_by == 'title' ? $sort_arrow : ''; ?> 
     </th> 
     <th scope="col" class="<?php echo $sort_by == 'id' ? 'sortColumn' : ''; ?>"> 
      <a href="myscript.php?s=total&d=<?php echo $reverse_direction; ?>">Total</a> 
      <?php echo $sort_by == 'total' ? $sort_arrow : ''; ?> 
     </th> 
    </thead> 
    <tbody> 
     <?php 
      if (count($results) > 0) { 
       foreach ($results as $r) { 
        print '<tr>'; 
        print '<th scope="row">'.$r['id'].'</th>'; 
        print '<td>'.$r['title'].'</td>'; 
        print '<td>'.$r['total'].'</td>'; 
        print '</tr>'; 
       } 
      } else { 
       print '<tr><td colspan=3>No results found</td></tr>'; 
      } 
     ?> 
    </tbody> 
</table> 
+0

感謝大家的回覆,高於我的預期。所以不需要JavaScript?我以爲我會需要JavaScript來動態訂購,會提供你們提供的代碼,非常感謝快速回復的人。 – Renai

+0

也可以通過AJAX通過javascript來實現,但是你沒有提供足夠的細節來說明你已經設置了哪些東西來推測這樣的例子。簡而言之,如果您使用的是AJAX,那麼您將對僅生成表格HTML的php腳本進行AJAX調用。在AJAX請求的回調中,您將替換舊的新HTML。考慮到我們的細節,這又是一個超出了這個答案的範圍。 –

+0

謝謝,沒問題,現在還在玩代碼。你能否推薦一個好的AJAX閱讀來源?人們經常告訴我W3schools是不好的資源..再次感謝您的時間。 – Renai

4
<?php 

    $order = ($_GET['order'] == 'asc') ? 'asc' : 'desc'; 

    $sql = "SELECT .... FROM ... WHERE ... ORDER BY TITLE $order"; 
    mysql_query($sql) or die(mysql_error()); 
    ... 

    $new_order = ($order == 'asc') ? 'desc' : 'asc'; 
?> 

<table> 
<thead><tr> 
    <th><a href="scriptname.php?order=<?php echo $new_order ?>">TITLE</a></th> 
    <th>Total</th> 
</tr></thead> 
etc.... 
0

這是我如何在C#中做它,你可以使用會話,而不是它的視圖狀態其餘部分將是相同的。

if (Convert.ToString(ViewState["sortField"]) == SortExpression){ 
if (ViewState["sortDir"].ToString() == "ASC") 
     { ViewState["sortDir"] = "DESC"; } 
    else 
     { ViewState["sortDir"] = "ASC"; } 
    } 
else 
{ ViewState["sortDir"] = "ASC"; } 
ViewState["sortField"] = SortExpression; 
0

你可以從URL的$ _GET ['sort']例如。 site.php?sort = ASC並將其傳遞給查詢,記住有關理智檢查!

相關問題