gotornot Posted May 16, 2012 Share Posted May 16, 2012 Hi I am building a product search engine using MySql and PHP (Jquery / AJAX). As i am getting the search results i wondered is it possible to count the instances of a category? For example: If i search for "Dell Laptops" i get 400+ results back because it has lots of laptops and accessories. What i am aiming for it to do is while looping through look at the categories and if its the first instanc eof it i want it to begin a count. until it has finished looping and then i can display it. Results: 436 [ Laptops(127) - Accesories (244) - Components(65) ] Im not even sure where to start here so any advice will be helpful Here is the code im using to generate the search: $id = str_replace(" ", "%", "$id"); $q = "SELECT * FROM products WHERE prod_name LIKE '%$id%' order by fee asc LIMIT 20"; Quote Link to comment https://forums.phpfreaks.com/topic/262635-counting-instances-and-then-displaying-a-result/ Share on other sites More sharing options...
smoseley Posted May 16, 2012 Share Posted May 16, 2012 2nd query: SELECT cat_name, COUNT(*) FROM products WHERE prod_name LIKE '%$id%' GROUP BY cat_name; Quote Link to comment https://forums.phpfreaks.com/topic/262635-counting-instances-and-then-displaying-a-result/#findComment-1346086 Share on other sites More sharing options...
gotornot Posted May 16, 2012 Author Share Posted May 16, 2012 how would i add that in? Or am i being blonde? Quote Link to comment https://forums.phpfreaks.com/topic/262635-counting-instances-and-then-displaying-a-result/#findComment-1346090 Share on other sites More sharing options...
scootstah Posted May 16, 2012 Share Posted May 16, 2012 You probably don't want to work with 400+ results all at once. If you have a few thousand users pulling hundreds of results at the same time you're going to use a lot of unnecessary memory. You'd be better off with pagination and only displaying 20-30 at a time. Therefore you'll probably need some sub-queries to count the categories that correspond to the keyword. I'm having a hard time coming up with a good example, though. Quote Link to comment https://forums.phpfreaks.com/topic/262635-counting-instances-and-then-displaying-a-result/#findComment-1346092 Share on other sites More sharing options...
gotornot Posted May 16, 2012 Author Share Posted May 16, 2012 I do limit the result to 20. Just racking my brain trying to think how i am going to show the result preferably without having multiple duplicate results displayed.... Quote Link to comment https://forums.phpfreaks.com/topic/262635-counting-instances-and-then-displaying-a-result/#findComment-1346098 Share on other sites More sharing options...
smoseley Posted May 16, 2012 Share Posted May 16, 2012 how would i add that in? Or am i being blonde? either execute them as 2 separate queries, or use mysqli_multi_query and separate the queries by a semicolon. Quote Link to comment https://forums.phpfreaks.com/topic/262635-counting-instances-and-then-displaying-a-result/#findComment-1346110 Share on other sites More sharing options...
gotornot Posted May 16, 2012 Author Share Posted May 16, 2012 I looked at doing that but their are many cats and we don't need them all so its only needed to be counted if we have product in that area. Quote Link to comment https://forums.phpfreaks.com/topic/262635-counting-instances-and-then-displaying-a-result/#findComment-1346112 Share on other sites More sharing options...
smoseley Posted May 16, 2012 Share Posted May 16, 2012 I looked at doing that but their are many cats and we don't need them all so its only needed to be counted if we have product in that area. The query i gave you would only return results for cats with products in them. Quote Link to comment https://forums.phpfreaks.com/topic/262635-counting-instances-and-then-displaying-a-result/#findComment-1346119 Share on other sites More sharing options...
gotornot Posted May 16, 2012 Author Share Posted May 16, 2012 All sorted thanks ) Quote Link to comment https://forums.phpfreaks.com/topic/262635-counting-instances-and-then-displaying-a-result/#findComment-1346129 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.