how to make a image search engine? - php

I'm trying to make a image search. it will be showing images of movies and i also want the movie name to show below the image. this is what i got far
In my phpMy i got 5 rows
id int(11)
title varchar(100)
description text
url text
keywords varchar(100)
Here is my search bar
<div>
<form action='/search.php' method='GET' style="margin-bottom:1px;">
<input id='searchbar' type='text' size='65' name='search' placeholder="search for movies & tv shows">
<input id='submit' type='submit' name='submit' value='Search' >
</form>
</div>
and this is the results page
<html>
<head>
<title>FastMegaMedia</title>
<link rel="icon" href="img/putlockermedia_logo.png" type="image/icon">
<link rel="stylesheet" href="Style/index.css" media="screen">
</head>
<body>
<?php include_once("/php/header.php"); ?>
<table id=content width="100%" height="25" border="0" cellpadding="0" cellspacing="0">
<?php include_once("/php/ad1.php"); ?>
<td width="63%" valign="top">
<section class="content"> <!-- start of conntent -->
<?php
$x = 0;
$construct = '';
$button = $_GET ['submit'];
$search = $_GET ['search'];
if(!$button)
echo "you didn't submit a keyword";
else
{
if(strlen($search)<=1)
echo "Search term too short";
else{
echo "You searched for <b>$search</b> <hr size='1'></br>";
mysql_connect("localhost","root","");
mysql_select_db("search");
$search_exploded = explode (" ", $search);
foreach($search_exploded as $search_each)
{
$x++;
if($x==1)
$construct .="keywords LIKE '%$search_each%'";
else
$construct .="AND keywords LIKE '%$search_each%'";
}
$construct ="SELECT * FROM searchengine WHERE $construct";
$run = mysql_query($construct);
$foundnum = mysql_num_rows($run);
if ($foundnum==0)
echo "Sorry, there are no matching result for <b>$search</b>.</br></br><li>
Try more general words. for example: If you want to search 'how to create a website'
then use general keyword like 'create' 'website'</li><li> Try different words with similar
meaning</li><li> make sure you are spelling is correct</li>";
else
{
echo "$foundnum results found !<p>";
while($runrows = mysql_fetch_assoc($run))
{
$title = $runrows ['title'];
$desc = $runrows ['description'];
$url = $runrows ['url'];
echo "
<a href='$url'><b>$title</b></a><br>
$desc<br>
<a href='$url'></a><p>
";
}
}
}
}
?>
</section> <!-- end of conntent -->
</td>
<?php include_once("/php/ad2.php"); ?>
</table>
</body>
</html>
what could I do to make it show images instead of just showing the tittle, i know i'm missing something.. i'm new to this btw be nice

