Solved

PHP pagnation

Posted on 2010-08-21
3
332 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
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 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 …

911 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

16 Experts available now in Live!

Get 1:1 Help Now