Jump to content

Recommended Posts

Hello Phpfreaks,

 

I am trying to do a bit of pagination but I am coming up 1 row short for each page. I could be way off with the code or something simple, after all I am a php toddler.  :D

What I am trying to do is with a form where you select country and state, it displays your mysql data according to that selection. For testing puposes I created 13 rows in the table but when I view results it shows 9 on the 1st page and 2 on the 2nd page, leaving out 1 on each page. The column is called company and all 13 rows have data so its not like its showing but just blank rows.

Here is the code:

<?php
include'dbc.php';

    if($_GET['page']) // Is page defined?

    {

        $page = $_GET['page']; // Set to the page defined

    }else{

        $page = 1; // Set to default page 1

    }

$max = 10; // Set maximum to 10



$cur = (($page * $max) - $max); // Work out what results to show



$getdata = mysql_query("SELECT * FROM sbt WHERE country LIKE '%$country%' AND state LIKE '%$state%' ORDER BY `id` DESC LIMIT $cur, $max") or die(mysql_error()); // select the results

$data = mysql_fetch_array($getdata); // get the data

while($user = mysql_fetch_array($getdata))
{
echo $user['company'] . '<br />';
}



$counttotal = mysql_query("SELECT * FROM sbt ") or die(mysql_error()); // select all records        

$counttotal = mysql_num_rows($counttotal); // count records



$total_pages = ceil($counttotal / $max); // dive the total, by the maximum results to show 

if($page > 1){ 

                $prev = ($page - 1); 

                echo '<a href="?page=' . $prev . '">« Previous</a>'; 

                }



for($i = 1; $i <= $total_pages; $i++) // for each page number

                {

                    if($page == $i) // if this page were about to echo = the current page

                        {

                            echo'<b>' . $i .'</b> '; // echo the page number bold

                                } else {

                            echo '<a href="?page=' . $i . '">' . $i . '</a> '; // echo a link to the page

                        }

                }



if($page < $total_pages){ 

                    $next = ($page + 1); 

                echo '<a href="?page=' . $next . '">Next »</a>'; // echo the next page link

                    } 

?> 

 

Could someone please advise me on my wrongful 1 row short ways  :confused:

 

Thank you for taking the time to read this. Much appreciated!

Link to comment
https://forums.phpfreaks.com/topic/177397-solved-pagination-showing-1-row-short/
Share on other sites

change to

$getdata = mysql_query("SELECT * FROM sbt WHERE country LIKE '%$country%' AND state LIKE '%$state%' ORDER BY `id` DESC LIMIT $cur, $max") or die(mysql_error()); // select the results

//$data = mysql_fetch_array($getdata); // get the data

while($user = mysql_fetch_array($getdata))
{
echo $user['company'] . '<br />';
}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.