/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] how to search value from two columns based on third column

I have a database table named with books. it consists of something like below


—————————————————————

title | author | category

| |
————————————————————–

xyz madman fiction

adapt brainless fiction

adaxx stupid fiction

follow idiot journal

your man acadamic


————————————————————–

user is trying to search the value by author or title and based on category
ex: if user search for adapt by selecting fiction need display both adapt and adaxx
below is the code for searching and using mysql database

[code]
<form action=”search.php” method=”post”>
<?php
$select_query= “Select distinct category from books”;
$select_query_run = mysql_query($select_query);
$select_query_array= mysql_fetch_array($select_query_run) ;
$cat = $select_query_array[‘category’];
echo “<select name=’name’>”;
while ($select_query_array= mysql_fetch_array($select_query_run) )
{// dynamic drop list

echo “<option value='”.htmlspecialchars($select_query_array[“category”]).”‘ >”.htmlspecialchars($select_query_array[“category”]).”</option>”;
}

echo “</select>”;
?>
<input type=”text” name=”searchvalue” size=”5″ id=”searchfield” title=”searchfield” onFocus=”clearText(this)”/>
<input type=”submit” name=”submit” value=”” alt=”Search” id=”searchbutton” title=”Search” />
</form>
<?php
$button = $_POST[‘submit’];
$search = $_POST[‘searchvalue’];
if(strlen($search)<=1)
{
$name = $_POST[‘name’];
//echo $a;
echo “Search term too short”;
}
else{
echo “You searched for $search <hr size=’1′></br>”;
$search_exploded = explode (” “, $search);
$x = “”;
$construct = “”;
foreach($search_exploded as $search_each)
{
$x++;
if($x==1)
$construct .=”title LIKE ‘%$search_each%'”;
else
$construct .=”AND title LIKE ‘%$search_each%'”;

}
$construct =”SELECT * FROM books WHERE $construct and category = ‘$name'”;// I tried this but not working

$run = mysql_query($construct);
$foundnum = mysql_num_rows($run);
if ($foundnum==0)
echo “Sorry, there are no matching result for $search.</br></br>1.”;
else
{
echo “$foundnum results found !<p>”;
}
?>
[/code]

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@LandslydeFeb 23.2015 — It would help if you posted PHP code in the provided PHP tags, not the code tags. PHP tags will force your copy/pasted PHP code to stay indented and so forth. Anyway, since it's kind of hard to read, all I found that might help is this. You have a line of MySQL code:

[code=php]
$construct ="SELECT * FROM books WHERE $construct and category = '$name'";// I tried this but not working
[/code]


I have absolutely no idea why you're trying to evaluate $construct which you set as a SELECT statement. Not to mention that you have $construct set here too:

[code=php]
$construct .="title LIKE '%$search_each%'";
else
$construct .="AND title LIKE '%$search_each%'";
[/code]


Maybe this is what you're trying to do:

[code=php]
$construct .="title LIKE '%$search_each%'";
else
$construct .="title LIKE '%$search_each%'";

$stmt ="SELECT * FROM books WHERE category = '$name' AND '$construct'";
[/code]


Maybe this will help you. I use PHP PDO prepared statements, which makes it easier and more secure. But again, since you didn't use php tags with your code, your stuff is hard to read. Good luck.
Copy linkTweet thisAlerts:
@subramanyemmauthorFeb 24.2015 — Thank you bro thank you very much for your reply, it is working.. It would help if you posted PHP code in the provided PHP tags, not the code tags. PHP tags will force your copy/pasted PHP code to stay indented and so forth. Anyway, since it's kind of hard to read, all I found that might help is this. You have a line of MySQL code:

[code=php]
$construct ="SELECT * FROM books WHERE $construct and category = '$name'";// I tried this but not working
[/code]


I have absolutely no idea why you're trying to evaluate $construct which you set as a SELECT statement. Not to mention that you have $construct set here too:

[code=php]
$construct .="title LIKE '%$search_each%'";
else
$construct .="AND title LIKE '%$search_each%'";
[/code]


Maybe this is what you're trying to do:

[code=php]
$construct .="title LIKE '%$search_each%'";
else
$construct .="title LIKE '%$search_each%'";

$stmt ="SELECT * FROM books WHERE category = '$name' AND '$construct'";
[/code]


Maybe this will help you. I use PHP PDO prepared statements, which makes it easier and more secure. But again, since you didn't use php tags with your code, your stuff is hard to read. Good luck.[/QUOTE]
Copy linkTweet thisAlerts:
@subramanyemmauthorFeb 24.2015 — I changed your query to more simply form, to this

$construct = "SELECT * FROM books WHERE category = '$name' AND (title LIKE '%$search%' or author LIKE '%$search%')";It would help if you posted PHP code in the provided PHP tags, not the code tags. PHP tags will force your copy/pasted PHP code to stay indented and so forth. Anyway, since it's kind of hard to read, all I found that might help is this. You have a line of MySQL code:

[code=php]
$construct ="SELECT * FROM books WHERE $construct and category = '$name'";// I tried this but not working
[/code]


I have absolutely no idea why you're trying to evaluate $construct which you set as a SELECT statement. Not to mention that you have $construct set here too:

[code=php]
$construct .="title LIKE '%$search_each%'";
else
$construct .="AND title LIKE '%$search_each%'";
[/code]


Maybe this is what you're trying to do:

[code=php]
$construct .="title LIKE '%$search_each%'";
else
$construct .="title LIKE '%$search_each%'";

$stmt ="SELECT * FROM books WHERE category = '$name' AND '$construct'";
[/code]


Maybe this will help you. I use PHP PDO prepared statements, which makes it easier and more secure. But again, since you didn't use php tags with your code, your stuff is hard to read. Good luck.[/QUOTE]
Copy linkTweet thisAlerts:
@LandslydeFeb 24.2015 — Good deal. Glad you got it working. And don't forget to use the right tags next time ?
×

Success!

Help @subramanyemm spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...