Shoutbox

PHP Pagination help - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Skype & Technology (/forumdisplay.php?fid=9)
+---- Forum: Tech Talk (/forumdisplay.php?fid=17)
+----- Thread: PHP Pagination help (/showthread.php?tid=58516)

PHP Pagination help by DJeX on 04-20-2006 at 01:08 AM

I want to add pagination to my php script/site im making. It's going to be a search engine so it's necessary that I have pagination in it.

I've searched Google and found many pagination tutorials but they either do not work or are a pain to understand.

Would any one be kind enough to help me integrate pagination into my php site?

Heres the code:

code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<div align="center">

<?php
echo "<form name=\"form1\" method=\"post\" action=\"index.php\">
<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">
<tr><td><div align=\"center\"><input name=\"search\" type=\"text\" id=\"search\" size=\"50\"> <input type=\"submit\" name=\"Submit\" value=\"Search\"></div></td></tr>
</table>
</form>";

if (!$_POST['search'])
{
  echo "<p>Please enter a search...</p>";
}
else
{
$var = $_POST['search'] ;
$search = trim($var);


mysql_connect($db_host, $db_user, $db_pass);
mysql_select_db($db_database);


$query = mysql_query("SELECT * FROM search WHERE description LIKE '%" . $search . "%'");
$numrows=mysql_num_rows($query);


if ($numrows)
  {
  echo '<BR>
  <table width="100%" border="0" cellpadding="1" cellspacing="0" bgcolor="#000000">
  <tr><td><table width="100%" border="0" cellpadding="1" cellspacing="0" bgcolor="#9FC3FF">
  <tr><td width="49%"><font color="#FFFFFF" size="2" face="Verdana, Arial, Helvetica, sans-serif">Searched for: <strong>' . $search . '</strong></font></td>
  <td width="51%"><div align="right"><font color="#FFFFFF" size="2" face="Verdana, Arial, Helvetica, sans-serif">' . $numrows . ' Result(s)</font></div></td>
  </tr></table></td></tr></table>
  <br>';       


  while ($data = mysql_fetch_array($query)) {
  $title = $data['title'];
  $desc = $data['description'];
  $link = $data['link'];
               
  echo '<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr><td><font size="2" face="Verdana, Arial, Helvetica, sans-serif"><strong>' . $title . '</strong></font></td></tr>
  <tr><td><font size="1" face="Verdana, Arial, Helvetica, sans-serif">' . $desc . '</td></font></tr>
  <tr><td><font size="1" face="Verdana, Arial, Helvetica, sans-serif"><font color="#009933"><a href="' . $link . '">' . $link . '</a></font></td></tr></table>
  <BR>';

// exit;
}   
  } else {
  echo "<center><strong>No result found, please try again.</strong></center>";           
  }
}


?>

<p><font size="2" face="Verdana, Arial, Helvetica, sans-serif">Powered By (dont know yet).<br>
</font><font size="2" face="Verdana, Arial, Helvetica, sans-serif">&copy; 2006 Music Related Network</font></p></div>
</body>
</html>

RE: PHP Pagination help by Lou on 04-20-2006 at 01:15 AM

All though you said you couldn't get anything..how would this be? http://www.phpfreaks.com/tutorials/43/0.php


RE: PHP Pagination help by DJeX on 04-20-2006 at 01:58 AM

Yea but I'm not sure on how to integrate that into my code.

Bah it's full of bugs. (N)


RE: PHP Pagination help by segosa on 04-20-2006 at 05:37 AM

I'm not going to do it for you, but I'll explain a bit what you should be doing.

Set a variable $perpage somewhere that'll define how many results are displayed per page. Also let's say you're getting the page number via $_GET['page'].

You'll want to use MySQL's "LIMIT" operator. The syntax is start,count where start is the row's offset and count is how many rows after the offset to return.

Use this algorithm to calculate the start position:

$start = ($page - 1) * $perpage;

and use $perpage as the count.

When page is 1, start = 0 which is the beginning
When page is 2, start = perpage * 1 which is perpage (the next set)
When page is 3, start = perpage * 2 which is the last start position + perpage
etc

For example:

code:
$page = intval($_GET['page']);
$start = ($page - 1) * $perpage;
$query = mysql_query("SELECT * FROM search WHERE description LIKE '%" . $search . "%' LIMIT $start,$perpage");
$numrows=mysql_num_rows($query);


$numrows will tell you how many results you got back. If you get none, then obviously there are too few results to show that page. As for displaying links to other pages, that's up to you.