What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Skype & Technology » Tech Talk » PHP Pagination help

PHP Pagination help
Author: Message:
DJeX
Veteran Member
*****

Avatar


Posts: 1138
Reputation: 11
– / Male / –
Joined: Jul 2003
O.P. Huh?  PHP Pagination help
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>
[Image: top.gif]
04-20-2006 01:08 AM
Profile PM Web Find Quote Report
Lou
Veteran Member
*****

Avatar

Posts: 2475
Reputation: 43
– / Male / Flag
Joined: Aug 2004
RE: PHP Pagination help
All though you said you couldn't get anything..how would this be? http://www.phpfreaks.com/tutorials/43/0.php
[Image: msghelp.net.png]
The future holds bright things in it\\\'s path, but only time will tell what they are and where they come from.
Messenger Stuff Forums
04-20-2006 01:15 AM
Profile PM Web Find Quote Report
DJeX
Veteran Member
*****

Avatar


Posts: 1138
Reputation: 11
– / Male / –
Joined: Jul 2003
O.P. RE: PHP Pagination help
Yea but I'm not sure on how to integrate that into my code.

Bah it's full of bugs. (N)


This post was edited on 04-20-2006 at 02:02 AM by DJeX.
[Image: top.gif]
04-20-2006 01:58 AM
Profile PM Web Find Quote Report
segosa
Community's Choice
*****


Posts: 1407
Reputation: 92
Joined: Feb 2003
RE: PHP Pagination help
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.
The previous sentence is false. The following sentence is true.
04-20-2006 05:37 AM
Profile PM Find Quote Report
« Next Oldest Return to Top Next Newest »


Threaded Mode | Linear Mode
View a Printable Version
Send this Thread to a Friend
Subscribe | Add to Favorites
Rate This Thread:

Forum Jump:

Forum Rules:
You cannot post new threads
You cannot post replies
You cannot post attachments
You can edit your posts
HTML is Off
myCode is On
Smilies are On
[img] Code is On