Jump to content

how to create search functionality in php and mysql and the search result will be on another page apart from the search page


mahenda

Recommended Posts

Start small.  Focus on one task at a time.  But produce some code for us to look at because we are not here to "write code for you".  This forum is geared towards helping others to become coders as well as to become better coders.  It is not a free labor pool, although we do labor and we do it for free.

Edited by ginerjm
Link to comment
Share on other sites

//the problem i'm experiencing here is the 1,2 ,3.... pages are black how to solve this help me if there is any mistake and how to get the url of the logical page and how http_build_query() work 
<?php
if(isset($_GET['submit'])){
	$names = $_GET['search'];
	$escapeString = mysqli_real_escape_string($conn, $names);	
		if(!empty($escapeString)){
			$resultPerPage = 8;  
				if (isset($_GET["page"])) { $page  = $_GET["page"]; 
				} else { 
				$page=1;
				}
				$startPage = ($page-1) * $resultPerPage;
				$sql = "SELECT * FROM movies WHERE name LIKE '%".$escapeString."%' OR  producer LIKE '%".$escapeString."%'  LIMIT ".$startPage.",".$resultPerPage;
				$result = mysqli_query($conn, $sql);
				while($row = mysqli_fetch_assoc($result)){	
						
						echo '  <div>

						<span>'.$row['name'].'</span>

						<span>'.$row['producer'].'</span>

						<p>'.$row['shortDescription'].'</p>  

						</div>  ';
				}
				echo '<ul class="pagination">';
				if ($page == 1) { 
				echo '<li class="disabled"><a href="#">First</a></li>
				<li class="disabled"><a href="#">&laquo;</a></li>';
				} else { 
				$linkPrev = ($page > 1) ? $page - 1 : 1;
				echo '<li><a href="search.php?page=1">First</a></li>
				<li><a href="search.php?page='.$linkPrev.'">&laquo;</a></li>';
				}
				$cSql = "SELECT COUNT(*) FROM movies WHERE name LIKE '%".$escapeString."%' OR  producer LIKE '%".$escapeString."%'";
			
				$cResult = mysqli_query($conn, $cSql); 
				$row = mysqli_fetch_row($cResult);
				$totalRecords = $row[0];
			
				$noOfpage = ceil($totalRecords/ $resultPerPage);
				$linkNumber = 7;
				$startNumber = ($page > $linkNumber) ? $page - $linkNumber : 1;
				$endNumber = ($page < ($noOfpage - $linkNumber)) ? $page + $linkNumber : $noOfpage; 
				for ($i = $startNumber; $i <= $endNumber; $i++) {
				$linkActive = ($page == $i) ? 'class="active"' : '';
					echo '<li '.$linkActive.'><a href="search.php?page='.$i.'">'.$i.'</a></li>';
				}
				if ($page == $noOfpage) {
					echo '<li class="disabled"><a href="#">&raquo;</a></li>
					<li class="disabled"><a href="#">Last</a></li>';
				} else 
				{
				$linkNext = ($page < $noOfpage) ? $page + 1 : $noOfpage;
					echo '<li><a href="search.php?page='.$linkNext.'">&raquo;</a></li>
					<li><a href="search.php?page='.$noOfpage.'">Last</a></li>';
				}
				echo '</ul>';	
				}
				else
				{
					echo '
					<div class="head">
					<h6 class="ml-0"> RESULTS REPORT:</h6>
					</div>';	
					echo '<span> The keyword </span><span class="text-warning"> '.$escapeString.' </span><span> is not available in our database </span>';
				}		
				}	
				else
				{
				echo 'You have to Enter movie name or producer';		
				}

		?>

help @Barand

On 5/19/2019 at 3:22 PM, ginerjm said:

Start small.  Focus on one task at a time.  But produce some code for us to look at because we are not here to "write code for you".  This forum is geared towards helping others to become coders as well as to become better coders.  It is not a free labor pool, although we do labor and we do it for free.

@ginerjm@cyberRobot@mac_gyver

Link to comment
Share on other sites

On 5/18/2019 at 8:00 AM, ginerjm said:

I suggest that you re-read what I gave you on how to write that query statement.

@mahenda - Are you still working on correcting the query, as ginerjm suggested? If so, the issue with the query is that you removed the double quotes before and after the $escapeString variables, but you didn't remove the concatenation characters.

