I have tried to create a simple paggigation and now i am facing a problem,

I am unable to show the number of pages i.e 1 2 3

2 when i click on last page the next button is still coming up can anyone help me out .

                    $rec_limit = 10;

                    $sql = "SELECT count(uid) FROM shortenedurls";
                    $retval = mysqli_query($connection, $sql);
                    if(! $retval )
                      die('Could not get data: ' . mysqli_error());
                    $row = mysqli_fetch_array($retval, MYSQL_NUM );
                    $rec_count = $row[0];

                    if( isset($_GET{'page'} ) )
                       $page = $_GET{'page'} + 1;
                       $offset = $rec_limit * $page ;
                       $page = 0;
                       $offset = 0;
                    $left_rec = $rec_count - ($page * $rec_limit);

                    $sql = "SELECT id, uid, url, longurl ".
                           "FROM shortenedurls ".
                           "LIMIT $offset, $rec_limit";

                    $retval = mysqli_query($connection, $sql);
                    if(! $retval )
                      die('Could not get data: ' . mysqli_error($connection));
                    while($record = mysqli_fetch_array($retval)) {
                                $url        = $record['url']; 
                                $longurl    = $record['longurl']; 
                                $ida        = $record['id']; 

                                echo "<div class='entries'>";
                                echo  "<div class='col-md'><a href='$longurl'>". $longurl   ."</a></div>";
                                echo  "<div class='col-md'><a href='$url'>". $url   ."</a></div>";
                                echo  "<div class='col-md smwidth1'>". $ida ."</div>";
                                echo  "<div class='col-sm smwidth2'><a href='' data-toggle='modal' data-target='#myModal2' ><i class='fa fa-pencil-square-o'></i></a></div>";
                                echo  "<div class='col-sm smwidth2'><a href='includes/delete.php?id=$id&delete=$ida&code=$coupen'><i class='fa fa-trash-o'></i></a></div>";
                                echo  "<div class='clear'></div>";
                                echo  "</div>";


                    if( $page > 0 )
                       $last = $page - 2;
                       $url = "coupon.php?id=$id&code=$coupen&page=$last";
                       echo "<a href='coupon.php?id=$id&code=$coupen&page=$last'>Previous</a> |"; 
                       echo "<a href='coupon.php?id=$id&code=$coupen&page=$page'>Next</a>";
                    else if($page == 0)
                        $last = "";
                        $url = "coupon.php?id=$id&code=$coupen&page=$last";
                        echo "<a href='coupon.php?id=$id&code=$coupen&page=$page'>Next</a>";
                    elseif( $left_rec < $rec_limit )
                        echo "<a href='coupon.php?id=$id&code=$coupen&page=$last'>Previous</a>";


Recommended Answers

All 6 Replies

changed but still the same issue on the second page there are only 4 listings though and still showing the next button as accroding to the condition it should show only last button

I think value of id is missing.

in line 37 $ida = $record['id'];

in line 54

<a href='coupon.php?id=$id&code=$coupen&page=$last'>

if ( $page > 0 )

will be true, so "next" will be displayed. Check you logic.

I put else if as well like if the records are less then 10 so it should show only last button not next..if this logic is wrong can you suggest me i was also thinking of page login but was really confused

You should write it with if's, not with else-if's. You have a previous button when the page you are on is larger than the first page (0). You have a next link when the page you are on is smaller than the last page.

I wrote a pagination example on this forum as a reply.

Be a part of the DaniWeb community

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