Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP][MySQL] Sortowanie danych
Forum PHP.pl > Forum > Przedszkole
-romaN-
Witam, mam następujący skrypt, który odpowiednio sortuje dane, jednak wydaje mi się zbyt obszerny jak na rolę jaką pełni. Czy idzie to rozwiązać w inny sposób ?
  1. <?php
  2. function sort_users()
  3. {
  4.  
  5.    global $display_users;
  6.  
  7.  
  8.    if($_GET['action'] == 'sort_asc_id')
  9.    {
  10.        $get_users = "SELECT * FROM users ORDER BY id ASC";
  11.        $result = mysql_query ($get_users);
  12.  
  13.        $display_users = array();
  14.        while($row=mysql_fetch_assoc ($result)){
  15.        $display_users[] = array(
  16.              'id' => $row['id'],
  17.              'nick' => $row['nick'],
  18.              'ip' => $row['ip'],
  19.              'date' => $row['date'],
  20.              'status' => $row['status'],
  21.              'city' => $row['city']);
  22.              }
  23.  
  24.    }
  25.  
  26.    if($_GET['action'] == 'sort_desc_id')
  27.    {
  28.        $get_users = "SELECT * FROM users ORDER BY id DESC";
  29.        $result = mysql_query ($get_users);
  30.  
  31.        $display_users = array();
  32.        while($row=mysql_fetch_assoc ($result)){
  33.        $display_users[] = array(
  34.              'id' => $row['id'],
  35.              'nick' => $row['nick'],
  36.              'ip' => $row['ip'],
  37.              'date' => $row['date'],
  38.              'status' => $row['status'],
  39.              'city' => $row['city']);
  40.              }
  41.  
  42.    }
  43.  
  44.  
  45.    if($_GET['action'] == 'sort_asc_nick')
  46.    {
  47.        $get_users = "SELECT * FROM users ORDER BY nick ASC";
  48.        $result = mysql_query ($get_users);
  49.  
  50.        $display_users = array();
  51.        while($row=mysql_fetch_assoc ($result)){
  52.        $display_users[] = array(
  53.              'id' => $row['id'],
  54.              'nick' => $row['nick'],
  55.              'ip' => $row['ip'],
  56.              'date' => $row['date'],
  57.              'status' => $row['status'],
  58.              'city' => $row['city']);
  59.              }
  60.  
  61.    }
  62.  
  63.    if($_GET['action'] == 'sort_desc_nick')
  64.    {
  65.        $get_users = "SELECT * FROM users ORDER BY nick DESC";
  66.        $result = mysql_query ($get_users);
  67.  
  68.        $display_users = array();
  69.        while($row=mysql_fetch_assoc ($result)){
  70.        $display_users[] = array(
  71.              'id' => $row['id'],
  72.              'nick' => $row['nick'],
  73.              'ip' => $row['ip'],
  74.              'date' => $row['date'],
  75.              'status' => $row['status'],
  76.              'city' => $row['city']);
  77.              }
  78.  
  79.    }
  80.  
  81.  
  82.    if($_GET['action'] == 'sort_asc_ip')
  83.    {
  84.        $get_users = "SELECT * FROM users ORDER BY ip ASC";
  85.        $result = mysql_query ($get_users);
  86.  
  87.        $display_users = array();
  88.        while($row=mysql_fetch_assoc ($result)){
  89.        $display_users[] = array(
  90.              'id' => $row['id'],
  91.              'nick' => $row['nick'],
  92.              'ip' => $row['ip'],
  93.              'date' => $row['date'],
  94.              'status' => $row['status'],
  95.              'city' => $row['city']);
  96.              }
  97.  
  98.    }
  99.  
  100.    if($_GET['action'] == 'sort_desc_ip')
  101.    {
  102.        $get_users = "SELECT * FROM users ORDER BY ip DESC";
  103.        $result = mysql_query ($get_users);
  104.  
  105.        $display_users = array();
  106.        while($row=mysql_fetch_assoc ($result)){
  107.        $display_users[] = array(
  108.              'id' => $row['id'],
  109.              'nick' => $row['nick'],
  110.              'ip' => $row['ip'],
  111.              'date' => $row['date'],
  112.              'status' => $row['status'],
  113.              'city' => $row['city']);
  114.              }
  115.  
  116.    }
  117.  
  118.    
  119.    if($_GET['action'] == 'sort_asc_status')
  120.    {
  121.        $get_users = "SELECT * FROM users ORDER BY status ASC";
  122.        $result = mysql_query ($get_users);
  123.  
  124.        $display_users = array();
  125.        while($row=mysql_fetch_assoc ($result)){
  126.        $display_users[] = array(
  127.              'id' => $row['id'],
  128.              'nick' => $row['nick'],
  129.              'ip' => $row['ip'],
  130.              'date' => $row['date'],
  131.              'status' => $row['status'],
  132.              'city' => $row['city']);
  133.              }
  134.  
  135.    }
  136.  
  137.    if($_GET['action'] == 'sort_desc_status')
  138.    {
  139.        $get_users = "SELECT * FROM users ORDER BY status DESC";
  140.        $result = mysql_query ($get_users);
  141.  
  142.        $display_users = array();
  143.        while($row=mysql_fetch_assoc ($result)){
  144.        $display_users[] = array(
  145.              'id' => $row['id'],
  146.              'nick' => $row['nick'],
  147.              'ip' => $row['ip'],
  148.              'date' => $row['date'],
  149.              'status' => $row['status'],
  150.              'city' => $row['city']);
  151.              }
  152.  
  153.    }
  154.  
  155.    if($_GET['action'] == 'sort_asc_city')
  156.    {
  157.        $get_users = "SELECT * FROM users ORDER BY city ASC";
  158.        $result = mysql_query ($get_users);
  159.  
  160.        $display_users = array();
  161.        while($row=mysql_fetch_assoc ($result)){
  162.        $display_users[] = array(
  163.              'id' => $row['id'],
  164.              'nick' => $row['nick'],
  165.              'ip' => $row['ip'],
  166.              'date' => $row['date'],
  167.              'status' => $row['status'],
  168.              'city' => $row['city']);
  169.              }
  170.  
  171.    }
  172.  
  173.    if($_GET['action'] == 'sort_desc_city')
  174.    {
  175.        $get_users = "SELECT * FROM users ORDER BY city DESC";
  176.        $result = mysql_query ($get_users);
  177.  
  178.        $display_users = array();
  179.        while($row=mysql_fetch_assoc ($result)){
  180.        $display_users[] = array(
  181.              'id' => $row['id'],
  182.              'nick' => $row['nick'],
  183.              'ip' => $row['ip'],
  184.              'date' => $row['date'],
  185.              'status' => $row['status'],
  186.              'city' => $row['city']);
  187.              }
  188.  
  189.    }
  190.  
  191.    if($_GET['action'] == 'sort_asc_date')
  192.    {
  193.        $get_users = "SELECT * FROM users ORDER BY date ASC";
  194.        $result = mysql_query ($get_users);
  195.  
  196.        $display_users = array();
  197.        while($row=mysql_fetch_assoc ($result)){
  198.        $display_users[] = array(
  199.              'id' => $row['id'],
  200.              'nick' => $row['nick'],
  201.              'ip' => $row['ip'],
  202.              'date' => $row['date'],
  203.              'status' => $row['status'],
  204.              'city' => $row['city']);
  205.              }
  206.  
  207.    }
  208.  
  209.    if($_GET['action'] == 'sort_desc_date')
  210.    {
  211.        $get_users = "SELECT * FROM users ORDER BY date DESC";
  212.        $result = mysql_query ($get_users);
  213.  
  214.        $display_users = array();
  215.        while($row=mysql_fetch_assoc ($result)){
  216.        $display_users[] = array(
  217.              'id' => $row['id'],
  218.              'nick' => $row['nick'],
  219.              'ip' => $row['ip'],
  220.              'date' => $row['date'],
  221.              'status' => $row['status'],
  222.              'city' => $row['city']);
  223.              }
  224.  
  225.    }
  226.  
  227.    
  228.  
  229. return $display_users;
  230. }
  231. ?>


