son Posted December 17, 2009 Share Posted December 17, 2009 I have a product overview page that displays items from all sub-categories of relevant selected parent category. My issue comes that product can be assigned to more than one main-category and my code displays the same item several time if assigned several times. The code is: $subQuery = "SELECT category_id FROM categories WHERE parent_id = $cid";$subResult = mysqli_query ($dbc, $subQuery); if (mysqli_num_rows($subResult) > 0) { while ($row = mysqli_fetch_array ($subResult, MYSQLI_ASSOC)) { $sub = $row['category_id']; $productQuery = "SELECT product_id, product, price, img1 FROM products WHERE parent_id = 0 AND product_id IN (SELECT product_id FROM productCat WHERE category_id = $sub)"; $productResult = mysqli_query ($dbc, $productQuery); if (mysqli_num_rows($productResult) > 0) { while ($row = mysqli_fetch_array ($productResult, MYSQLI_ASSOC)) { retrieve and display product data } } } } What can I do to check if a product is already displayed and if yes, not to display it again? As it is run with two queries I cannot use DINSTINCT..Son Link to comment Share on other sites More sharing options...
justsomeguy Posted December 17, 2009 Share Posted December 17, 2009 You can put all of the IDs or whatever into an array as you print them, and use in_array to check if a certain ID is already in the array. Link to comment Share on other sites More sharing options...
ProblemHelpPlease Posted December 17, 2009 Share Posted December 17, 2009 If you are adding to an array then you could run a array_unique to remove any duplicate IDs, then use the array to display the results. Link to comment Share on other sites More sharing options...
son Posted December 22, 2009 Author Share Posted December 22, 2009 If you are adding to an array then you could run a array_unique to remove any duplicate IDs, then use the array to display the results. $array = array();$subQuery = "SELECT category_id FROM categories WHERE parent_id = $cid";$subResult = mysqli_query ($dbc, $subQuery);if (mysqli_num_rows($subResult) > 0){while ($row = mysqli_fetch_array ($subResult, MYSQLI_ASSOC)){$sub = $row['category_id'];$productQuery = "SELECT product_id, product, price, img1 FROM products WHERE parent_id = 0 AND product_id IN (SELECT product_id FROM productCat WHERE category_id = $sub)";$productResult = mysqli_query ($dbc, $productQuery);if (mysqli_num_rows($productResult) > 0){while ($row = mysqli_fetch_array ($productResult, MYSQLI_ASSOC)){$id = $row['product_id'];$pname = $row['product'];if( !in_array( $id, $array ) ){$array[] = $id;display product data }}}}} Seems to work well.Many thanks,Son Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.