help with mysql date

Discussion in 'MySQL' started by izlik, Jul 13, 2007.

  1. #1
    Hello, as im not very good with mysql i need to somehelp. currently on my websites newspage it's sorted so that old news is at the top and new news is at the bottom, i wanna switch that around so new news are at the top and old news go down, how is that done? bellow is the code in my php file.

    function showNews()
    {
    global $input,$baseUrl,$baseWeb,$db,$user,$LANG,$template;
        $nowtime=time();
        if($user->uid==0)
        $wherex=' AND public=1';
        $db->setQuery("select * from news
                       where ((start_date<=$nowtime AND end_date>=$nowtime)
                       OR
                       (start_date=0 AND end_date=0)
                       OR
                       (start_date is null AND end_date is null)
                       )
                       $wherex");
        $db->query();
        $rows=$db->loadRowList();
        if(!$rows) return '';
    
        $template->set_filenames(array(
    	'news' => 'news.inc.html')
    	);
        foreach($rows as $row)
        {
            $template->assign_block_vars('news', array(
    				'title' =>$row[title],
                    'publish_date' =>date('m/d/y H:i a',$row[date]),
                    'body' =>$row[body],
    				)
    			    );
    PHP:

     
    izlik, Jul 13, 2007 IP
  2. Clark Kent

    Clark Kent Guest

    Messages:
    122
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Use Order by clause in your query. Desc means descending, asc means ascending order. (by default mysql uses ascending)

    $db->setQuery("select * from news
    where ((start_date<=$nowtime AND end_date>=$nowtime)
    OR
    (start_date=0 AND end_date=0)
    OR
    (start_date is null AND end_date is null)
    )
    $wherex
    order by start_date desc ");
     
    Clark Kent, Jul 14, 2007 IP
  3. izlik

    izlik Well-Known Member

    Messages:
    2,399
    Likes Received:
    50
    Best Answers:
    0
    Trophy Points:
    185
    #3
    i pasted in what you saib above and nothing changed :( here is now all the code for shownews() that was in the php file, maybe i missed someting :(

    function showNews()
    {
    global $input,$baseUrl,$baseWeb,$db,$user,$LANG,$template;
        $nowtime=time();
        if($user->uid==0)
        $wherex=' AND public=1';
        $db->setQuery("select * from news
    where ((start_date<=$nowtime AND end_date>=$nowtime)
    OR
    (start_date=0 AND end_date=0)
    OR
    (start_date is null AND end_date is null)
    )
    $wherex
    order by start_date desc ");
        $db->query();
        $rows=$db->loadRowList();
        if(!$rows) return '';
    
        $template->set_filenames(array(
    	'news' => 'news2.inc.html')
    	);
        foreach($rows as $row)
        {
            $template->assign_block_vars('news', array(
    				'title' =>$row[title],
                    'publish_date' =>date('m/d/y H:i a',$row[date]),
                    'body' =>$row[body],
    				)
    			    );
        }
        $template->assign_var_from_handle('inc_site_news','news');
    }
    function grabRemoteContent($url)
    {
        $content = '';
        if(function_exists('curl_init'))
        {
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_HEADER, 0);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0);
            $content = curl_exec($ch);
            curl_close($ch);
        }
        elseif(function_exists('fsockopen'))
        {
            require_once('includes/http.php');
            $http = new http_class();
            $http->follow_redirect=0;
    
            $error=$http->GetRequestArguments($url,$arguments);
            $arguments["Headers"]["Pragma"]="nocache";
            $arguments["Referer"]=$url;
            $error=$http->Open($arguments);
            if($error=="")
            {
               $error=$http->SendRequest($arguments);
               if($error=="")
               {
                   $headers=array();
                   $error=$http->ReadReplyHeaders($headers);
                   if($error=="")
                   {
                       $red_location = $headers['location'];
                       for(;;)
      		           {
                           $error=$http->ReadReplyBody($body,4096);
                           if(strlen($body)==0) break;
                           $content .= $body;
    		           }
                   }
        	    }
    	    }
    	    $http->Close();
            if(!empty($error))
            {
                $content = $error;
            }
        }
        elseif(ini_get('allow_url_fopen'))
        {
            $content = file_get_contents($url);
        }
        else
        {
            $content = ('could not get contents from $url');
        }
        return $content;
    }
    PHP:
     
    izlik, Jul 15, 2007 IP
  4. Clark Kent

    Clark Kent Guest

    Messages:
    122
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #4
    If this is your date field: 'publish_date' =>date('m/d/y H:i a',$row[date]), try order with date.

    $db->setQuery("select * from news
    where ((start_date<=$nowtime AND end_date>=$nowtime)
    OR
    (start_date=0 AND end_date=0)
    OR
    (start_date is null AND end_date is null)
    )
    $wherex
    order by date desc ");
     
    Clark Kent, Jul 16, 2007 IP