Jump to content

About pagination


jtified

Recommended Posts

Hi guys! I've created a paginator and it seems like I'm getting infinite page numbers..In my sample code, I have the database "experiment" , table "specimen01" , 3 columns and 12 rows

each labels has its own value, I just didn't bother to fill it up.

TABLE specimen01

================

| id | fname | lname |

================

| 1

| |

| |

| |

| |

| |

| |

| |

| v

| 12

===============

This is my code:

//CONNECT TO SERVER AND DB

$mysqlConnect = mysql_connect ('localhost','root','');

 

if (!$mysqlConnect) {

die('Could not connect: ' . mysql_error());

}

 

echo 'Connected successfully to server' . '

';

 

$mysqlDbConnect = mysql_select_db('experiment');

if (!$mysqlDbConnect)

{

die('Could not connect to database: ' . mysql_error());

}

echo 'Connected successfully to database' . '

';

 

//COUNT NUMBER OR ROWS

$query = 'SELECT * FROM specimen01';

$result = mysql_query($query);

$r = mysql_fetch_row($result);

$numrows = $r[0];

$rowsperpage = 3;

$totalpages = ceil($numrows / $rowsperpage);

 

if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage']))

{

$currentpage = $_GET['currentpage'];

}

else

{

$currentpage = 1;

}

 

 

 

 

if ($currentpage > $totalpages)

{

$currentpage = $totalpages;

}

 

if ($currentpage < 1)

{

$currentpage = 1;

}

 

 

$offset = ($currentpage - 1) * $rowsperpage;

 

$query = "SELECT * FROM specimen01 LIMIT $offset, $rowsperpage";

$result = mysql_query($query);

$mytable ="";

while ($list = mysql_fetch_assoc($result))

 

{

$mytable = $mytable . "

{$list['lname']}

{$list['fname']}

";

}

 

//BUTTONS

$range = 3;

for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {

 

if (($x > 0) && ($x <= $totalpages)) {

 

if ($x == $currentpage) {

 

echo " [$x] ";

 

} else {

 

echo " $x ";

}

}

}

 

 

if ($currentpage > 1)

 

{

echo " FIRST ";

$prevpage = $currentpage - 1;

echo " Back ";

}

 

 

 

if ($currentpage != $totalpages)

{

$nextpage = $currentpage + 1;

 

echo " Next ";

echo " LAST ";

}

 

mysql_close($mysqlConnect);

 

 

?>

 

 

I'm getting 334003 pages. I only need to have 3 data per page. so it needs to have 4 pages only since I do have 12 data in my table.

 

This is kinda confusing to me since I'm a beginner, I only made this sample database that's why has few data.

 

You might ask why separate this few data. This is just my experiment, I'm actually working on different database that needs to have a limit per page.

 

Hope someone can help me out on this...Cheers!

Link to comment
Share on other sites

You may want to start by using mysql_num_rows (http://us2.php.net/manual/en/function.mysql-num-rows.php) rather than

 

$r = mysql_fetch_row($result);

$numrows = $r[0];

 

That may fix your problem... I'm not sure, I don't have your database set up on my computer, so I can't really test this. Try that to start, and if you are still having issues, perhaps post the revised code/an exported version of your database (log in to PHPMyAdmin, select the correct database, and click the "export" tab and follow the instructions through.)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

Loading...
×
×
  • Create New...