LiveEn
LiveEn

Reputation: 3253

PHP pagination problem, gives blank page

Below is a code for my pagination. The problem that I am having is that the page count is displayed correctly but when I click on page 2 onwards I just get a blank page.

        <?php   
if (isset($_POST['edit'])) {
            if (empty($_GET['page'])) {
                $page=0;
            }
            else {$page = (int)$_GET['page'];}


         if ($page == 0){ $page = 1;}

          if (ob_get_level() == 0) ob_start();


       $per_page = 10;
        $p = ($page - 1) * $per_page;


        $sql="select * from tba where word='$word' order by id DESC limit ".$p.",".$per_page;
        $result=mysql_query($sql) or die(mysql_error());



        while ($row=mysql_fetch_array($result)) {
            $id=$row['id'];
            $word=$row['word'];
            $pr=$row['pr'];


            if ($pr==0) {


            }
    else {
      ?>
        <td><span class="style5"><?php echo $id; ?> </span></td>
        <td><span class="style5"><?php echo $word?></span></td>
    <?php

            }
                    $pages = floor($total / $per_page) + ($total%$per_page>0?1:0);
    ?>
    <center>
    <?php
        }
    for ($i=1;$i<=$pages;$i++) {
      print "<a href='?page=".$i."'>".$i."</a> ";

    }

     echo "<br>You are in page ".$_GET['page']; 
} 

can some one please tell me what the problem is?

Upvotes: 0

Views: 870

Answers (4)

Zsolti
Zsolti

Reputation: 1607

Well, the whole output is in an if statement and you didn't posted the POST parameters ($_POST['edit'])

Upvotes: 0

Dubas
Dubas

Reputation: 2876

The variable $total is never set making the total pages count incorrect

You need to fetch first the total number of rows with a query similar to:

$sql="SELECT COUNT(id) AS rows FROM tba where word='$word'";

Instead of doing the "floor" operation and adding "1" when has "other page" you can use "ceil"

Instead of:

$pages = floor($total / $per_page) + ($total%$per_page>0?1:0);

You can use:

$pages = ceil($total / $per_page);

Upvotes: 0

Your Common Sense
Your Common Sense

Reputation: 157870

It's time to learn debugging.

As a matter of fact, we can only guess what is going wrong.
While it is only programmer oneself who can tell it for sure. It's their job and duty.
The art of finding what is going wrong is called debugging.

To do it, you have to check every operation result.
For example, does your query return any results?
If not - why? Where does that $word variable come from? But on the second page?

You have to pass all required data to other pages as well as $page variable.

Upvotes: 1

Bob Baddeley
Bob Baddeley

Reputation: 2262

It looks like the $pages = floor... line is inside the while loop, which is the first problem. Also, $total is never being set.

Upvotes: 0

Related Questions