I need help pulling the rows in order of date?

Discussion in 'Programming' started by Kurt Whittingham, May 8, 2012.

  1. #1
    when someone searches through the search bar it shows all the entries with the same keywords.. but i want it to show from newest entries to oldest how would i do that???

    This is the code that i have at the moment
    <?php
               
                $keyword = $_GET['kw'];
                if(!empty($keyword)){
                require('connect.php');
                $keyword = mysql_real_escape_string($keyword);
    
                $page   = intval($_GET['page']);
                $tpages = ($_GET['tpages']) ? intval($_GET['tpages']) : 1; // 20 by default
                $adjacents  = intval($_GET['adjacents']);
               
                $countQuery = "SELECT count(article_id) as count FROM articles WHERE keywords LIKE '%$keyword%'";
                $countResult = mysql_query($countQuery);
                $countArray = mysql_fetch_array($countResult);
                $count = $countArray['count'];
    
                if($count>0){
                echo "<p>";
                echo $count. ' results found for the keywords "' .$keyword;
                echo '"';
                echo "</p>";
                echo "<br />";
    
                           
                $query = "SELECT * FROM articles WHERE keywords LIKE '%$keyword%' OR Description LIKE '%$keyword%'";
                $result = mysql_query($query);
    
                while($row = mysql_fetch_array($result)) {
                    echo ("<img src='arrow.gif'>"); echo "&nbsp;&nbsp;";
                    echo '<a style="font-size: 20px" href="'. $row['url'] .'">'. $row['article_name'] .'</a>';
                    echo "<br />";
                    echo "<hr />";
                    
                }
                }
                else{
                echo "<p>";
                echo 'No results found for the keywords "' .$keyword;
                echo'"';
                echo "</p>";
                
                }
    
    
                }
                
            
                
                echo "<br />";
                if($page<=0)  $page  = 1;
                if($adjacents<=0) $adjacents = 4;
    
                $reload = $_SERVER['PHP_SELF'] . "?tpages=" . $tpages . "&amp;adjacents=" . $adjacents;
                
                include("pagination3.php");
                echo paginate_three($reload, $page, $tpages, $adjacents);
                ?>
    PHP:
    Thanks Kurt
     
    Kurt Whittingham, May 8, 2012 IP
  2. Arttu

    Arttu Member

    Messages:
    139
    Likes Received:
    2
    Best Answers:
    8
    Trophy Points:
    40
    #2
    Replace this:
    $query = "SELECT * FROM articles WHERE keywords LIKE '%$keyword%' OR Description LIKE '%$keyword%'";
    with
    $query = "SELECT * FROM articles WHERE keywords LIKE '%$keyword%' OR Description LIKE '%$keyword%' ORDER BY [replace this with field name that contains timestamp] DESC";

    If you dont have a field that contains timestamp, then create one.
     
    Arttu, May 8, 2012 IP
  3. Kurt Whittingham

    Kurt Whittingham Member

    Messages:
    151
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    46
    #3
    How do i create the timestamp?
     
    Kurt Whittingham, May 8, 2012 IP
  4. michaelhomas83

    michaelhomas83 Peon

    Messages:
    141
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    0
    #4
    just put order by <date field name>
     
    michaelhomas83, May 25, 2012 IP
  5. TeemuGr

    TeemuGr Greenhorn

    Messages:
    26
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #5
    You can also

    ORDER BY (ID-field) DESC
    Code (markup):
     
    TeemuGr, May 25, 2012 IP
  6. Kurt Whittingham

    Kurt Whittingham Member

    Messages:
    151
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    46
    #6
    i sorta cheated and made to colounms, one was the DATE and another i just called posted...

    so i put the date in both and showed the POSTED as DD/MM/YYYY but pulled them in order of DATE
     
    Kurt Whittingham, May 25, 2012 IP