tony_stockman
asked on
Match and count user inputs against Mysql fields
Hi there,
I am new to PHP & Mysql and I have managed to build a basic search page.
please see the code below
Currently i am able to select single field type and search within....
I would like to search multiple fields at the same time and return the matching
E.g.
FieldType = Sounds
User input = ?
FieldType = Generic
User input = ?
FieldType = Activity
User input = ?
SEARCH BUTTON
RESULT
Echo story IDs of matching all three input in each rows in the table
and how many in total
Echo story ids of matching Two types appearing in a single row
and how many in total
Echo Story id of matching One
and how many in total
Echo how many in total each words occurs all three fields
Thank you..and please ask me if i don't make sense..thanks again
I am new to PHP & Mysql and I have managed to build a basic search page.
please see the code below
Currently i am able to select single field type and search within....
I would like to search multiple fields at the same time and return the matching
E.g.
FieldType = Sounds
User input = ?
FieldType = Generic
User input = ?
FieldType = Activity
User input = ?
SEARCH BUTTON
RESULT
Echo story IDs of matching all three input in each rows in the table
and how many in total
Echo story ids of matching Two types appearing in a single row
and how many in total
Echo Story id of matching One
and how many in total
Echo how many in total each words occurs all three fields
Thank you..and please ask me if i don't make sense..thanks again
search.php
-----search page
$searchtype=$_POST['searchtype'];
$searchterm=trim($_POST['searchterm']);
if (!$searchtype || !$searchterm) {
echo 'You have not entered search details. Please go back and try again.';
exit;
}
if (!get_magic_quotes_gpc()){
$searchtype = addslashes($searchtype);
$searchterm = addslashes($searchterm);
}
@ $db = new mysqli('localhost', 'root', 'pass', 'dbname');
if (mysqli_connect_errno()) {
echo 'Error: Could not connect to database. Please try again later.';
exit;
}
$query = "select * from stories where ".$searchtype." like '%".$searchterm."%'";
$result = $db->query($query);
$num_results = $result->num_rows;
echo "<p>Number of records found: ".$num_results."</p>";
for ($i=0; $i <$num_results; $i++) {
$row = $result->fetch_assoc();
echo "<strong> <br />Stor ID:</strong> ";
echo stripslashes($row['id']);
echo.....
}
$result->free();
form.php
<form action="search.php" method="post">
Search by Person:<br />
<select name="searchtype">
<option value="sounds">Sounds</option>
<option value="generic">Generic</option>
<option value="activity">Activity</option>
</select>
<br />
Enter Search Term:<br />
<input name="searchterm" type=”"text" size="40"/>
<br />
<input type="submit" name="submit" value="Search"/>
</form>
It's really not making much sense. Can you show an example of what you are looking for.
it seems that the only change u need to make is to the sql query.
if u wish to search multiple fields at the same u need to add integrate those fields in your query.
i see that u have 3 fields from user input: Sounds, Generic and Activity.
if u wish to search multiple fields at the same u need to add integrate those fields in your query.
i see that u have 3 fields from user input: Sounds, Generic and Activity.
ASKER
Thanks for responding, im sorry i wasnt clear enoungh...okee hear it is... What i want is shown under "E.g" n "RESULT" above and what i have is in the code i hav provided...
My current search page has only one field please see my form php code n search
My current search page has only one field please see my form php code n search
ASKER
Hi.... here is what I want again
The search form should look like this
Example \
FieldName = "Question"
User input = ? e.g. what is it?
FieldName = "Answers"
User input = ? e.g. nothing
FieldName = "Elements"
User input = ? e.g. animal
SEARCH BUTTON
Once user clicked on search button.... it should retrieve and echo all the Records(rows) which contain each keyword in a single row under each field name. We display 100% match on the top of the page and then records which 50% matches (Two fields) and One field at the bottom.
the Result page should look like:
"2 records found with 100% match"
id Question Answers Elements
1 what is it? nothing animal
33 what is it? nothing animal
"2 records match 60%(2/3)"
id Question Answers Elements
25 what is it? somthing animal
60 where is it? nothing animal
[B]"2 record match 30%(1/3)"
[B] id Question Answers Elements [/B]
30 how is it? nothing animal
80 where is it? nothing animal
please help..
Thank you
Currently my search engine retrieve data from single field..please feel free to modify my code and help me.
The search form should look like this
Example \
FieldName = "Question"
User input = ? e.g. what is it?
FieldName = "Answers"
User input = ? e.g. nothing
FieldName = "Elements"
User input = ? e.g. animal
SEARCH BUTTON
Once user clicked on search button.... it should retrieve and echo all the Records(rows) which contain each keyword in a single row under each field name. We display 100% match on the top of the page and then records which 50% matches (Two fields) and One field at the bottom.
the Result page should look like:
"2 records found with 100% match"
id Question Answers Elements
1 what is it? nothing animal
33 what is it? nothing animal
"2 records match 60%(2/3)"
id Question Answers Elements
25 what is it? somthing animal
60 where is it? nothing animal
[B]"2 record match 30%(1/3)"
[B] id Question Answers Elements [/B]
30 how is it? nothing animal
80 where is it? nothing animal
please help..
Thank you
Currently my search engine retrieve data from single field..please feel free to modify my code and help me.
form.php
<form action="results.php" method="post">
Search within:<br />
<select name="searchtype">
<option value="question">Question</option>
<option value="answers">Answers</option>
<option value="elements">Elements</option>
</select>
<br />
Enter Search Term:<br />
<input name="searchterm" type=”"text" size="40"/>
<br />
<input type="submit" name="submit" value="Search"/>
</form>
result.php
<?php
// create short variable names
$searchtype=$_POST['searchtype'];
$searchterm=trim($_POST['searchterm']);
if (!$searchtype || !$searchterm) {
echo 'You have not entered search details. Please go back and try again.';
exit;
}
if (!get_magic_quotes_gpc()){
$searchtype = addslashes($searchtype);
$searchterm = addslashes($searchterm);
}
@ $db = new mysqli('localhost', 'root', '', '');
if (mysqli_connect_errno()) {
echo 'Error: Could not connect to database. Please try again later.';
exit;
}
$query = "select * from table where ".$searchtype." like '%".$searchterm."%'";
$result = $db->query($query);
$num_results = $result->num_rows;
echo "<p>Number of records found: ".$num_results."</p>";
for ($i=0; $i <$num_results; $i++) {
$row = $result->fetch_assoc();
echo "<p><strong><br>".($i+1).". First Name: </strong>";
echo htmlspecialchars(stripslashes($row['fname']));
echo "<strong><br />Last Name: </strong>";
echo stripslashes($row['lname']);
echo "<strong>Question: </strong>";
echo stripslashes($row['question']);
echo "<strong> <br />Answer: </strong>";
echo stripslashes($row['answers']);
//echo "</p>";
echo "<strong> <br />Elements:</strong> ";
echo stripslashes($row['elements']);
$db->close();
?>
Here's how I might approach the task. Take the client's input search term, and do three separate queries - one each against each of the three salient columns in the table. The number of rows in the three results sets will tell you whether you've found the terms in (for example) Questions only, or Questions plus Answers, etc.
ASKER
hi Ray..
yes.. i want the output to be like this
If the user input "what is it?" for the Question, "nothing" in "answers" and "animal" in Elements search fields and hit search button result shuld will come up like this...
"2 records found with 100% match (3/3)"
id Question Answers Elements
1 what is it? nothing animal
33 what is it? nothing animal
"2 records match 60%(2/3)"
id Question Answers Elements
25 what is it? somthing animal
60 how is it? nothing animal
[B]"2 record match 30%(1/3)"
[B] id Question Answers Elements [/B]
30 how is it? something animal
80 where? nothing nonanimal
how do i modify the query and out put result in Result.php above and the and code in form.php
Thanks for your time
I don't really have the time to work out all the details here, so I'll just try to suggest some ways to approach this.
First, create a temporary table with all the matches in the disjunctive ('OR') so you're looking for this kind of thing:
Question = 'what is it?' OR Answers = 'nothing' OR Elements = 'animal'
This would give you all 6 of the rows you've got above. Next you can create queries against this (smaller, faster) temporary table to match the conjunctive ('AND') for the 100% match, and combinations of AND/OR for the 2/3 and 1/3 matches.
First, create a temporary table with all the matches in the disjunctive ('OR') so you're looking for this kind of thing:
Question = 'what is it?' OR Answers = 'nothing' OR Elements = 'animal'
This would give you all 6 of the rows you've got above. Next you can create queries against this (smaller, faster) temporary table to match the conjunctive ('AND') for the 100% match, and combinations of AND/OR for the 2/3 and 1/3 matches.
ASKER
HI Ray,
Can you please write just the SQL query for me lease.. I am not an expert in php.. i need help
Thank you
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you for your time Ray.
I will go through the code now...
ASKER
Thank you for your time Ray.
I will go through the code now...
This question has been classified as abandoned and is being closed as part of the Cleanup Program. See my comment at the end of the question for more details.