Solved

PHP pagnation

Posted on 2010-08-21
3
329 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 150 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 200 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 150 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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

757 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now