Page 1 of 1
Hello, i need help:(
Posted: Tue Dec 12, 2006 11:07 am
by roxiroxi
Hello im new in this forum,
I am very desperated, I have to finish a search engine for the university for this thursday morning.
I didnt sleep last night, trying to improve the search thing but I cant get any logic implementation.
I have to do a search with php and mysql using logic such as AND, NOT, OR but i am very frustated.
I cant get any help from anyone and I cant find examples in the net. If anyone can help me I could pay him by paypal.
I would like to get a text box, where I could type the name of a person or two names, and a following drop-down menu where the options on it would be: matches exactly or include. Then a buton that when I click on it goes to o the php and shows the results. The code of both pages would be very apreciated.
My email is
davidcadinot@gmail.com.
Thank you.
Posted: Tue Dec 12, 2006 11:14 am
by Citizen
A search engine for the web or just the ability to search through one database?
Post your code, it'll give us something to work with.
Posted: Tue Dec 12, 2006 11:25 am
by impulse()
Try this for me:
Code: Select all
$search = $_POST["textBoxInput"];
$query = mysql_query("SELECT *
FROM somewhere
WHERE something LIKE '%$search%'");
while ($res = mysql_fetch_array($query)) {
$results[$i] = $res['columnName'];
$i++;
}
Code: Select all
<form name = 'list' method = 'post' action = 'page.php'>
<select name = 'list1'>
Code: Select all
for ($i = 0; $i < count($results); $i++) {
echo "<option value = '$results[$i]'> $results[$i] </option>";
}
Regards,
[/syntax]
My code
Posted: Tue Dec 12, 2006 11:54 am
by roxiroxi
This is the code that I have but it is diferent from what I want to do. It is a drop down menu that get user details from a database. when selecting a user and then clicking on search it goes to another php that list the products that the user has in a table. But this is not what I want to do, I want a text box, where the user can type words, and then a drop-down menu, that asks the user to select if want to get results of any word or the exact word for example. when user click on search a list of results appears
Code: Select all
?php
// script to display all the films in drop down menu
// connection information
$hostName = "localhost";
$userName = "root";
$password = "password";
$dbName = "dvdswap";
// make connection to database
mysql_connect($hostName, $userName, $password) or die("Unable to connect to host $hostName");
mysql_select_db($dbName) or die( "Unable to select database $dbName");
$query = "SELECT DISTINCT product.userid
FROM product ORDER BY userid";
$result = mysql_query($query);
// Determine the number of results
$number = mysql_numrows($result);
// Create drop-down menu
print "<tr>
<td>View films of a selected owner:<p></td>
<td><form action=\"listFilmCriteria.php\" method=\"get\">
<select name=\"userid\">
<option value=\"\">Select owner</option>";
for ($i=0; $i<$number; $i++) {
$userid = mysql_result($result,$i,"userid");
print "<option value=\"$userid\">$userid </option>";
}
print "</select><input type=\"submit\" value=\"Search\"
name=\"submit\"></form></tr>";
// Close the database connection
mysql_close();
?>
------------------------------------------------------------------------------------------------------------
<?php
// script to display all the Computers in the Computers table
// connection information
$hostName = "localhost";
$userName = "root";
$password = "password";
$dbName = "dvdswap";
$userid = $_GET['userid'];
$genre = $_GET['genre'];
$director = $_GET['director'];
$pyear = $_GET['pyear'];
$cert = $_GET['cert'];
// make connection to database
mysql_connect($hostName, $userName, $password) or die("Unable to connect to host $hostName");
mysql_select_db($dbName) or die( "Unable to select database $dbName");
$query ="SELECT * FROM product where userid='$userid' or genre='$genre' or director='$director' or pyear='$pyear' or cert='$cert' ORDER BY title";
$result = mysql_query($query);
// Determine the number of results returned
$number = mysql_numrows($result);
// Print the relevant information
if ($number == 0)
{
print "<h3>Sorry, there are no records matching your criteria:</h3>
<p><b>$number</b> films found.</p>
<p>You did not select a valid input. <br>Please try again.</br></p>
<p>Alternatively you could:
<ul>
<li>Conduct a quick search at the right of the page.</li>
<li>Conduct an advanced film search by clicking <a href='advancedSearchFilm.php'>here.</a></li>
<li>Browse all films by clicking <a href='browseAllProducts.php'>here.</a></li>
</ul>";
}
else
{
print "<h3>We have $number product(s) that match your criteria:</h3>
<table cellpadding=5 width='100%'>
<tr bgcolor=#6D6867>
<td align='center'><font color=white><b>Title</b></font></td>
<td align='center'><font color=white><b>Year</b></font></td>
<td align='center'><font color=white><b>Format</b></font></td>
<td align='center'><font color=white><b>Certificate</b></font></td>
<td align='center'><font color=white><b>Owner</b></font></td></tr>";
for($i=0; $i<$number; $i++) {
$prodid = mysql_result($result, $i, "prodid");
$title = mysql_result($result, $i, "title");
$pyear = mysql_result($result, $i, "pyear");
$format = mysql_result($result,$i,"format");
$cert = mysql_result($result,$i,"cert");
$userid = mysql_result($result,$i,"userid");
/* print even-numbered rows with a grey background,
odd-numbered with a white background */
if ($i%2 == 0) {
print "<tr bgcolor=lightgrey>";
} else {
print "<tr>";
}
print "<td><a href=\"prodinfo.php?prodid=$prodid\">$title</a></td>
<td align='center'>$pyear</td>
<td align='center'>$format</td>
<td align='center'>$cert</td>
<td><a href=\"userinfo.php?userid=$userid\">$userid</a></td></tr>";
// <td><input type='submit' value='Contact' size=5></td>
// <td><input type='submit' value='Swap' size=5></td>
}
print "</table>";
}
// Close the database connection
mysql_close();
?>
Posted: Tue Dec 12, 2006 12:06 pm
by impulse()
The code I posted above should do that for you.
It firstly queries the DB for any entries that are similar to what was entered in a text box. It then generates a drop menu and puts all the results from the DB query into it. What else did you want it to do?
Posted: Tue Dec 12, 2006 1:37 pm
by roxiroxi
No no, i think we are confused, what i want is something like the advance product search engine in
http://www.movieswaps.co.uk. Do u know what i mean?
For example, you type something in 2 textboxes and then you select if you want that everything matches all criteria or any criteria.
Thank you
Posted: Tue Dec 12, 2006 1:47 pm
by impulse()
Code: Select all
if $_POST["title"] == "Matches Exactly") {
$query = mysql_query("SELECT *
FROM somewhere
WHERE columnName = '$_POST[textboxInput]");
}
For the others such as "Mathches All Words" I'd be guessing you'd have to
the input and run a query to fetch everything that matched every word from the explode.
For "Includes At Least 1 Word" use explode again and run a MySQL query that fetches anything in the DB that matches either of the exploded words.
Hope that helps,
Posted: Tue Dec 12, 2006 5:25 pm
by RobertGonzalez
This is a university project? Can your professor give you some insight. I think when it comes to helping out students doing school work, I am a little reserved about giving out code (this is just my opinion).
Posted: Wed Dec 13, 2006 6:34 am
by roxiroxi
feyd | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Hi, this is the code that im working with, the php problems are when i use if in the queries, i dont know how to fix it, anyone can help me???
Thank you for your help, it is very appreciated
[syntax="html"]
</form>
<form name="searchProduct" method="post" action="try2.php">
Title:
<input type="text" name="title" size="18"> <br />
Format:
<select name="option">
<option value="exactly"> exactly </option>
<option value="starts"> starts </option>
<option value="includes"> includes </option>
</select> <br />
<input type="submit" value="Search" class="rightColButton"><br />
</p>
</form>
[/syntax]
Code: Select all
<?php
// connection information
$hostName = "localhost";
$userName = "root";
$password = "password";
$dbName = "dvdswap";
$title = $_POST['title'];
$option = $_POST['option'];
// make connection to database
mysql_connect($hostName, $userName, $password) or die("Unable to connect to host $hostName");
mysql_select_db($dbName) or die( "Unable to select database $dbName");
if $_POST["option"] == "exactly")
{
$query = "SELECT product.prodid, product.title, product.pyear, product.format, product.cert, product.userid
FROM product where product.title = '$title'";
}
else $_POST["option"] == "starts") {
$query = "SELECT product.prodid, product.title, product.pyear, product.format, product.cert, product.userid
FROM product where product.title like '$title%'";
}
else $_POST["option"] == "includes") {
$query = "SELECT product.prodid, product.title, product.pyear, product.format, product.cert, product.userid
FROM product where product.title like '%$title%'";
}
$result = mysql_query($query);
// Determine the number of results
$number = mysql_numrows($result);
// if no results
if ($number == 0)
{
print "<h3>Sorry, there are no records matching your criteria:</h3>
<p><b>$number</b> films found.</p>
<p>Try narrowing your search criteria to bring back more results.</p>
<p>Alternatively you could:
<ul>
<li>Conduct a quick search at the right of this page.</li>
<li>Conduct an advanced film search by clicking <a href='advancedSearchFilm.php'>here.</a></li>
<li>Browse all films by clicking <a href='browseAllProducts.php'>here.</a></li>
</ul>";
}
else
{
print "<h3>There are $number films that mactch your criteria:</h3>
<table cellpadding=5 width='100%'>
<tr bgcolor=#6D6867>
<th><font color=white><b>Title</b></font></th>
<th><font color=white><b>Year</b></font></th>
<th><font color=white><b>Format</b></font></th>
<th><font color=white><b>Certificate</b></font></th>
<th><font color=white><b>Owner</b></font></th>
</tr>";
// Print the film names
for($i=0; $i<$number; $i++)
{
$prodid = mysql_result($result, $i, "prodid");
$title = mysql_result($result, $i, "title");
$pyear = mysql_result($result, $i, "pyear");
$format = mysql_result($result,$i,"format");
$cert = mysql_result($result,$i,"cert");
$userid = mysql_result($result,$i,"userid");
/* print even-numbered rows with a grey background,
odd-numbered with a white background */
if ($i%2 == 0)
{
print "<tr bgcolor=lightgrey>";
} else
{
print "<tr>";
}
print "<td><a href=\"prodinfo.php?prodid=$prodid\">$title</a></td>
<td align='center'>$pyear</td>
<td align='center'>$format</td>
<td align='center'>$cert</td>
<td align='center'><a href=\"userinfo.php?userid=$userid\">$userid</a></td></tr>";
// <td><input type='submit' value='Contact' size=4></td>
// <td><input type='submit' value='Swap' size=4></td>
}
print "</table>";
}
// Close the database connection
mysql_close();
?>
feyd | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Posted: Wed Dec 13, 2006 6:42 am
by roxiroxi
the problem is here but i cant get it
Code: Select all
$hostName = "localhost";
$userName = "root";
$password = "password";
$dbName = "dvdswap";
$title = $_POST['title'];
$option = $_POST['option'];
// make connection to database
mysql_connect($hostName, $userName, $password) or die("Unable to connect to host $hostName");
mysql_select_db($dbName) or die( "Unable to select database $dbName");
if ($_POST['option'] == "exactly")
{
$query = "SELECT product.prodid, product.title, product.pyear, product.format, product.cert, product.userid
FROM product where product.title = '$title'";
}
else ($_POST['option'] == "starts") {
$query = "SELECT product.prodid, product.title, product.pyear, product.format, product.cert, product.userid
FROM product where product.title like '$title%'";
}
else ($_POST['option'] == "includes") {
$query = "SELECT product.prodid, product.title, product.pyear, product.format, product.cert, product.userid
FROM product where product.title like '%$title%'";
}
Posted: Wed Dec 13, 2006 10:00 am
by roxiroxi
Thanks it is working!!!
Many thanks
Posted: Wed Dec 13, 2006 10:29 am
by RobertGonzalez
Your else statements are a bit wonky. What did you do to get it working?
Posted: Wed Dec 13, 2006 10:39 am
by roxiroxi
Everah | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
I use this:
Code: Select all
$query = "SELECT product.prodid, product.title, product.pyear, product.format, product.cert, product.userid, product.director, product.starring
FROM product where ";
switch($_POST['optionfilm']) {
case 'exactlyfilm' :
$query .=" title = '$title' ";
break;
case 'startsfilm' :
$query .=" title like '$title%'";
break;
case 'includesfilm' :
$query .= " title like '%$title%'";
break;
}
I got working the code that you posted bofore as well but didnt save it sorry.
Thanks.
Now im trying to allow the users to type AND, OR, NOT in the search text box, if anyone can help me it would be great.
Everah | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]