Jump to content

phpmysql page links


srinivas6203

Recommended Posts

Hi

 

i am new to php

i have thousands of records in database table. I want to display 6o records per page. But the following code shows all the links. For example i have 6000 records. It is showing 100 page links. The page is fully occupied with those links. So i want to display those page links as 1 2 3 4 5 ........ n-5,n-4,n-3,n-2,n-1,n.

 

I think phpfreaks using this type of method. Can any one gove code or modify my code.

 

Here is my code:

 

<html>

<head>

<title>Implementing Paging with next and prev</title>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

</head>

 

<body>

<?php

include 'library/config.php';

include 'library/opendb.php';

 

// how many rows to show per page

$rowsPerPage = 2;

 

// by default we show first page

$pageNum = 1;

 

// if $_GET['page'] defined, use it as page number

if(isset($_GET['page']))

{

$pageNum = $_GET['page'];

}

 

// counting the offset

$offset = ($pageNum - 1) * $rowsPerPage;

 

$query  = "SELECT * FROM student LIMIT $offset, $rowsPerPage";

$result = mysql_query($query) or mysql_error('Error, query failed');

 

// print the random numbers

while($row = mysql_fetch_array($result))

{

echo $row['name'] . '<br>';

}

echo '<br>';

 

// how many rows we have in database

$query  = "SELECT COUNT(*) AS numrows FROM student";

$result  = mysql_query($query) or mysql_error('Error, query failed');

$row    = mysql_fetch_array($result, MYSQL_ASSOC);

$numrows = $row['numrows'];

 

// how many pages we have when using paging?

$maxPage = ceil($numrows/$rowsPerPage);

 

// print the link to access each page

$self = $_SERVER['PHP_SELF'];

$nav = '';

for($page = 1; $page <= $maxPage; $page++)

{

if ($page == $pageNum)

{

$nav .= " $page ";  // no need to create a link to current page

}

else

{

$nav .= " <a href=\"$self?page=$page\">$page</a> ";

}

}

 

// creating previous and next link

// plus the link to go straight to

// the first and last page

 

if ($pageNum > 1)

{

$page = $pageNum - 1;

$prev = " <a href=\"$self?page=$page\">[Prev]</a> ";

 

$first = " <a href=\"$self?page=1\">[First Page]</a> ";

}

else

{

$prev  = ' '; // we're on page one, don't print previous link

$first = ' '; // nor the first page link

}

 

if ($pageNum < $maxPage)

{

$page = $pageNum + 1;

$next = " <a href=\"$self?page=$page\">[Next]</a> ";

 

$last = " <a href=\"$self?page=$maxPage\">[Last Page]</a> ";

}

else

{

$next = ' '; // we're on the last page, don't print next link

$last = ' '; // nor the last page link

}

 

// print the navigation link

echo $first . $prev . $nav . $next . $last;

 

// and close the database connection

include 'library/closedb.php';

?>

</body>

</html>

 

Thanq very much

Link to comment
https://forums.phpfreaks.com/topic/110729-phpmysql-page-links/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.