I have a website that gives search results of products. I am using page numbering for splitting up search results from 1 to 10. I need to do some work on this search page. First of all, I want to fix the page numbering system. Currently it is showing a hyperlink for the page we are on. It should not do that. Because of this, users are not able to understand which page they are on. I need to do following things here:
1. Remove hyperlink for page number from the current page and replace it with a normal text (black)
2. If there are not more than 10 results, then it should not show any page number at the bottom.

I would really appreciate if you can help me find a bug and resolve this. Thanks!!

Below is the code I am using for php script:

<div id="lnk" style="text-align: center;">
<?php
$pages=intval($total/$limit);
if ($total%$limit) // has remainder so add one page
{
$pages++;
}
// next we need to do the links to other results
if ($startrow>=1) // bypass PREV link if startrow is 0
{
$prevs=($startrow-$limit);
//echo '&nbsp;&nbsp;<a href="'.$_SERVER['PHP_SELF'].'?startrow='.($prevs).'&q='.($passinput).'" id="lnk">Prev &raquo;</a>';
}
$link=0;
for($i=1;$i<=$pages;$i++)
{
echo '&nbsp;&nbsp;<a id="lnk" href="'.$_SERVER['PHP_SELF'].'?startrow='.($link).'&q='.($passinput).'" >'.$i.'</a>';
$link= $link+$limit;
}
// check to see if last page
if (!((($startrow+$limit)/$limit)==$pages) && $pages!=1)
{
// not last page so give NEXT link
$newr=$startrow+$limit;
//echo '&nbsp;&nbsp;<a href="'.$_SERVER['PHP_SELF'].'?startrow='.($newr).'&q='.($passinput).'" id="lnk">&raquo;Next </a>';
}
$a = $startrow + ($limit);
$p=$a/10;
if ($a > $total) 
{ $a = $total; }
$b = $startrow + 1;
?>
<span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif; font-size: 15px; line-height: 18px; ">
<?php
//echo "<br>Showing results $p of $pages";
mysql_close($con);
?>
</span>
</div>
<br><br>
</div>

This is the code used for CSS:

/* Pagination */
#lnk a {
font-size: 15px;
text-decoration: none;
font-weight: bold;
color: blue;
border: 0px solid #0D62C3;
background-color: ;
padding: 3px;
-moz-border-radius: 3px;
}

#lnk a:hover {
font-size: 15px;
text-decoration: none;
font-weight: bold;
color: blue;
border: 0px solid #600e74;
background-color: ;
padding: 3px;
-moz-border-radius: 3px;
}

#lnk a:active {
font-size: 15px;
text-decoration: none;
font-weight: bold;
color: black;
border: 1px solid #600e74;
background-color: ;
padding: 3px;
-moz-border-radius: 3px;
}

#lnk a:visited {
font-size: 15px;
text-decoration: none;
font-weight: bold;
color: #600e74;
padding: 3px;
}
Member Avatar for LastMitch

@s0bigg

I would really appreciate if you can help me find a bug and resolve this. Thanks!!

OK?

  1. Remove hyperlink for page number from the current page and replace it with a normal text (black)

You are kidding me right? You can't do that? What so hard removing a link and make a regular text from your CSS?

echo '&nbsp;&nbsp;<a id="lnk" href="'.$_SERVER['PHP_SELF'].'?startrow='.($link).'&q='.($passinput).'" >'.$i.'</a>';

If you already identify the source already why can't you do this on your own?

  1. If there are not more than 10 results, then it should not show any page number at the bottom.

if $link=0 then what is $limit= ?

Member Avatar for diafol

SOmething like this maybe:

//intial variables
$numPerPage = 10;
$totalPages = 14; //get this from DB
$paginator = '';

//clean and set page number
$page = (isset($_GET['page'])) ? intval($_GET['page']) : 1;
$page = intval($page);
if($page < 1)$page = 1;
if($page > $totalPages)$page = $totalPages;

//get paginator
if($totalPages > 1){
    if($page > 1)$pageArray[] = "<a href='?page=" . ($page - 1) . "'>&laquo;</a>";
    for($p=1;$p<=$totalPages;$p++){
        $pageArray[] = ($p != $page) ? "<a href='?page=$p'>$p</a>" : "<span>$p</span>";
    }
    if($page < $totalPages)$pageArray[] = "<a href='?page=" . ($page + 1) . "'>&raquo;</a>";
    $paginator = "<div id='paginator'>" . implode(' ', $pageArray) . "</div>";
}

//make record table
//your LIMIT clause calculation etc
$sql = "SELECT field1 FROM table1 LIMIT " . (($page - 1) * $numPerPage) . ",$numPerPage";
echo $sql;
?>

<style>
#paginator span{
    font-weight: bold;  
}
#paginator a:link, #paginator a:visited{
    text-decoration:none;
    display:inline-block;
    color: blue;
    border: solid 1px blue; 
    padding-left: 4px;
    padding-right: 4px;
    height: 20px;
    text-align:center;
}
#paginator a:hover,#paginator a:active{
    font-weight: bold;
    color:#009;
    background-color:#CFF;
}


</style>

<?php echo $paginator;?>
commented: Wow, that's a smooth code! +6