For what it's worth, the query you had before ginerjm's advice should be fine.

$sql = "SELECT name, producer FROM movies WHERE name LIKE '%".$escapeString."%' OR  producer LIKE '%".$escapeString."%' ";

Or you could use ginerjm's suggestion.

$sql = "SELECT name, producer FROM movies WHERE name LIKE '%$escapeString%' OR  producer LIKE '%$escapeString%'";

They're both the same query. Ginerjm's suggestion just has the PHP variables embedded in a string. Your's uses string concatenation.

 

With that said, I would recommend renaming $escapeString to something more meaningful. At some point in time, you'll likely need to write a script where you are escaping more than one piece of information.

 

On 5/16/2019 at 8:10 AM, ginerjm said:

PPS - if you haven't realized it yet PHP is a case-sensitive language.  That means "$escapeString" is a different variable than "$escapestring".   That means when you needlessly add caps to your names you force yourself to remember how you did it on every one of your variables and it really is not necessary.

I personally use camel case because that's how I was taught. Granted my initial learning experience wasn't with PHP. I just prefer camel case because it's easier to read (for me) then using all lower cases. I've tried switching to other recommended formats (e.g. using underscores or hyphens between words $escape_string), but I always find myself switching back to camel case. In the end, consistency is key.

Using all lower-case letters, in and of itself, isn't going to save you from the headaches of mistyping a variable name. I've had many frustrations with plural vs. singular words (e.g. $name and $names). I've also had problems with words in different tenses (e.g. $escapeString and $escapedString).

If you're worried about mistyping a variable, try working with a code editor that auto-completes variable names as you're typing. Mistyping should also be something you're looking at when debugging a script.

Link to comment
Share on other sites

34 minutes ago, mahenda said:

pages are black how to solve this help me if there is any mistake and how to get the url of the logical page and how http_build_query() work

@mahenda - Is PHP set to display errors? You can add the following to the top of your script so that errors are displayed:

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
?>

Note that the above should only be present when you're developing the script or debugging. The errors generated by PHP shouldn't be displayed to your visitors. They're not very user friendly and they can potentially reveal information to someone trying to compromise your website.

Link to comment
Share on other sites

10 hours ago, cyberRobot said:

@mahenda - Is PHP set to display errors? You can add the following to the top of your script so that errors are displayed:


<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
?>

Note that the above should only be present when you're developing the script or debugging. The errors generated by PHP shouldn't be displayed to your visitors. They're not very user friendly and they can potentially reveal information to someone trying to compromise your website.

let's us leave the issue of escape string first i'll implement it later , i'm already changed the sql statement as ginerjm said,  but the issue here is when you click the pagination number it show the first  page only and the remaining page link show blank page i want to match the returned results with the page please try testing my code if i don't understand show me  just an example or use my code to show me any problem you think it may have as you quoted sql line 

Link to comment
Share on other sites

