views:

41

answers:

4

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?

A: 

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

Bob Baddeley
+1  A: 

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.

Col. Shrapnel
Her *or* his duty... if we're going to be thinking about each possible detail ;)
Peter Ajtai
I did the debugging the problem was because the the code was withing the submit button. :D Fixed it now.
LiveEn
Politically corrected.
Col. Shrapnel
A: 

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);
Dubas
A: 

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

Zsolti