I think this code is better :P I added ability to control how many pages in each direction to show... Just set the variable :) OH also it has a new auto showing -5/+5 button. You can manually change it to -10/+10 or whatever you want.

Thanks for the code to 'hack' up lolz :) I was ripping my hair out trying to pagenate and this finally got it working so seriously thank yoy guys SO much!!!

function pagenator($url = 'account.php') {  

    //intial variables
    $numPerPage = $_SESSION['pPerPage'];        //from user input ;)
    $totalPages = $_SESSION['totalPages'];      //get this from DB
    $maxPagesTShow = 5;             //Number of pages in each direction from current page to show

    devMsg("Total Pgs: " . $totalPages, 3);

    //leave this :)
    $paginator = '';

    //clean and set page number
    $page = (isset($_GET['page'])) ? intval($_GET['page']) : 1;
    $page = intval($page);

    if($page < 1)$page = 1;
    if($page > $totalPages)$page = $totalPages;


    $maxPagesTShow = ($maxPagesTShow * 2);      //mult by 2 :)


    //get paginator
    if($totalPages > 1){


        //DO -5 BUTTON IF > 5 PGS LEFT - OUTSIDE
        if ($page > 5) { $pageArray[] = "<a href='" . $url . "&page=" . ($page - 5) . "'>&laquo;-10</a>"; }

        //DO PREVIOUS PAGE ON THE INSIDE
        if ($page > 1) { $pageArray[] = "<a href='" . $url . "&page=" . ($page - 1) . "'>&laquo;</a>"; }

        for($p=1; $p <= $totalPages; $p++) {

            //confirmed working :)
            if (   (($p > ($page - $maxPagesTShow)) and ($p < ($page + $maxPagesTShow)))   ) {

                $pageArray[] = ($p != $page) ? "<a href='" . $url . "&page=$p'>$p</a>" : "<span>$p</span>";

            }

        }

        //DO NEXT PAGE FIRST - ON THE INSIDE
        if ($page < $totalPages) { $pageArray[] = "<a href='" . $url . "&page=" . ($page + 1) . "'>&raquo;</a>"; }

        //DO +5 BUTTON IF > 5 PGS LEFT - OUTSIDE
        if ($page < (($totalPages) - 5)) { $pageArray[] = "<a href='" . $url . "&page=" . ($page + 5) . "'>+10&raquo;</a>"; }


        $paginator = "<div id='paginator'>" . implode(' ', $pageArray) . "</div>";

    }


    if (!(defined("LIMIT"))) { define("LIMIT", " LIMIT " . (($page - 1) * $numPerPage) . "," . $numPerPage . ""); }

    devMsg($sql, 1);

    return $paginator;  

}

Saweeeet~! Took forever to get that equation lmao

Try this, I added auto showing -5/+5 page links (change to -10/+10 if like), and also there is a variable to show X number of pages on each side of the current page.

Use like: echo pagenator($url); example of my URL passed in echo pagenator('account.php?mode=search&action=display'); then the pageinagor adds on the page number &page=.

function pagenator($url = 'account.php') {  

    //intial variables
    $numPerPage = $YOUR_VARIABLE_HERE;      //from user input ;) (change to your variable)
    $totalPages = $YOUR_VARIABLE_HERE;      //get this from DB   (change to your variable)
    $maxPagesTShow = $YOUR_VARIABLE_HERE;           //Number of pages in each direction from current page to show
                                            //(change to your variable)
    //leave this :)
    $paginator = '';

    //clean and set page number
    $page = (isset($_GET['page'])) ? intval($_GET['page']) : 1;
    $page = intval($page);

    if($page < 1)$page = 1;
    if($page > $totalPages)$page = $totalPages;


    $maxPagesTShow = ($maxPagesTShow * 2);      //mult by 2 :)


    //get paginator
    if($totalPages > 1){


        //DO -5 BUTTON IF > 5 PGS LEFT - OUTSIDE
        if ($page > 5) { $pageArray[] = "<a href='" . $url . "&page=" . ($page - 5) . "'>«-10</a>"; }

        //DO PREVIOUS PAGE ON THE INSIDE
        if ($page > 1) { $pageArray[] = "<a href='" . $url . "&page=" . ($page - 1) . "'>«</a>"; }

        for($p=1; $p <= $totalPages; $p++) {

            //confirmed working :)
            if (   (($p > ($page - $maxPagesTShow)) and ($p < ($page + $maxPagesTShow)))   ) {

                $pageArray[] = ($p != $page) ? "<a href='" . $url . "&page=$p'>$p</a>" : "<span>$p</span>";

            }

        }

        //DO NEXT PAGE FIRST - ON THE INSIDE
        if ($page < $totalPages) { $pageArray[] = "<a href='" . $url . "&page=" . ($page + 1) . "'>»</a>"; }

        //DO +5 BUTTON IF > 5 PGS LEFT - OUTSIDE
        if ($page < (($totalPages) - 5)) { $pageArray[] = "<a href='" . $url . "&page=" . ($page + 5) . "'>+10»</a>"; }


        $paginator = "<div id='paginator'>" . implode(' ', $pageArray) . "</div>";

    }


    if (!(defined("LIMIT"))) { define("LIMIT", " LIMIT " . (($page - 1) * $numPerPage) . "," . $numPerPage . ""); }

    return $paginator;  

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.