Page 1 of 1

need prevoius and next links to go to the corresponding reco

Posted: Wed Feb 03, 2010 10:19 am
by iamroming
Hello coders ,
I need the previos and next links to my query like if somebody looking for an item , the item page must have the links for the previuos and next item here iam pasting my query

Code: Select all

<?
 
$itemid=$_GET['itemid'];
$page_name="paging.php"; // If you use this code with a different page ( or file ) name then change this
 
$eu = ($start - 0);
$limit = 10; // No of records to be shown per page.
$that = $eu + $limit;
$back = $eu - $limit;
$next = $eu + $limit;
 
$query2=" SELECT * FROM items where itemid='$itemid'";
$result2=mysql_query($query2);
echo mysql_error();
$nume=mysql_num_rows($result2);
 
$bgcolor="#f1f1f1";
echo "<TABLE width=50% align=center cellpadding=0 cellspacing=0> <tr>";
echo "<td bgcolor='dfdfdf' >&nbsp;<font face='arial,verdana,helvetica' color='#000000' size='4'>Name</font></td>";
echo "<td bgcolor='dfdfdf' >&nbsp;<font face='arial,verdana,helvetica' color='#000000' size='4'>Class</font></td>";
echo "<td bgcolor='dfdfdf'>&nbsp;<font face='arial,verdana,helvetica' color='#000000' size='4'>Mark</font></td></tr>";
 
 
$query=" SELECT * FROM items where itemid='$itemid limit $eu, $limit ";
$result=mysql_query($query);
echo mysql_error();
 
while($noticia = mysql_fetch_array($result))
{
if($bgcolor=='#f1f1f1'){$bgcolor='#ffffff';}
else{$bgcolor='#f1f1f1';}
echo "<tr >";
echo "<td align=left bgcolor=$bgcolor id='title'>&nbsp;<font face='Verdana' size='2'>$noticia[itemname]</font></td>";
echo "<td align=left bgcolor=$bgcolor id='title'>&nbsp;<font face='Verdana' size='2'>$noticia[itemid]</font></td>";
echo "<td align=left bgcolor=$bgcolor id='title'>&nbsp;<font face='Verdana' size='2'>$noticia[date]</font></td>";
echo "</tr>";
}
echo "</table>";
 
 
echo "<table align = 'center' width='50%'><tr><td align='left' width='30%'>";
if($back >=0) {
print "<a href='$page_name?start=$back'><font face='Verdana' size='2'>PREV</font></a>";
}
 
echo "</td><td align=center width='30%'>";
$i=0;
$l=1;
for($i=0;$i < $nume;$i=$i+$limit){
if($i <> $eu){
echo " <a href='$page_name?start=$i'><font face='Verdana' size='2'>$l</font></a> ";
}
else { echo "<font face='Verdana' size='4' color=red>$l</font>";} /// Current page is not displayed as link and given font color red
$l=$l+1;
 
echo "</td><td align='right' width='30%'>";
if($that < $nume) {
print "<a href='$page_name?start=$next'><font face='Verdana' size='2'>NEXT</font></a>";
}
echo "</td></tr></table>";
 
}
?>

Re: need prevoius and next links to go to the corresponding reco

Posted: Thu Feb 04, 2010 4:51 am
by jaiswarvipin
it is so simple why are making it complicated. Please refer the below code which help you do achieve e the operation.

Code: Select all

 
<?php
    /*
        Place code to connect to your DB here.
    */
    include('config.php');  // include your code to connect to DB.
 
    $tbl_name="";       //your table name
    // How many adjacent pages should be shown on each side?
    $adjacents = 3;
    
    /* 
       First get total number of rows in data table. 
       If you have a WHERE clause in your query, make sure you mirror it here.
    */
    $query = "SELECT COUNT(*) as num FROM $tbl_name";
    $total_pages = mysql_fetch_array(mysql_query($query));
    $total_pages = $total_pages[num];
    
    /* Setup vars for query. */
    $targetpage = "filename.php";   //your file name  (the name of this file)
    $limit = 2;                                 //how many items to show per page
    $page = $_GET['page'];
    if($page) 
        $start = ($page - 1) * $limit;          //first item to display on this page
    else
        $start = 0;                             //if no page var is given, set start to 0
    
    /* Get data. */
    $sql = "SELECT column_name FROM $tbl_name LIMIT $start, $limit";
    $result = mysql_query($sql);
    
    /* Setup page vars for display. */
    if ($page == 0) $page = 1;                  //if no page var is given, default to 1.
    $prev = $page - 1;                          //previous page is page - 1
    $next = $page + 1;                          //next page is page + 1
    $lastpage = ceil($total_pages/$limit);      //lastpage is = total pages / items per page, rounded up.
    $lpm1 = $lastpage - 1;                      //last page minus 1
    
    /* 
        Now we apply our rules and draw the pagination object. 
        We're actually saving the code to a variable in case we want to draw it more than once.
    */
    $pagination = "";
    if($lastpage > 1)
    {   
        $pagination .= "<div class=\"pagination\">";
        //previous button
        if ($page > 1) 
            $pagination.= "<a href=\"$targetpage?page=$prev\">« previous</a>";
        else
            $pagination.= "<span class=\"disabled\">« previous</span>";    
        
        //pages 
        if ($lastpage < 7 + ($adjacents * 2))   //not enough pages to bother breaking it up
        {   
            for ($counter = 1; $counter <= $lastpage; $counter++)
            {
                if ($counter == $page)
                    $pagination.= "<span class=\"current\">$counter</span>";
                else
                    $pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";                 
            }
        }
        elseif($lastpage > 5 + ($adjacents * 2))    //enough pages to hide some
        {
            //close to beginning; only hide later pages
            if($page < 1 + ($adjacents * 2))        
            {
                for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
                {
                    if ($counter == $page)
                        $pagination.= "<span class=\"current\">$counter</span>";
                    else
                        $pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";                 
                }
                $pagination.= "...";
                $pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>";
                $pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>";       
            }
            //in middle; hide some front and some back
            elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
            {
                $pagination.= "<a href=\"$targetpage?page=1\">1</a>";
                $pagination.= "<a href=\"$targetpage?page=2\">2</a>";
                $pagination.= "...";
                for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
                {
                    if ($counter == $page)
                        $pagination.= "<span class=\"current\">$counter</span>";
                    else
                        $pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";                 
                }
                $pagination.= "...";
                $pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>";
                $pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>";       
            }
            //close to end; only hide early pages
            else
            {
                $pagination.= "<a href=\"$targetpage?page=1\">1</a>";
                $pagination.= "<a href=\"$targetpage?page=2\">2</a>";
                $pagination.= "...";
                for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
                {
                    if ($counter == $page)
                        $pagination.= "<span class=\"current\">$counter</span>";
                    else
                        $pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";                 
                }
            }
        }
        
        //next button
        if ($page < $counter - 1) 
            $pagination.= "<a href=\"$targetpage?page=$next\">next »</a>";
        else
            $pagination.= "<span class=\"disabled\">next »</span>";
        $pagination.= "</div>\n";       
    }
