Solved

PHP pagnation

Posted on 2010-08-21
3
346 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

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

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…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

627 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