First, you appear to be vulnerable to SQL injection attacks. Never trust unsanitized user-input data.
Well, you need to store the image somewhere (I'd store it on disk with the path/file name stored in the database as type varchar), then in your PHP code where you output the title, description, and URL, just also query the database for the image path and <img src... that.

You missed the images and the references to them (if they exists in your database):
echo "
<a href='$url'><b>$title</b></a><br>
$desc<br>
<a href='$url'></a><br>
<img class='images' src='$srcOfFoundImage' title='$titleOfFoundImage'><p>
";
If you dont have the images, you can ask Google to find them.

Related

Is there a way for me to make a view more button on my search results page?

I have made a site called tantami.com and what I have been trying to find out is if I would be able to make a view more link on my search results page. This is so that if a user types in a keyword with say more than 20 results, the page will display 20 with a button at the bottom with a view more link on it. I have tried lots of different ways of doing this and I am not very good with Javascript or other languages apart from HTML, CSS and PHP
Here is my code for the search results page:
<?php
include("center.php")
?>
<hr>
<div id="right">
<h1>Adverts</h1>
<p></p>
<center>
<img src="http://thehostgroup.com/banners/120x600.gif" alt="The Host Group" title="banners/120x600" width="120" height="600" class="alignnone size-full wp-image-322" /></center>
</div>
<div class="item">
<div id="left">
<?php
$button = $_GET ['submit'];
$search = $_GET ['search'];
if(!$button)
echo "<p>Sorry but we can't find any results if you do not submit a keyword</p>";
else
{
if(strlen($search)<=1)
echo "<p>Sorry but the search term you provided is too short for us to find any items related.</p> ";
else{
echo "<p>You searched for <b>$search</b> <hr size='1'></br></p><p>If your site hasn't shown up just visit the Members area and submit it!</p>";
mysql_connect("localhost","User","Password");
mysql_select_db("Database");
$search_exploded = explode (" ", $search);
foreach($search_exploded as $search_each)
{
$x++;
if($x==1)
$construct .="keywords LIKE '%$search_each%'";
else
$construct .="AND keywords LIKE '%$search_each%'";
}
$construct ="SELECT * FROM searchengine WHERE $construct";
$run = mysql_query($construct);
$foundnum = mysql_num_rows($run);
if ($foundnum==0)
echo "<p>Sorry, there are no matching results for <b>$search</b>.</br></br>1.
Try more general words. for example: If you want to search 'how to create a website'
then use general keyword like 'create' 'website'</br>2. Try different words with similar
meaning</br>3. Please check your spelling.</br>4. If you have any ideas on sites you want to show up submit it to our database.</p>";
else
{
echo "<p>We found $foundnum results!</p>";
while($runrows = mysql_fetch_assoc($run))
{
$title = $runrows ['title'];
$desc = $runrows ['description'];
$url = $runrows ['url'];
echo "
<p><a href='$url'><b>$title</b></a><br>
$desc<br>
<a href='$url'>$url</a></p>
";
}
}
}
}
?>
</div>
</div>
</div>
<center>
</center>
<?php
include("footer.php")
?>
If anyone is able to give me some help on doing this it would help me alot!
Thanks

Linking to search results directly

I got myself into a project at work that is going beyond my (very modest) coding skills.
So i have a database with 6 fields. I'm able to make searchs on it using php (i followed a youtube tutorial), add records, delete records and update records. All of that is working just fine.
What i need now is this:
I need to link search results (only a few, the most popular) on a image.
Per example, i have a company logo on my main page (let's say company name is..i dunno.. "Google". When i click on it, i wanted it to redirect to the search results of "google", like if i had inserted that in the search field. Is that possible?
(notice that i don't have companies in my database, i'm just trying to give an example so that people can understand what i intend. In my database i would need to link to reflect search results of the field "unidade")
Here is my code so far. The add / update .php i don't think are needed.
<?php
// include the connection file
include "connection.php";
$sql = "SELECT * FROM usuariotb";
if (isset($_POST['search'])) {
$search_term = mysql_real_escape_string($_POST['search_box']);
$sql .=" WHERE nome LIKE '%".$search_term."%'";
$sql .=" OR posto = '{$search_term}' ";
$sql .=" OR nim = '{$search_term}' ";
$sql .=" OR unidade LIKE '%".$search_term."%'";
$sql .=" OR codigoueo LIKE '%".$search_term."%'";
}
$query = mysql_query($sql) or die (mysql_error());
if (isset($_GET['recordId'])) {
$id = mysql_real_escape_string($_GET['recordId']);
$sql_delete = "DELETE FROM usuariotb WHERE id = {$id}";
mysql_query($sql_delete) or die(mysql_error());
header("location:display_data.php");
exit();
}
?>
<html>
<head>
<meta charset="iso-8859-1">
<title></title>
<link rel="stylesheet" type="text/css" href="format.css" />
</head>
<body>
<div class="container">
<form name="search_form" method="POST" action="display_data.php">
Search: <input type="text" name="search_box" value=""/>
<input type="submit" name="search" value="Procurar">
</form>
<div class="container">
<div class="content">
<div class="toolbar">Adicionar Novo Militar</div>
</div>
<div class="toolbar">Voltar à página de pesquisa</div>
</div>
</div>
<div class="container">
<div class="content">
<table width="90%" cellpadding="5" cellspacing="5">
<tr>
<td><strong>Nome</strong></td>
<td><strong>Nim</strong></td>
<td><strong>Posto</strong></td>
<td><strong>Unidade</strong></td>
<td><strong>Sigla Unidade</strong></td>
<td><strong>Observações</strong></td>
<td><strong>Acções</strong></td>
</tr>
<?php if (mysql_num_rows($query)) { ?>
<?php while ($row=mysql_fetch_array($query)) {?>
<tr>
<td><?php echo $row['nome'];?></td>
<td><?php echo $row['nim'];?></td>
<td><?php echo $row['posto'];?></td>
<td><?php echo $row['unidade'];?></td>
<td><?php echo $row['codigoueo'];?></td>
<td><?php echo $row['observacoes'];?></td>
<td>Delete</td>
<td>Edit</td>
</tr>
<?php } /* end loop */ ?>
<?php } else { ?>
<h2> Nothing to display!</h2>
<?php } /* end rows checking */ ?>
</table>
</div>
</div>
</body>
</html>
First of all, I empathize with you regarding being tasked with a more complex task than your current ability. Been there, done that. Many companies do this.
Regarding your task, for search every website usually uses GET to do the search e.g. https://www.google.co.in/search?q=stackoverflow. Notice the q=stackoverflow part. For this you do not need to submit any form, just use the URL directly.
So, I suggest you visit the websites where you want to link directly for search. Search for some sample text, say TEST and observe the URL of the page that opens.
Then using JavaScript/jQuery/etc, on click of the logo, read whatever text you want to search, create a URL in JavaScript and redirect there.
This is the general idea, feel free to comment and ask if you face any issues in implementation.

Creating search engine in PHP [duplicate]

This question already has answers here:
Build SELECT query with dynamic number of LIKE conditions as a mysqli prepared statement
(2 answers)
Closed 1 year ago.
I have created search engine so when someone type in search box it takes data from my database and show the results in images.
Everything is working fine but if I have data in database with Name like 'Lamborghini', when someone type in search box 'lambo' or 'Lamborghini' it is working fine and show the result but when he types 'Lamborghini car' it is not showing any result.
Here is my PHP Code:
<html>
<link href="css/imgpages.css" rel="stylesheet" type="text/css">
<head>
<title>Search the Database</title>
</head>
<body>
<form action="" method="post">
Search: <input type="text" name="term" /><br />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
<?php
include("connection.php");
$term = $_POST['term'];
$query = mysql_query("select * from save_data where Title like '%$term%'");
while($row = mysql_fetch_array($query)) {
$post_id = $row['ID'];
$title = $row['Title'];
$image = $row['Name'];
?>
<div id="body">
<a href="pictures.php?title=<?php echo $title; ?>">
<div id="title"><?php echo $title; ?></div></a>
<a href="pictures.php?title=<?php echo $title; ?>">
<img src='uploads/<?php echo $image; ?>' width='140' height='140'></a>
</div>
<?php } ?>
You should:
fix your SQL (make it secure - just one search could delete your whole database if you leave it like this, switch to mysqli or PDO because mysql_* functions are deprecated)
split your query string into separate words and decide how to handle them (whether you'll use OR or AND... in other words, do all search terms need to match, or any of them, etc.)
When you deal with all that and decide to make it more advanced, you should learn about full-text searching.
It is 'cause { %$term% } means the query try search something that contain all of the string in $term. Try this :
$term = explode(" ",$term);
if (count($term) > 0) {
$Where = '';
foreach($term as $Item) {
$Where .= "Title like '%$Item%' OR ";
}
$Where = substr($Where,0,-4);
$query = mysql_query("SELECT * FROM `save_data` WHERE $Where");
}
In this way the search will check out all words.
As the above people said just Explode the input string into an array and search for documents(description, keywords) where most of the elements match. Also take care of SQL Injection.
first make your term search explode so it would go into an array
<?php
$term = $_POST['term'];
$words = explode(" ", $term);
?>
then create a forearch loop for the mysql_query of your search and implode with "OR"
<?php
foreach ($words as $words)
{
$queries[]="select * from save_data where Title LIKE '%" .
mysql_real_escape_string($words) . "%'";
}
$query=implode(' OR ' ,$queries);
$results=mysql_query($query);
while($row = mysql_fetch_array($results))
{
$post_id = $row['ID'];
$title = $row['Title'];
$image = $row['Name'];
?>
<div id="body">
<a href="pictures.php?title=<?php echo $title; ?>">
<div id="title"><?php echo $title; ?></div>
</a>
<a href="pictures.php?title=<?php echo $title; ?>">
<img src='uploads/<?php echo $image; ?>' width='140' height='140'>
</a>
</div>
<?php } ?>

How to show this image?

<?php
require 'db.php';
include_once("header.php");
include_once("functions.php");
include_once("profile.php");
if(isset($_POST['search_term'])){
$search_term = mysql_real_escape_string(htmlentities ($_POST['search_term']));
if(!empty($search_term)){
$search = mysql_query("SELECT `username`,`id` FROM `users` WHERE `username` LIKE '%$search_term%' and `business` <> 'business'");
$result_count = mysql_num_rows($search);
$suffix = ($result_count != 1) ? 's' : '';
echo '<div data-theme="a">Your search for <strong>' , $search_term ,'</strong> returned <strong>', $result_count,' </strong> record', $suffix, '</div>';
while($results_row = mysql_fetch_assoc($search)){
echo '<div data-theme="a"><strong>', "<img src='/image/<?php echo $image; ?>' width= 50px height=50px>", $results_row['username'], '</strong></div>';
$following = following($_SESSION['userid']);
if (in_array($key,$following)){
echo ' <div action= "action.php" method="GET" data-theme="a">
<input type="hidden" name="id" value="$key"/>
<input type="submit" name="do" value="follow" data-theme="a"/>
</div>';
}else{
echo " <div action='action.php' method='GET' data-theme='a'>
<input type='hidden' name='id' value='$key'/>
<input type='submit' name='do' value='follow' data-theme='a'/>
</div>";
}
}
}
}
?>
I would like some help putting the user image into the echo section of this code. I am not exactly sure how to do this so that it puts the image on the correct line of the search. Any advice would be greatly appreciated. Below is the line of code that I am referring too. Thanks.
while($results_row = mysql_fetch_assoc($search)) {
echo '',
"' width= 50px
height=50px>", $results_row['username'], '';
I don't see $image defined anywhere in your code, so I'm going to assume the image is being pulled from the database.
If that's the case then you'll want to do something like this:
while($results_row = mysql_fetch_assoc($search)){
echo '<div data-theme="a"><strong><img src="/image/'.$results_row['image'].'" style='width:50px;height:50px' />'.$results_row['username'].'</strong></div>';
}
just write:
..."<img src='/image/<?php echo $image; ?>' width='50' height='50'>", ...
but are you sure that 50x50 is suitable? you might want to set only width=50 and leave the browser to set height accordingly.
$image = 'someImage.png';
echo '<div data-theme="a"><strong>', "<img src='/image/{$image}' width= 50px height=50px>", $results_row['username'],  '</strong></div>';
You would want something like this:
<?php
while($results_row = mysql_fetch_assoc($search)){
?>
<div data-theme="a">
<img src="/image/<?php echo $image; ?>" width="50px" height="50px">
<strong>
<?php echo $results_row['username']; ?>
</strong>
</div>
<?php
}
?>
A few things to check though:
dont use tags to wrap tags. They're generally used for text only
You're not selecting any image on your query.
don't use echo to output html its ugly.
don't use GET.
Use prepared statements (PDO or mysqli) to protect from mysql injection.

Filter mysql search results using php and/or jquery

I've built a simple search engine for my site, the way it works is when the user enters a keyword it queries the database and displays the results.
My database table looks like this:
game_id title developer publisher genre release_date platform
rating image_location description
I want to allow the user to filter through the results once they have made a search, preferably without refreshing the page(only been coding for a month but have a general understanding of jquery and ajax).
Here is my code, im using includes so the search bar, search results and the page that queries are seperated.
page 1 search bar
<div id=\"top_search\">
<form name=\"input\" action=\"search.php\" method=\"get\" id=\"search_form\">
<input type=\"text\" id=\"keywords\" name=\"keywords\" size=\"128\" class=\"searchbox\" value=\"$defaultText\">
<select id=\category\" name=\"category\" class=\"searchbox\">
";
createCategoryList();
echo '
</select>
<input type="submit" value="Search" class="button" />
</form>
</div>
</header>
';
page 2 display results
<?php
session_start();
include ("includes/search_func.php");
if (isset($_GET['keywords'])){
$keywords = mysql_real_escape_string(htmlentities(trim($_GET['keywords'])));
$errors = array();
if(empty($keywords)){
$errors[] = 'Please enter a search term';
}else if(strlen($keywords)<3){
$errors[] = 'Your search term must be at least 3 characters long';
}else if(search_results($keywords) == false){
$errors[] = 'Your search for'.$keywords.' returned no results';
}
if(empty($errors)){
function diplay_results($keywords){
$results = search_results($keywords);
$results_num = count($results);
foreach($results as $result ){
echo '
<div id="game_result">
<a href= game_page.php?game_id='.$result['game_id'].'><img src= '.$result['image_location'].' id="image" /></a>
<div id="main_title">
'.$result['title'].' </h2>
<h3 id="platform">for '.$result['platform'].'</h3>
</div>
<p id=game_description>'.$result['description'].'</p>
<div id="right_side">
<h4 id="rating">'.$result['rating'].'</h4>
</div>
<hr id="hr"/>
</div>
';
}
}
}else{
foreach($errors as $error){
echo $error, '<br />';
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Search</title>
<link rel="stylesheet" href="css/main.css">
<link rel="stylesheet" href="css/search.css">
</head>
<body>
<div id="wrapper">
<div id="main_aside">
//filter navigation will go here
</div>
<div id="main_section" class="header">
<?php diplay_results($keywords)?>
</div>
</div>
</body>
</html>
page 3 querying database
<?php
include 'includes/connect.php';
function search_results($keywords){
$returned_results = array();
$where = "";
$keywords = preg_split('/[\s]+/', $keywords);
$total_keywords = count($keywords);
foreach($keywords as $key=>$keyword){
$where .= "title LIKE '%$keyword%'";
if($key != ($total_keywords - 1)){
$where .= " AND ";
}
}
$results ="SELECT * FROM games WHERE $where ";
echo $results;
$results_num = ($results = mysql_query($results)) ? mysql_num_rows($results) : 0;
if($results_num === 0){
return false;
}else{
while($row = mysql_fetch_assoc($results)){
$returned_results[] = array(
'game_id' => $row['game_id'],
'title' => $row['title'],
'platform' => $row['platform'],
'rating' => $row['rating'],
'image_location' => $row['image_location'],
'description' => $row['description'],
);
}
return $returned_results;
}
}
?>
So in case its not clear i want to allow the user to search for a key term, then it will display from the database any games with the keyword in the title(i already have this part working). Now what cant figure it out is how to let the user filter their results to lets say only action games, along with lets say a certain platform and so on.
Like i said ive only been doing this for a month so if im doing something wrong dont hesitate to call me an idiot, but please help. Any help would be appreciated even links to tutorials or book recommendations.Thanks.
Your approach is proper. To filter the search results on the basis of publisher name or developer, genre, release date,
1) You can have another "Advanced Search" Page wherein you could plugin the HTML for these attributes, and just tweak the WHERE clause of your query. So there will be two pages for search - a simple search that will allow the user to filter on the basis of just the title name and say the category and an advanced search page to filter on the basis of the other attributes.
2) You may even choose to provide options on the basic search page itself for filtering on the basis of additional parameters.
How you choose your interface would be a decision mandated by your requirement specification. Note that only your WHERE clause will change in all cases.

Categories