<?php
if(isset($_GET['submit'])){
	$names = $_GET['search'];
	$escapeString = mysqli_real_escape_string($conn, $names);	
		if(!empty($escapeString)){
			$resultPerPage = 8;  
				if (isset($_GET["page"])) { $page  = $_GET["page"]; 
				} else { 
				$page=1;
				}
				$startPage = ($page-1) * $resultPerPage;
				$sql = "SELECT * FROM movies WHERE name LIKE '%$escapeString%' OR  producer LIKE '%$escapeString%'  LIMIT ".$startPage.",".$resultPerPage;
				$result = mysqli_query($conn, $sql);
				while($row = mysqli_fetch_assoc($result)){	
						
						echo '  <div>

						<span>'.$row['name'].'</span>

						<span>'.$row['producer'].'</span>

						<p>'.$row['shortDescription'].'</p>  

						</div>  ';
				}
				echo '<ul class="pagination">';
				if ($page == 1) { 
				echo '<li class="disabled"><a href="#">First</a></li>
				<li class="disabled"><a href="#">«</a></li>';
				} else { 
				$linkPrev = ($page > 1) ? $page - 1 : 1;
				echo '<li><a href="search.php?page=1">First</a></li>
				<li><a href="search.php?page='.$linkPrev.'">«</a></li>';
				}
				$cSql = "SELECT COUNT(*) FROM movies WHERE name LIKE '%$escapeString%' OR  producer LIKE '%$escapeString%'";
			
				$cResult = mysqli_query($conn, $cSql); 
				$row = mysqli_fetch_row($cResult);
				$totalRecords = $row[0];
			
				$noOfpage = ceil($totalRecords/ $resultPerPage);
				$linkNumber = 7;
				$startNumber = ($page > $linkNumber) ? $page - $linkNumber : 1;
				$endNumber = ($page < ($noOfpage - $linkNumber)) ? $page + $linkNumber : $noOfpage; 
				for ($i = $startNumber; $i <= $endNumber; $i++) {
				$linkActive = ($page == $i) ? 'class="active"' : '';
					echo '<li '.$linkActive.'><a href="search.php?page='.$i.'">'.$i.'</a></li>';
				}
				if ($page == $noOfpage) {
					echo '<li class="disabled"><a href="#">»</a></li>
					<li class="disabled"><a href="#">Last</a></li>';
				} else 
				{
				$linkNext = ($page < $noOfpage) ? $page + 1 : $noOfpage;
					echo '<li><a href="search.php?page='.$linkNext.'">»</a></li>
					<li><a href="search.php?page='.$noOfpage.'">Last</a></li>';
				}
				echo '</ul>';	
				}
				else
				{
					echo '
					<div class="head">
					<h6 class="ml-0"> RESULTS REPORT:</h6>
					</div>';	
					echo '<span> The keyword </span><span class="text-warning"> '.$escapeString.' </span><span> is not available in our database </span>';
				}		
				}	
				else
				{
				echo 'You have to Enter movie name or producer';		
				}

		?>

-

Link to comment
Share on other sites

what does the 'view source' in your browser of this blank page show?  i'm betting it has your - 'You have to Enter movie name or producer', because there are two apparent problems in your current code .

the 1st problem is $_GET['search'] isn't being added to the pagination links, which you would have noticed if you had looked at the URL in your browser's address bar. you can edit all the places where you are producing pagination links or you can use http_build_query() like has been suggested (if you search the  phpfreaks forum, you will find examples showing how to use it with pagination.)

the 2nd problem is you have too many different $_GET variables from the search form. you only need $_GET['search']. remove $_GET['submit'] and change your logic to only use $_GET['search'].

Link to comment
Share on other sites

12 hours ago, mac_gyver said:

what does the 'view source' in your browser of this blank page show?  i'm betting it has your - 'You have to Enter movie name or producer', because there are two apparent problems in your current code .

the 1st problem is $_GET['search'] isn't being added to the pagination links, which you would have noticed if you had looked at the URL in your browser's address bar. you can edit all the places where you are producing pagination links or you can use http_build_query() like has been suggested (if you search the  phpfreaks forum, you will find examples showing how to use it with pagination.)

the 2nd problem is you have too many different $_GET variables from the search form. you only need $_GET['search']. remove $_GET['submit'] and change your logic to only use $_GET['search'].

ok let me try your suggestion , after that i'll return back, but after clicking page link and looking on the url bar this is showing 

http://localhost/movies_dbase/search.php?page=1

with no data on the page

Link to comment
Share on other sites

On 5/21/2019 at 6:24 PM, mac_gyver said:

what does the 'view source' in your browser of this blank page show?  i'm betting it has your - 'You have to Enter movie name or producer', because there are two apparent problems in your current code .

the 1st problem is $_GET['search'] isn't being added to the pagination links, which you would have noticed if you had looked at the URL in your browser's address bar. you can edit all the places where you are producing pagination links or you can use http_build_query() like has been suggested (if you search the  phpfreaks forum, you will find examples showing how to use it with pagination.)

the 2nd problem is you have too many different $_GET variables from the search form. you only need $_GET['search']. remove $_GET['submit'] and change your logic to only use $_GET['search'].

So how to pass  $_GET['search'] in pagination I don't understand because I have another  $_GET['page']

Link to comment
Share on other sites

5 hours ago, mahenda said:

So how to pass  $_GET['search'] in pagination I don't understand because I have another  $_GET['page']

i noticed you viewing a forum thread earlier that used http_build_query() to produce pagination links with any existing get parameters. what issue are you still having with doing this yourself?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.