Below is my code can you debug it?? when i try to search on my search box it only show the data equal to the input box but when i clicked the pagination all the data in the database show i only want to show the data that been search or equal to item that been search can any one help?


thanks for who will help ^_^

<!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
	include "dbconfig.php";	// include your code to connect to DB.
	error_reporting(E_ALL ^ E_NOTICE);
	$tbl_name="book";	
	?>
</head>

<body>
<form name="Item Search" method="post" action="search1.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'];
}else
{
$search=$_GET['swords'];
}
$newurl = "search.php?swords=";
$searchwords = addslashes(htmlspecialchars($_POST['swords'])); 
{ 
$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 = "search1.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['b_sr_num'].'</td>');
		echo ('<td>'.$row['b_name'].'</td>');
		echo ('<td>'.$row['b_author'].'</td>');
		echo ('<td>'.$row['b_avail'].'</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>

Recommended Answers

All 4 Replies

Your pagination uses swords to pass a page number, but you try to read it as the search word.

Your pagination uses swords to pass a page number, but you try to read it as the search word.

can you tell me what should i modify?? in the code

Hello Yamot47,

I agree with "pritaeas" you use search boxes "swords" value in your query so it work only first time and everytime when you click on pagniation next time your search boxes value is null so you query give whole database value.

So solution for that you need to make your searchbox value into session which you get every time when you click on pagination.

Here is some code you need to add:

For session please update first line:

session_start();
include "dbconfig.php";

and when you click on pagination you need to update:

if(isset($_POST['swords']))
    { 
        $search=$_POST['swords'];
        $_SESSION['sword'] = $_POST['swords'];

    }
    else
    {
        $search=$_SESSION['sword'];
    }

    $newurl = "search.php?swords=";

    $searchwords = addslashes(htmlspecialchars($_SESSION['sword'])); 
    {

Hope this will help you!

its working thank you HermanTPhp
it works

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.