How can this be done?

Discussion in 'PHP' started by arrezes, Sep 15, 2009.

  1. #1
    my idea is like this

    i have this table in receive.php after getting
    $_POST['acc_no']
    $_POST['acc_holder']
    $_POST['amount']
    from a page say start.php. using these data I am able to get this table querying it from MySQL database

    | Account No | Holder Name | Amount |
    | 2331 | John Doe | $400 |
    | 2332 | Amanda Jack| $350 |
    | 2333 | Ahmed Rio | $700 |
    | 2334 | Mary Jane | $640 |

    I want the title on each column to be a hyperlink so that user able to click on each column title and sort the data accordingly.

    Eg. When a user click on holder name receive.php will sort the data in the table like this and be displayed in receive.php too

    | Account No | Holder Name | Amount |
    | 2333 | Ahmed Rio | $700 |
    | 2332 | Amanda Jack| $350 |
    | 2331 | John Doe | $400 |
    | 2334 | Mary Jane | $640 |

    and if the user want to sort by Amount just click the title Amount and it will be sorted by Amount

    how can this be done?
     
    arrezes, Sep 15, 2009 IP
  2. dweebsonduty

    dweebsonduty Active Member

    Messages:
    131
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    71
    Digital Goods:
    1
    #2
    You links will look something like:
    <a href="receive.php?orderby=account">Account No</a>

    You then write your POSTS to a cookie value or include them in the get link.

    Then you add this if statement to your code

    
    $orderby="";
    if ($_GET["orderby"])
    {
    $fieldname = $_GET["orderby"];
    $orderby = "ORDER BY $fieldname";
    }
    
    PHP:
    Then Include $oderby in your mysql

    "SELECT * FROM Blah where blah = 'blah' $oderby"

    If there is not $_GET then that last part will be blank and ignored.

    Hope that helps
     
    dweebsonduty, Sep 15, 2009 IP
  3. arrezes

    arrezes Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    thanks it works now
     
    arrezes, Sep 16, 2009 IP
  4. caprichoso

    caprichoso Well-Known Member

    Messages:
    433
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    110
    #4
    Avoid using $_GET["orderby"] right into your SQL query string. It's dangerous.

    <a href="receive.php?orderby=C1">Account No</a>

    
    switch($_GET["orderby"]) {
    case 'C1':
      $orderby = "ORDER BY account"
      break;
    case 'C2':
      $orderby = "ORDER BY holder"
      break;
    case 'C3':
      $orderby = "ORDER BY amount"
      break;
    }
    
    PHP:
     
    caprichoso, Sep 16, 2009 IP