Jump to content

Display Of Unique Data


son

Recommended Posts

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...