golfer Posted December 10, 2010 Share Posted December 10, 2010 I currently have a search page on my site that prints the products but it prints the products more than once if its in more than one category I have tried getting distinct item in my SQL. But this doesnt work so im trying an if statement that if there is more than one specific result then to just print this once. I was wondering if anyone had any ideas of how to do this using an if statement I just dont know how to go about just printing the result just once if its greater than 1. The code is below to make it clearer. $searchterm = $_POST['searchterm']; trim ($searchterm); /*check if search term was entered*/ if (!$searchterm){ echo 'Please enter a search term.'; echo $searchterm; } /*add slashes to search term*/ if (!get_magic_quotes_gpc()) { $searchterm = addslashes($searchterm); } /*query the database*/ $query = "SELECT * from (products LEFT JOIN categories_products_link ON products.prod_id = categories_products_link.prod_id) LEFT JOIN categories ON categories_products_link.cat_id = categories.cat_id WHERE prod_title LIKE '%" . $searchterm . "%' ORDER BY cat_title, prod_title"; $result = mysql_query($query); /*number of rows found*/ $num_results = mysql_num_rows($result); echo '<p><h1>Search Results: '.$num_results.'</h1></p><br />'; /*loops through results*/ for ($i=0; $i <$num_results; $i++) { $num_found = $i + 1; $row = mysql_fetch_assoc($result); echo "$num_found. "?><a href="store-<?php echo $row['cat_id'];?>-<?php echo $row['prod_id']; ?>/<?php echo seo_makeSafeURI($row['prod_title']); ?>.html"><strong><?php echo $row['prod_title']; ?></strong></a> <br /> Quote Link to comment https://forums.phpfreaks.com/topic/221222-if-1-then-print-1-variable/ Share on other sites More sharing options...
JakeTheSnake3.0 Posted December 10, 2010 Share Posted December 10, 2010 Try and use another SELECT query wrapped outside of that one which uses DISTINCT on just the id of the product. Quote Link to comment https://forums.phpfreaks.com/topic/221222-if-1-then-print-1-variable/#findComment-1145434 Share on other sites More sharing options...
QuickOldCar Posted December 11, 2010 Share Posted December 11, 2010 Use LIMIT in your select query. 0 is starting from where, 10 is the max limit from the starting point displays the first 10 results from the database SELECT * FROM `your_table` LIMIT 0, 10 displays records 6, 7, 8, 9,,10 SELECT * FROM `your_table` LIMIT 5, 5 $searchterm = $_POST['searchterm']; trim ($searchterm); /*check if search term was entered*/ if (!$searchterm){ echo 'Please enter a search term.'; echo $searchterm; } /*add slashes to search term*/ if (!get_magic_quotes_gpc()) { $searchterm = addslashes($searchterm); } /*query the database*/ $query = "SELECT * from (products LEFT JOIN categories_products_link ON products.prod_id = categories_products_link.prod_id) LEFT JOIN categories ON categories_products_link.cat_id = categories.cat_id WHERE prod_title LIKE '%" . $searchterm . "%' ORDER BY cat_title, prod_title LIMIT 0,1"; $result = mysql_query($query); /*number of rows found*/ $num_results = mysql_num_rows($result); echo '<p><h1>Search Results: '.$num_results.'</h1></p><br />'; /*loops through results*/ for ($i=0; $i <$num_results; $i++) { $num_found = $i + 1; $row = mysql_fetch_assoc($result); echo "$num_found. "?><a href="store-<?php echo $row['cat_id'];?>-<?php echo $row['prod_id']; ?>/<?php echo seo_makeSafeURI($row['prod_title']); ?>.html"><strong><?php echo $row['prod_title']; ?></strong></a> <br /> Quote Link to comment https://forums.phpfreaks.com/topic/221222-if-1-then-print-1-variable/#findComment-1145603 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.