Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Senior Coder
    Join Date
    May 2006
    Posts
    1,680
    Thanks
    28
    Thanked 4 Times in 4 Posts

    How can I reverse the data order in this script ?

    Hi,
    I have an interesting problem !

    What I am trying to do is to get the last 60 rows of data from my table
    and then process them in data order. Sounds easy ...

    The column "mday_no" is the day number of the row

    So I have used the following this code to put the data in reverse order (by date) so that that I can pick out the last 60 days.

    PHP Code:
    $sql_graph "SELECT mcb_date, data1, data2, data3 FROM my_tab WHERE prod = '$Db_prod' ORDER BY mday_no DESC LIMIT 60";

    $result_graph mysql_query($sql_graph)    or die("could not execute SELECT my_tab"mysql_error()); 
    Now that should get the correct data
    - but I actually want to process this data in day order going from
    low to high because I am going to use it in a graph.

    My script continues to process the data with:

    PHP Code:
    $ctr 1;
    while( 
    $row_graph mysql_fetch_assoc($result_graph)) {
      if( 
    $ctr == 1) {$first_dt date('d-M-Y'$row_graph['mcb_date']);}  // picks out the first date
         
    $data1[] = $row_graph['data1'];
         
    $data2[] = $row_graph['data2'];
         
    $data3[] = $row_graph['data3'];
          
    $ctr++; 
          }

    So does anyone know how I can reverse the data after I have selected it ?

    At the moment the data runs backwards and "$ctr == 1" is going to be the last date not the first.

    Any ideas ?




    .
    Last edited by jeddi; 02-15-2010 at 08:36 AM.
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.

  • #2
    Regular Coder
    Join Date
    Jan 2010
    Posts
    101
    Thanks
    17
    Thanked 5 Times in 5 Posts
    I believe what you need is array_reverse. *I just glanced at the code so I could be wrong..

  • #3
    Senior Coder
    Join Date
    May 2006
    Posts
    1,680
    Thanks
    28
    Thanked 4 Times in 4 Posts
    Thanks.

    That sounds logical !

    So I guess I get my array first with
    the select,
    then get the row array,
    and then reverse it,
    and then do the while loop ?

    So:

    PHP Code:
    $sql_graph "SELECT mcb_date, data1, data2, data3 FROM my_tab WHERE prod = '$Db_prod' ORDER BY mday_no DESC LIMIT 60";

    $result_graph mysql_query($sql_graph)    or die("could not execute SELECT my_tab"mysql_error());  

    $row_graph mysql_fetch_assoc($result_graph);

    $row_graph array_reverse($row_graph);

    $ctr 1;
    while( 
    $row_graph) {
      if( 
    $ctr == 1) {$first_dt date('d-M-Y'$row_graph['mcb_date']);}  // picks out the first date
         
    $data1[] = $row_graph['data1'];
         
    $data2[] = $row_graph['data2'];
         
    $data3[] = $row_graph['data3'];
          
    $ctr++; 
          }

    Does that look about right ?




    .
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.

  • #4
    New Coder
    Join Date
    Mar 2009
    Posts
    90
    Thanks
    5
    Thanked 14 Times in 14 Posts

    Lightbulb

    PHP Code:
    $sql_graph "SELECT * FROM 
    (SELECT mcb_date, data1, data2, data3 FROM my_tab 
    WHERE prod = '$Db_prod' ORDER BY mday_no DESC LIMIT 60) A 
    ORDER BY mday_no ASC"


  • #5
    Senior Coder
    Join Date
    May 2006
    Posts
    1,680
    Thanks
    28
    Thanked 4 Times in 4 Posts
    Hmm .. thats interesting !

    what is that first A in "A ORDER BY mday_no ASC" ?

    haven't seen that before !




    .
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.

  • #6
    New Coder
    Join Date
    Mar 2009
    Posts
    90
    Thanks
    5
    Thanked 14 Times in 14 Posts
    Quote Originally Posted by jeddi View Post
    Hmm .. thats interesting !

    what is that first A in "A ORDER BY mday_no ASC" ?

    haven't seen that before !




    .
    It's derived table alias


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •