0

Hi All

Just have a question

I'm having difficulties in displaying my data in Desc..cant find where i need to put the DESC in my query

this is the code

function.php

<?php
   function pagination($query, $per_page = 10,$page = 1, $url = '?'){        
        $query = "SELECT COUNT(*) as `timestamp` FROM {$query}";
        $row = mysql_fetch_array(mysql_query($query));
        $total = $row['timestamp'];
        $adjacents = "2"; 

        $page = ($page == 0 ? 1 : $page);  
        $start = ($page - 1) * $per_page;                               

        $prev = $page - 1;                          
        $next = $page + 1;
        $lastpage = ceil($total/$per_page);
        $lpm1 = $lastpage - 1;

        $pagination = "";

        if($lastpage > 1)
        {   
            $pagination .= "<ul class='pagination'>";
                    $pagination .= "<li class='details'>Page $page of $lastpage</li>";
            if ($lastpage < 7 + ($adjacents * 2))
            {   
                for ($counter = 1; $counter <= $lastpage; $counter++)
                {
                    if ($counter == $page)
                        $pagination.= "<li><a class='current'>$counter</a></li>";
                    else
                        $pagination.= "<li><a href='{$url}page=$counter'>$counter</a></li>";                    
                }
            }
            elseif($lastpage > 5 + ($adjacents * 2))
            {
                if($page < 1 + ($adjacents * 2))     
                {
                    for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
                    {
                        if ($counter == $page)
                            $pagination.= "<li><a class='current'>$counter</a></li>";
                        else
                            $pagination.= "<li><a href='{$url}page=$counter'>$counter</a></li>";                    
                    }
                    $pagination.= "<li class='dot'>...</li>";
                    $pagination.= "<li><a href='{$url}page=$lpm1'>$lpm1</a></li>";
                    $pagination.= "<li><a href='{$url}page=$lastpage'>$lastpage</a></li>";      
                }
                elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
                {
                    $pagination.= "<li><a href='{$url}page=1'>1</a></li>";
                    $pagination.= "<li><a href='{$url}page=2'>2</a></li>";
                    $pagination.= "<li class='dot'>...</li>";
                    for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
                    {
                        if ($counter == $page)
                            $pagination.= "<li><a class='current'>$counter</a></li>";
                        else
                            $pagination.= "<li><a href='{$url}page=$counter'>$counter</a></li>";                    
                    }
                    $pagination.= "<li class='dot'>..</li>";
                    $pagination.= "<li><a href='{$url}page=$lpm1'>$lpm1</a></li>";
                    $pagination.= "<li><a href='{$url}page=$lastpage'>$lastpage</a></li>";      
                }
                else
                {
                    $pagination.= "<li><a href='{$url}page=1'>1</a></li>";
                    $pagination.= "<li><a href='{$url}page=2'>2</a></li>";
                    $pagination.= "<li class='dot'>..</li>";
                    for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
                    {
                        if ($counter == $page)
                            $pagination.= "<li><a class='current'>$counter</a></li>";
                        else
                            $pagination.= "<li><a href='{$url}page=$counter'>$counter</a></li>";                    
                    }
                }
            }

            if ($page < $counter - 1){ 
                $pagination.= "<li><a href='{$url}page=$next'>Next</a></li>";
                $pagination.= "<li><a href='{$url}page=$lastpage'>Last</a></li>";
            }else{
                $pagination.= "<li><a class='current'>Next</a></li>";
                $pagination.= "<li><a class='current'>Last</a></li>";
            }
            $pagination.= "</ul>\n";     
        }


        return $pagination;
    } 
?>

and this part is the index.php that display the data

<?php

$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');

$dbname = 'mydatabase';
mysql_select_db($dbname);
    //get the function
    include_once ('function.php');

        $page = (int) (!isset($_GET["page"]) ? 1 : $_GET["page"]);
        $limit = 3;

        $startpoint = ($page * $limit ) - $limit;

        //to make pagination
        $statement = "`php_blog` ";  //table name
?>

----------------------------------------------------------------------------

            <?php
                //show records
                $query = mysql_query("SELECT * FROM {$statement} LIMIT {$startpoint} , {$limit}");

                while ($row = mysql_fetch_assoc($query)) {
            ?>
                <div class="record round"><?php echo "<a href=index.php?id=$row[id]> $row[title]"; echo"</a>";?></div>
            <?php    
                }
            ?>
        </div>

    <?php
        echo pagination($statement,$limit,$page);
    ?>

can someone point me where to add the DESC?by the way this is part of pagination..THANKS

2
Contributors
2
Replies
8
Views
4 Years
Discussion Span
Last Post by ehpratah
2

The syntax is:

ORDER BY field DESC

It's usually the last clause before LIMIT.

0

@diafol thanks got it

i put it in this part of the code

 <?php
            //show records
            $query = mysql_query("SELECT * FROM  {$statement}  ORDER BY timestamp DESC LIMIT {$startpoint} ,  {$limit}");

            while ($row = mysql_fetch_assoc($query)) {
  ?>
            <div class="record round"><?php echo "<a href=index.php?id=$row[id]> $row[title]"; echo"</a>";?></div>
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.