?>
 
    <?php
        while($row = mysql_fetch_array($result))
        {
    
        // Your while loop here
    
        }
    ?>
 
<?=$pagination?>
    
 
Simply replace the you query and connection as well table column name at respective place and it will start working for you.

Re: need prevoius and next links to go to the corresponding reco

Posted: Thu Feb 04, 2010 4:59 am
by pbs
You can use this functions as well

Code: Select all

 
function execute($sql)
{
    if ($sql!="")
    {
        $result = mysql_query($sql) or die("Error: ".mysql_errno().":- ".mysql_error());
        if ($result)
            return $result;
        else
            return false;
    }
}
function recordset($result)
{
    if ($result)
    {
        while($row = mysql_fetch_assoc($result))
            $data[] = $row;
    }
    return $data;
}
function pagingPN($sql, $page, $limit, $getvars, $class)
{
    if ($page == "")
        $page = 1;
    if ($limit == 0)
        $limit = 1;
    $tsql = $sql;
    $result = mysql_query($tsql) or die("Error: ".mysql_errno().":- ".mysql_error());
    $total = mysql_num_rows($result);
    $totnumpages = ceil($total/$limit);
    if ($offset < 0)
        $offset = 0;
    else          
        $offset = ($page - 1) * $limit;
    $sql = $sql. "  limit $offset, $limit"; 
    $rs = execute($sql);
    $res = recordset($rs);
    $serial_no = ($page - 1) * $limit;
    
    if ($total > 0)
    {
        $link .= "<font face='verdana' size='1'>Page: <strong>".$page."</strong>&nbsp;of&nbsp;<strong>".$totnumpages."</strong>&nbsp;&nbsp;&nbsp;Goto:&nbsp;</font>";
        
        if ($page > 1)
        {
            $link .= "<a href=".$_SERVER['PHP_SELF']."?page=1$getvars class='".$class."' title='Jump to First Page'><<</a>&nbsp;|&nbsp;";
            $prev = $page - 1;
            $link .= "<a href=".$_SERVER['PHP_SELF']."?page=".$prev."$getvars class='".$class."' title='Goto Previous Page'>Previous</a><span class='".$class."'>&nbsp;|&nbsp;</span>";
        }
        else
        {
            $link .= "<span class='".$class."' title='Jump to First Page'><<</span>&nbsp;|&nbsp;<span class='".$class."' title='Goto Previous Page'>Previous&nbsp;|&nbsp;</span>";
        }
        if ($page < $totnumpages)
        {
            $next = $page + 1;
            $link .= "<a href=".$_SERVER['PHP_SELF']."?page=".$next."$getvars class='".$class."' title='Goto Next Page'>Next</a>&nbsp;|&nbsp;";
            $link .= "<a href=".$_SERVER['PHP_SELF']."?page=".$totnumpages."$getvars class='".$class."' title='Jump to Last Page'>>></a>";
        }
        else
        {
            $link .= "<span class='".$class."' title='Goto Next Page'>Next</span>&nbsp;| <span class='".$class."' title='Jump to Last Page'>>></span>";
        }
    }       
    $retarr["sql"] = $sql;
    $retarr["records"] = $res;
    $retarr["serial_no"] = $_no;
    $retarr["link"] = $link;
    return $retarr;
}   
 

Re: need prevoius and next links to go to the corresponding reco

Posted: Fri Feb 05, 2010 5:01 am
by iamroming
hello sir thannks for both of ur code MR jaiswarvipin and pbs its working nice