Click here to Skip to main content
15,885,869 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
PHP
<?php
$search = $_POST['searchvalue'];
    $search= $_POST['searchvalue'];//first time the 'searchvalue' is from form
    $newsearch = $_GET['searchvalue'];//for next time 'searchvalue' is from url
 if((!$newsearch) && strlen($search)<=1)
    {
    echo "Search term too short";
    }
    else{    	    	
        echo "You searched for $search <hr size='1'></br>";
    $construct = "SELECT * FROM books WHERE category = '$name' AND ((title LIKE '%$search% ' or author LIKE '%$search% ')  or (title LIKE '%$newsearch% ' or author LIKE '%$newsearch% ' ))";
    $run = mysql_query($construct);
    $foundnum = mysql_num_rows($run);
    if ($foundnum==0){
    	//echo $_GET['start'];
    echo "Sorry, there are no matching result for $search.</br></br>1.";
    }
    else
    {
    echo "$foundnum results found !<p>";
    }
//I have next button here.
?>

if search results have 10 records i want to print 5 records per page, for first search i am getting 5 records in first page successfully it is only happening with this sql query
<br />
"$construct=SELECT * FROM books WHERE category = '$name' AND (title LIKE '%$search% ' or author LIKE '%$search% ')";

if i click next button the next five records is not printing in next page, its is due to POST, because FORM is in one php page and SEARCH.PHP is another page, I need to get the value from url for next search. so I used GET method, but problem is $search is using POST, hence I changed the sql query, like below as showed in code
$construct = "SELECT * FROM books WHERE category = '$name' AND ((title LIKE '%$search% ' or author LIKE '%$search% ')  or (title LIKE '%$newsearch% ' or author LIKE '%$newsearch% ' ))";<br />

but this time its not working even for first search, please anyone help me how to modify my query to GET the correct results. And sorry for my poor english. please try to understand my problem.
Posted

1 solution

Based on your question I assume that you want to have paging in your search result. Unfortunately paging in search result is impossible with POST method, you should use GET method.

Here sample code. It's should be working

PHP
<?php
// Chech whether specific page is requested or not
$page = isset($_GET["page"]) ? $_GET["page"] : 1;
// Get user search value
$search = $_GET["searchvalue"];
// We will have 5 record(s) each page
$maxRecordPerPage = 5;

// #01: Count total result
$construct = "SELECT COUNT(id) AS total FROM books WHERE category = '$category' AND (title LIKE '%$search%' OR author LIKE '%$search%')";
$run = mysql_query($construct);
if (!$run) {
	die("Count Result Failed");
}
$temp = mysql_fetch_assoc($run);
$total = $temp["total"];
$maxPage = ceil($total / $maxRecordPerPage);

// #02: Retrieve appropriate result based on user request
$startFrom = ($page - 1) * $maxRecordPerPage;
$construct = "SELECT * FROM books WHERE category = '$category' AND (title LIKE '%$search%' OR author LIKE '%$search%') LIMIT $startFrom, $maxRecordPerPage";
$run = mysql_query($construct);
if (!$run) {
	die("Count Result Failed");
}
?>

// HTML, BODY Removed from clarity

// Result Table
<table>
	<tr>
		<td>Title</td>
		<td>Author</td>
	</tr>
	<?php
	while ($row = mysql_fetch_assoc($run)) {
		print("<tr>");
		printf("<td>%s</td>", $row["title"]);
		printf("<td>%s</td>", $row["author"]);
		print("</tr>");
	}
	?>
</table>

// Paging for search
<ul>
	<?php
	for ($i = 1; $i <= $maxPage; $i++) {
		printf("<li><a href='search.php?page=%s&searchvalue=%s'>Page #%s</a></li>", $i, $search, $i);
	}
	?>
</ul>
 
Share this answer
 
v2
Comments
Member 11406049 27-Feb-15 2:35am    
thank you for your reply. I have two files "header.php" and "search.php" form is in header.php file and search logic and pagination is done in search.php file, hence i used POST to get the value from header.php to search.php. for next page result i have to get the values from url in the same page called search.php. when ever I click the next button I have to use GET method, please help me how to use like this, I can't write search and pagination code in header.php...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900