Pagination
It shows following messages
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\wamp\www\Paging\111\test\index.php on line 55
Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\wamp\www\Paging\111\test\index.php on line 154
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\wamp\www\Paging\111\test\index.php on line 167
config.php
<?php
$query = mysql_connect("localhost","root","");
if (!$query)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("phptest", $query);
?>
<!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" />
<link href="style.css" rel="stylesheet" type="text/css" />
<title>sample code</title>
<?php
session_start();
include "config.php"; // include your code to connect to DB.
error_reporting(E_ALL ^ E_NOTICE);
$tbl_name="people";
?>
</head>
<body>
<form name="Item Search" method="post" action="index.php">
Item Search:<br>
<label>
<input name="swords" type="text" size="30" maxlength="30">
</label>
<br>
<label>
<input name="search" type="submit" id="search" value="Search">
</label>
<a href="logout.php">Logout</a>
</form>
<?php
if(isset($_POST['submit'])||isset($_POST['swords'])||isset($_GET['swords'])){
if (isset($_POST['swords']))
{
$search=$_POST['swords'];
$_SESSION['sword'] = $_POST['swords'];
}else
{
$search=$_GET['sword'];
}
$newurl = "index.php?swords=";
$searchwords = addslashes(htmlspecialchars($_POST['sword']));
{
$words = explode(' ',$searchwords);
$totalwords = count($words);
$i = 0;
$searchstring = "";
while ($i != $totalwords){
if ($i != 0 and $i != $wordcount){
$searchstring .= " and ";;
}
$searchstring .= "b_name LIKE '%$words[$i]%'";
$searchstring1 .= "b_author LIKE '%$words[$i]%'";
$i = $i + 1;
// How many adjacent pages should be shown on each side?
$adjacents = 3;
$query = "SELECT COUNT(*) as num FROM $tbl_name WHERE $searchstring or $searchstring1";
$total_pages = mysql_fetch_array(mysql_query($query));
$total_pages = $total_pages[num];
$targetpage = "index.php"; //your file name (the name of this file)
$limit = 2; //how many items to show per page
$page = $_GET['swords'];
if($page)
$start = ($page - 1) * $limit; //first item to display on this page
else
$start = 0; //if no page var is given, set start to 0
/* Get data. */
$sql = "SELECT DISTINCT * FROM $tbl_name WHERE $searchstring or $searchstring1 LIMIT $start, $limit";
$result = mysql_query($sql);
/* Setup page vars for display. */
if ($page == 0) $page = 1; //if no page var is given, default to 1.
$prev = $page - 1; //previous page is page - 1
$next = $page + 1; //next page is page + 1
$lastpage = ceil($total_pages/$limit); //lastpage is = total pages / items per page, rounded up.
$lpm1 = $lastpage - 1; //last page minus 1
$pagination = "";
if($lastpage > 1)
{
$pagination .= "<div class=\"pagination\">";
//previous button
if ($page > 1)
$pagination.= "<a href=\"$targetpage?swords=$prev?=\">« previous</a>";
else
$pagination.= "<span class=\"disabled\">« previous</span>";
//pages
if ($lastpage < 7 + ($adjacents * 2)) //not enough pages to bother breaking it up
{
for ($counter = 1; $counter <= $lastpage; $counter++)
{
if ($counter == $page)
$pagination.= "<span class=\"current\">$counter</span>";
else
$pagination.= "<a href=\"$targetpage?swords=$counter\">$counter</a>";
}
}
elseif($lastpage > 5 + ($adjacents * 2)) //enough pages to hide some
{
//close to beginning; only hide later pages
if($page < 1 + ($adjacents * 2))
{
for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
{
if ($counter == $page)
$pagination.= "<span class=\"current\">$counter</span>";
else
$pagination.= "<a href=\"$targetpage?swords=$counter\">$counter</a>";
}
$pagination.= "...";
$pagination.= "<a href=\"$targetpage?swords=$lpm1\">$lpm1</a>";
$pagination.= "<a href=\"$targetpage?swords=$lastpage\">$lastpage</a>";
}
//in middle; hide some front and some back
elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
{
$pagination.= "<a href=\"$targetpage?swords=1\">1</a>";
$pagination.= "<a href=\"$targetpage?swords=2\">2</a>";
$pagination.= "...";
for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
{
if ($counter == $page)
$pagination.= "<span class=\"current\">$counter</span>";
else
$pagination.= "<a href=\"$targetpage?swords=$counter\">$counter</a>";
}
$pagination.= "...";
$pagination.= "<a href=\"$targetpage?swords=$lpm1\">$lpm1</a>";
$pagination.= "<a href=\"$targetpage?swords=$lastpage\">$lastpage</a>";
}
//close to end; only hide early pages
else
{
$pagination.= "<a href=\"$targetpage?swords=1\">1</a>";
$pagination.= "<a href=\"$targetpage?swords=2\">2</a>";
$pagination.= "...";
for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
{
if ($counter == $page)
$pagination.= "<span class=\"current\">$counter</span>";
else
$pagination.= "<a href=\"$targetpage?swords=$counter\">$counter</a>";
}
}
}
//next button
if ($page < $counter - 1)
$pagination.= "<a href=\"$targetpage?swords=$next\">next »</a>";
else
$pagination.= "<span class=\"disabled\">next »</span>";
$pagination.= "</div>\n";
}
if (mysql_num_rows($result) == 0){
echo "No results were found.<br><input type=button value='Back' onClick='history.go(-1)'>";
}
echo ("
<H4 ALIGN=left>Books found matching search criteria :</H4><BR>
<TABLE width='70%' border='2' cellpadding='3' ALIGN=CENTER >
<TR bgcolor='B0C4DE'>
<TH>Sr.No</TH>
<TH>Title Of Book</TH>
<TH>Author Name</TH>
<TH>Number of Copies</TH>
<TH>Borrow</TH>
</TR>");
while($row = mysql_fetch_array($result))
{
echo ("<tr id='$a' bgcolor='B0C4DE'>");
echo ('<td>'.$row['id'].'</td>');
echo ('<td>'.$row['firstname'].'</td>');
echo ('<td>'.$row['list'].'</td>');
echo ('<td>'.$row['list'].'</td>');
if($row['b_avail']>0)
{
if (!$_SESSION["valid_user"])
{
// User not logged in, redirect to login page
echo("<td>Available</td>");
}else
echo("<td><INPUT TYPE=button id=show name=show[] value=add onClick=buttonPress('$sr_num')></td>");
}else
echo("<td>No stock of book</td>");
echo("</tr>");
}
echo $pagination;
}
}
}
?>
</body>
</html>