Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

PHP pagnation

Posted on 2010-08-21
3
Medium Priority
?
348 Views
Last Modified: 2013-12-13
Hello,

I've got the following.
<div class="entry"> 
					<table class="browsetable"> 
						
						<?php 
						$getartists_sql = mysql_query("SELECT artistID, artistname FROM artists WHERE artistname LIKE '".mysql_real_escape_string($browse)."%'");
						$color = 1;
						while($row_browse = mysql_fetch_array($getartists_sql, MYSQL_ASSOC))
						{
							if($color == 1) {
							?>
							<tr> 
								<td class="even"><a href="#" title=""><?php echo stripslashes($row_browse['artistname']); ?></a></td> 
								<td class="even lyriccount">123 lyrics</td> 
							</tr> 								
							<?php 
							$color = 2;
							}
							else{
							?>
							<tr> 
								<td class="uneven"><a href="#" title=""><?php echo stripslashes($row_browse['artistname']); ?></a></td> 
								<td class="uneven lyriccount">123 lyrics</td> 
							</tr> 
							<?php
							$color = 1;
							}
						}
						?>
					</table> 	
				</div> 

Open in new window


and it returns a lot of artistnames, but now I want pagnation.
So when there are more than 20 names, make a new page with link.
more than 40 names, 2 pages with link etc.
0
Comment
Question by:Serellyn
3 Comments
 
LVL 5

Assisted Solution

by:ploftin
ploftin earned 600 total points
ID: 33493505
Something like this:
<?php
$currentPage = $_SERVER["PHP_SELF"];

$maxRows_Recordset1 = 20;
$pageNum_Recordset1 = 0;
if (isset($_GET['pageNum_Recordset1'])) {
  $pageNum_Recordset1 = $_GET['pageNum_Recordset1'];
}
$startRow_Recordset1 = $pageNum_Recordset1 * $maxRows_Recordset1;

mysql_select_db($database_Music, $Music);
$query_Recordset1 = "SELECT * FROM Artists";
$query_limit_Recordset1 = sprintf("%s LIMIT %d, %d", $query_Recordset1, $startRow_Recordset1, $maxRows_Recordset1);
$Recordset1 = mysql_query($query_limit_Recordset1, $Music) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);

if (isset($_GET['totalRows_Recordset1'])) {
  $totalRows_Recordset1 = $_GET['totalRows_Recordset1'];
} else {
  $all_Recordset1 = mysql_query($query_Recordset1);
  $totalRows_Recordset1 = mysql_num_rows($all_Recordset1);
}
$totalPages_Recordset1 = ceil($totalRows_Recordset1/$maxRows_Recordset1)-1;

$queryString_Recordset1 = "";
if (!empty($_SERVER['QUERY_STRING'])) {
  $params = explode("&", $_SERVER['QUERY_STRING']);
  $newParams = array();
  foreach ($params as $param) {
    if (stristr($param, "pageNum_Recordset1") == false && 
        stristr($param, "totalRows_Recordset1") == false) {
      array_push($newParams, $param);
    }
  }
  if (count($newParams) != 0) {
    $queryString_Recordset1 = "&" . htmlentities(implode("&", $newParams));
  }
}
$queryString_Recordset1 = sprintf("&totalRows_Recordset1=%d%s", $totalRows_Recordset1, $queryString_Recordset1);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>

<body>
<div class="entry"> 
  <table class="browsetable"> 
						
						<?php 
						$color = 1;
						do {
							if($color == 1) {
							?>
							<tr> 
								<td class="even"><a href="#" title=""><?php echo stripslashes($row_Recordset1['artistname']); ?></a></td> 
								<td class="even lyriccount">123 lyrics</td> 
							</tr> 								
							<?php 
							$color = 2;
							}
							else{
							?>
							<tr> 
								<td class="uneven"><a href="#" title=""><?php echo stripslashes($row_Recordset1['artistname']); ?></a></td> 
								<td class="uneven lyriccount">123 lyrics</td> 
							</tr> 
							<?php
							$color = 1;
							}
						} while ($row_Recordset1 = mysql_fetch_assoc($Recordset1));
						?>
					</table>
                    <p>&nbsp;<a href="<?php printf("%s?pageNum_Recordset1=%d%s", $currentPage, 0, $queryString_Recordset1); ?>">First</a> <a href="<?php printf("%s?pageNum_Recordset1=%d%s", $currentPage, max(0, $pageNum_Recordset1 - 1), $queryString_Recordset1); ?>">Previous</a> <a href="<?php printf("%s?pageNum_Recordset1=%d%s", $currentPage, min($totalPages_Recordset1, $pageNum_Recordset1 + 1), $queryString_Recordset1); ?>">Next</a> <a href="<?php printf("%s?pageNum_Recordset1=%d%s", $currentPage, $totalPages_Recordset1, $queryString_Recordset1); ?>">Last</a></p>
</div>
</body>
</html>
<?php
mysql_free_result($Recordset1);
?>

Open in new window

0
 
LVL 11

Accepted Solution

by:
Rajesh Dalmia earned 800 total points
ID: 33494130
you can use this freee php pagination class http://phpsense.com/php/php-pagination-script.html
it's easy to implement
0
 
LVL 14

Assisted Solution

by:john-formby
john-formby earned 600 total points
ID: 33494295
Hi,

Please check the following script: http://phpfreak.co.uk/comments.php?bID=17

It is a fully commented example which is easy to implement.

Hope this helps,

John
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses four methods for overlaying images in a container on a web page
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

782 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question