/    Sign up×
Community /Pin to ProfileBookmark

Search mysql database and filter results

Hi,
I’ve been buckling down and teaching myself PHP for about a week now and am attempting to make a search script.

Rather then try to explain myself and take the chance of losing, here is main idea I have come up with so far

[code=php]if ($search) {
$srch = ‘%’.$search.’%’;
//select db code here
$query = “SELECT * FROM Product, Picture WHERE Product.Title LIKE ‘$srch’ AND Product.ProductID = Picture.EntityID ORDER BY LastModifiedDateTime ASC”;
$num_row_query = //num rows
$row_query = //fetch assoc
} do {
//exit php and do output xhtml
} while ();
}[/code]

This part works just fine, problem is that it returns too many results. I need to be able to turn products on and off. Because of this I have another table which says whether or not the Product is Enabled. So, if the search does return results, before output it needs to check that CategoryProduct.Enabled = 1.

I was able to do this successfully when a user chooses the category from a menu with this code(it returns all the products in the category chosen, but first checks CategoryProduct to see if the Product is enabled):

[code=php]
<?php
//is product Enabled
//select db
$query_productEnabled = sprintf(“SELECT * FROM CategoryProduct WHERE CategoryID = %s AND ENABLED = 1 ORDER BY CategoryProduct.LastModifiedDateTime ASC”, $colname_category);
$productEnabled = mysql_query($query_productEnabled);
$row_productEnabled = mysql_fetch_assoc($productEnabled);
$numRow_productEnabled = mysql_num_rows($productEnabled);

?>
<?php if ($numRow_productEnabled >= 1) {
do {
$enabled = $row_productEnabled [‘ProductID’];
$query_product = “SELECT * FROM Product, Picture WHERE Product.ProductID = ‘$enabled’ AND Product.ProductID = Picture.EntityID”;
$product = mysql_query($query_product);
$row_product = mysql_fetch_assoc($product);
$numRow_product = mysql_num_rows($product);
}
//html
} while ($row_productEnabled = mysql_fetch_assoc($productEnabled));
}
else
{
// html
}
?>
[/code]

I feel like a j-a for being able to get it to work here, but not as a result of the search.

I tried using the same concept, but have been obviously unsuccessful.

I hope I have explained my problem thoroughly enough.

Thanks
Chris

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@The_Little_GuyApr 04.2006 — Here is a pretty good tutorial on how to make a search script

http://www.phpfreaks.com/tutorials/129/0.php
×

Success!

Help @chrisrock79 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 5.14,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...