#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    34
    Rep Power
    2

    Problem with "News" looping


    Hi,

    The company I now work for had a website made by a different company.
    One of the features of this new website was a news page - which worked fine - until recently.

    It seems to duplicate "News Categories" under the months of September, June and May and I cannot seem to find how to fix it, due to the way that the code is written.

    You can see the example here: http://www.darlingtontowntwinning.co.uk/archive

    There are several different files involved with this also.

    Directly from the archive page, the following is used:
    PHP Code:
    $news=$session->getNews("","","",1);?>
    <? while($article=mysql_fetch_array($news)){?>
    <? 
    $date 
    $article['thedate'];
    $year date('Y'$date);
    $month date('F'$date);
    if (
    $year<1970) continue;
    ?>
    <h4><?=$month." - ".$year;?></h4>
    <nav>
    <? $innernews=$session->getNews("",$month,$year);?>
    <? 
    while($innerarticle=mysql_fetch_array($innernews)){?>
    <a href="/news/<?=$innerarticle['ftitle']?><? if($title==$innerarticle['ftitle']){?> class="active"<? }?>><?=$innerarticle['title']?></a>
    <? }?>
    From the session file (which I believe is called):
    PHP Code:
    function getNews($title,$month,$year,$group){
    global 
    $database;
          return 
    $database->getNews($title,$month,$year,$group);
       } 
    And then from the database file:
    PHP Code:
    function getNews($title,$month,$year,$group){
           if(
    $title){
               
    $q=$this->query("SELECT * FROM ".TBL_NEWS." WHERE ftitle = '$title'" );
               return 
    mysql_fetch_array($q);
           }else if(
    $year && $month){
             
    $q=mysql_query("SELECT * FROM ".TBL_NEWS." WHERE (FROM_UNIXTIME(thedate, '%Y') = '$year') AND (FROM_UNIXTIME(thedate, '%M') = '$month') ORDER BY thedate DESC");
             return 
    $q;
             }else if(
    $group){
                 
    $q=$this->query("SELECT * FROM ".TBL_NEWS." GROUP BY (FROM_UNIXTIME(thedate, '%Y')),(FROM_UNIXTIME(thedate, '%M')) ORDER BY thedate DESC" );
               return 
    $q;
             }else{
               
    $q=$this->query("SELECT * FROM ".TBL_NEWS." ORDER BY thedate DESC" );
               return 
    $q;
           }
           
       } 
    Hopefully someone will be able to help.
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Hi,

    could be an issue with the time zones. Does the database server reside on a different machine than the application itself? What are the time zones of MySQL and PHP?

    The overall code is awful, but this piece is particularly terrible. It first asks MySQL for the distinct months and years, abusing the GROUP BY statement. And then it does the same thing in PHP. If the time zones happen to be different, you end up with repeated entries.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    34
    Rep Power
    2
    I forgot to say that also misses some months out, so i could have:
    -May 2013
    -Article1
    -Article2
    -June 2013
    -Article3
    -Article4
    -June 2013
    -Article3
    -Article4
    -August 2013
    -Article5
    -Article6

    Pretty basic, but you see where i'm going.

    With regards to the timezones in both mysql and php, how do I check these?


    Originally Posted by Jacques1
    Hi,

    could be an issue with the time zones. Does the database server reside on a different machine than the application itself? What are the time zones of MySQL and PHP?

    The overall code is awful, but this piece is particularly terrible. It first asks MySQL for the distinct months and years, abusing the GROUP BY statement. And then it does the same thing in PHP. If the time zones happen to be different, you end up with repeated entries.
  6. #4
  7. Old Fart
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Location
    Chicago
    Posts
    106
    Rep Power
    4
    http://www.php.net/manual/en/function.date-default-timezone-get.php

    http://dev.mysql.com/doc/refman/5.5/en/time-zone-support.html
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    34
    Rep Power
    2
    Originally Posted by richpri
    http://www.php.net/manual/en/function.date-default-timezone-get.php

    http://dev.mysql.com/doc/refman/5.5/en/time-zone-support.html
    PHP time zone is UTC and on testing my database, simply by entering a value with a current timestamp, it equals the same time as the UK (where I am now).

    Is this an issue?

IMN logo majestic logo threadwatch logo seochat tools logo