0

I am fetching username and Id from the database. I have more than 500 usernames. I have to display pagination number only 1 to 5 and last pagination number.

Example:- pagination number is:- 1 2 3 4 5...20(last number).

Now I am getting all numbers in horizontal. Would you help me in this?

Can anyone help me with NEXT and LAST in pagination?

include('../db/connection.php');
    $reclimit = 3;
     if(isset($_GET['page'])){
        $page = $_GET['page'];
    } else {
        $page = 1;
    }

    $start = (($page-1) * $reclimit);
    $sql = "SELECT * FROM request";

    $records =$conn->query($sql);;
    $total = $records->num_rows;
    $tpages = ceil($total / $reclimit);

    $search_sql="SELECT * FROM request LIMIT ".$start."," .$reclimit; 
    $search_result = $conn->query($search_sql); 

HTML

 <body>
    <?php 

        if (isset($search_result->num_rows) > 0) {
        ?>
        <h2 class="result-title">Results matching your need</h2>
        <?php
            while($search_ok = $search_result->fetch_assoc()) {
                    $user_id=$search_ok['Id'];
                    $user_name=$search_ok['Name'];

        echo "
            <div class='search-section'>
                    <div class='search-profile'>

                    <div class='s_user_id'>{$user_id}</div>
                    <div class='s_user_name'>{$user_name}</div>

                    </div>
                    </div>
        ";

         }}
      for($i=1;$i<=$tpages;$i++) {
                        echo "<a href=page.php?page=".$i.">".$i."</a>";
                    }
                ?>



    </body>
Naren Verma
  • 2,205
  • 5
  • 38
  • 95

2 Answers2

0

The pagination which you are using is simple and working one. But the pagination which you are searching is smart way and you should achieve this by using some if conditions. Similar answer are there in SO. Go to the following, this may help you

Community
  • 1
  • 1
rahul patel
  • 262
  • 1
  • 2
  • 15
0

It's a simple idea, but I didn't test it. Edit foreach displaying numbers:

$pgStart = 1;
if (isset($_GET['page'])) { // get first showing number = current page - 2
    $pg = $_GET['page'] - 2;
    $pgStart = $pg + 5 > $tpages ? $tpages - 4 : $pg; //EDIT fix when reach pages end
    $pgStart = $pg < 1 ? 1 : $pg; // This must be after ending correction (previous line)
}

if ($pgStart > 1) { // show 1
    echo '<a href=page.php?page="1">1</a> ... ';
}

for($i = $pgStart; $i <= $tpages && $i < $pgStart + 5; $i++) { // show 5 pages
    echo ' <a href=page.php?page="'.$i.'">'.$i.'</a> ';
}

if ($i < $tpages) { // show last
    echo ' ... <a href=page.php?page="'.$tpages.'">'.$tpages.'</a>';
}

EDIT

Output of this script with $_GET['page'] = 7 and $tpages = 20 from php sandbox is (without linebreaks):

<a href=page.php?page="1">1</a> ... 
<a href=page.php?page="5">5</a> 
<a href=page.php?page="6">6</a> 
<a href=page.php?page="7">7</a> 
<a href=page.php?page="8">8</a> 
<a href=page.php?page="9">9</a> 
... <a href=page.php?page="20">20</a>
Jirka Picek
  • 589
  • 5
  • 19
  • Thanks for replying Mr.Jirka, It is working but I am not getting (...) in between – Naren Verma Mar 16 '17 at 11:59
  • And also first two no not working..like 1 and 2. Not getting any records. if I clicked on 3 then I am getting records – Naren Verma Mar 16 '17 at 12:04
  • I tried it in sandbox and it should work. I just forget `$` in line with `for` loop. The three dots are hardcoded to string with first and last page and there is no reason why first two records doesn't work. I'll post output from sandbox. – Jirka Picek Mar 16 '17 at 12:13
  • Mr.Jirka, I reached pagination number till 16, 17,18,19,20. Now I am getting records of 16 on my screen but when I am clicking on 17 then 16 goes off from number same as I clicked on 18 then 17 goes off at the end on 20 showing – Naren Verma Mar 16 '17 at 12:27
  • Yes, I forgot about that, but it's simple fix – Jirka Picek Mar 16 '17 at 12:34
  • I hoped you have set it when you are using it. The `$tpages` should be set to value which equals `ceil($tableItemsCount/$itemsPerPage)` where $tableItemsCount is number of records and $itemsPerPage is number of displayed items – Jirka Picek Mar 16 '17 at 12:48
  • Ok, then when it's set 5 lines later then it's set in the added line. Try to use debugger. I guess that I answered your question and I won't fix your every bug in code. I don't know how it behaves with your code since I'm using some of your variables. If you find bug in my code, then let me know, I'll fix it in answer. – Jirka Picek Mar 16 '17 at 13:02
  • I'm sorry that I was rude, but I would like to know, have you found the solution? – Jirka Picek Apr 07 '17 at 07:09