Później wywołuje np:
  1. <a id="asc" href="?action=sort_asc_id">ASC</a> | <a id="desc" href="?action=sort_desc_id">DESC</a>


Z góry dziękuję za okazaną pomoc.
Pozdrawiam.
phpion
Jasne, że można prościej:
  1. <?php
  2. function sort_users()
  3. {
  4.  
  5.   global $display_users;
  6.    
  7.     $q = 'SELECT * FROM users ORDER BY ';
  8.    
  9.     switch ($_GET['action']) {
  10.        case 'sort_desc_id':
  11.            $q .= 'id DESC';
  12.            break;
  13.        case 'sort_asc_nick':
  14.            $q .= 'nick ASC';
  15.            break;
  16.        // itd itd
  17.        default:
  18.            $q .= 'id ASC';
  19.     }
  20.  
  21.    $result = mysql_query ($q);
  22.  
  23.    $display_users = array();
  24.  while($row=mysql_fetch_assoc ($result)){
  25.    $display_users[] = array(
  26.            'id' => $row['id'],
  27.      'nick' => $row['nick'],
  28.      'ip' => $row['ip'],
  29.      'date' => $row['date'],
  30.      'status' => $row['status'],
  31.      'city' => $row['city']);
  32.    }
  33. }
  34. ?>

Ja bym jednak rozważył rozbicie kolumny do sortowania oraz metody sortowania na osobne zmienne GET np. ?orderBy=nick&ascDesc=0|1 (np. 0 = ASC, 1 = DESC). Ponadto przekazałbym te dane poprzez parametry funkcji, a nie odwołując się poprzez $_GET bezpośrednio w funkcji.
Chrom
sprawdź jakie zapytanie chcesz zrobić /do tego to chyba najlepsza pętla / zapisz do zmiennej i podstaw zmienną do zapytanie MySQL-a

edit:

zgadzam się z przedmówcą nie dawaj takich zapytań przez GET
-romaN-
Zrobiłem tak jak poradziłeś, jednak nie wiem czy dobrze rozdzieliłem...
np.
  1. <?php
  2. case 'nick=ASC':
  3.           $q .= 'nick ASC';
  4.           break;
  5.       case 'nick=DESC':
  6.           $q .= 'nick DESC';
  7.           break;
  8. ?>


Odwołanie:
  1. <?php
  2. <a id="asc" href="?orderBy=nick=ASC">ASC</a> | <a id="desc" href="?orderBy=nick=DESC">DESC</a></div>
  3. ?>

Dziwnie wygląda z dwoma znakami równości, wykazuje oznaki że działa.

Cytat
Ponadto przekazałbym te dane poprzez parametry funkcji, a nie odwołując się poprzez $_GET bezpośrednio w funkcji.

W jaki sposób ? Dodam, że korzystam z smarty. Funkcję sort_users() mam w pliku function.admin.php, który następnie includuje w pliku /admin/users.php, plik ten natomiast wyświetlam w cp_users.tpl.
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.