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 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Jul 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    how to change the order of months in this script?

    I have an agenda with the latest month coming first.
    I would like to change this: the latest month should come last.

    The change of order within the month is easy (desc -> asc)
    But how do I change the months??

    Here is part of the the script:

    Code:
    $earliest   = agenda::get_earliest($_SESSION['area']);
    $earliest_m = substr($earliest, 5, 2);
    $earliest_y = substr($earliest, 0, 4);
    
    $latest     = agenda::get_latest($_SESSION['area']);
    $latest_m   = substr($latest, 5, 2);
    $latest_y   = substr($latest, 0, 4);
    then:


    Code:
    <?php
    $x = 0;
    for ($i = $latest_y; $i >= $earliest_y; $i--) {
        for ($j = 12; $j > 0; $j--) {
            $printed = false;
            $start = $i . '-' . $j . '-01';
            $end   = $i . '-' . ($j + 1) . '-01';
            $sql = "select id from agenda where eventdate < '" . prepare_var($end) . "' and eventdate >= '" . prepare_var($start) . "'";
            if (strlen($_SESSION['area'])) {
                $sql .= " and entityid = '" . prepare_var($_SESSION['area']) . "'";
            }
            $sql .= " order by eventdate desc";
            $res = mysql_query($sql, $GLOBALS['conn']) or die(mysql_error() . '<br/>' . $sql);
            while ($row = mysql_fetch_row($res)) {
                if (!$printed) { print('<span class="subkop">' . $months[$j] . ' ' . $i . '</span>'); }
                $printed = true;
                $ids[] = $row[0];
                $agendaitem = new agenda($row[0]);
    ?>
    Hope someone can help.
    Thnx in advance

  • #2
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,273
    Thanks
    10
    Thanked 581 Times in 562 Posts
    is this javascript?
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,021
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    No, it's all PHP and MySQL. Wrong forum.

    It's also horrible coding, using one SQL query per month instead of one query for *ALL* dates in the range. The code should be tossed out and started over.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,589
    Thanks
    0
    Thanked 644 Times in 634 Posts
    Quote Originally Posted by Old Pedant View Post
    The code should be tossed out and started over.
    Particularly since it uses the obsolete and soon to be no longer supported mysql_ calls.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,021
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Hopefully, a moderator will move this thread to the MySQL forum, where it belongs.

    In which case this answer will perhaps help:
    Code:
    <?php
    // NOTE: assumes that $earliest and $latest are dates in the format YYYY/MM/DD
    $earliest   = agenda::get_earliest($_SESSION['area']);
    $latest     = agenda::get_latest($_SESSION['area']);
    
    $sql = "SELECT YEAR(eventdate) AS yr, MONTH(eventdate) AS mn, id "
         . " FROM agenda WHERE DATE(eventdate) BETWEEN '$earliest' AND '$latest' ";
    if ( strlen($_SESSION['area']) > 0 ) {
                $sql .= " AND entityid = '" . prepare_var($_SESSION['area']) . "'";
    }
    $sql .= " ORDER BY eventdate DESC";
    
    $curyear = 0;
    $curmonth = 0;
    
    $res = mysql_query($sql, $GLOBALS['conn']) or die(mysql_error() . '<br/>' . $sql);
    while ( $row = mysql_fetch_assoc($res) ) 
    {
        $yr = $row["yr"];
        $mn = $row["mn"];
        $id = $row["id"];
        if ( $mn != $curmonth || $yr != $curyear )
        {
             print('<span class="subkop">' . $months[$mn] . ' ' . $yr . '</span>'); 
             $curyear = $yr;
             $curmonth = $mn;
        }
        $agendaitem = new agenda($id);
        ... I guess you print that agenda item? ...
    
    }
    ?>
    *ONE* SQL query for all dates.

    I don't know what new agenda( ) does, but if it also goes out an makes a SQL query, then it also needs to be rewritten. The whole goal should be to minimize the number of queries made in any web page (or any program, for that matter). Let the database do the work, not the application code.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,021
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Quote Originally Posted by felgall View Post
    Particularly since it uses the obsolete and soon to be no longer supported mysql_ calls.
    Granted, that's bad. But the whole idea of making one query per month is about 10 times worse, in my opinion. Converting from mysql to mysqli is trivial in comparison to needing to completely restructure that horrible horrible coding.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #7
    New to the CF scene
    Join Date
    Jul 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thnx Old Pedant,
    I'll rewrite this script cause it is too old.
    This issue can be closed.


  •  

    Posting Permissions

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