SELECT Product_type FROM product INNER JOIN product Product_type.typeid = product.pid
I have this sql query result list. This is exactly what i need.
--------------------------
Product_type | Product
--------------------------
ProductType 1 | Poduct A
ProductType 1 | Poduct B
ProductType 1 | Poduct C
ProductType 1 | Poduct D
ProductType 2 | Poduct E
ProductType 2 | Poduct F
ProductType 2 | Poduct G
--------------------------
Now, my question is how to get list of product based on product type. Is there any way to get such result with sql query?
I need frontend output as bellow:
ProductType 1
| Product A
| Product B
| Product C
| Product D
ProductType 2
| Product E
| Product F
| Product G
Or how can i store result in multi-dimational array based on product_type. Can anyone help.
Big Thanks an advance!
I got a solution from our discussion, it may help you.
Your query would be like below :
SELECT product_type.product_type, GROUP_CONCAT(products.product) AS pros FROM product_type LEFT JOIN products ON product_type.id = products.product_type_id GROUP BY product_type.id
This will give you output as below array:
$result_arr = array(
array( 'product_type'=>'ProductType 1',
'pros'=>'product A, product B, product C'),
array( 'product_type'=>'ProductType 2',
'pros'=>'product D, product E, product F')
);
Then you can manipulate that output as below :
foreach($result_arr as $key=>$val){
echo "<b>".$val['product_type']."</b><br/>";
$products = explode(',',$val['pros']);
foreach($products as $product){
echo $product."</br>";
}
}
Try using this
SELECT Product_type FROM product INNER JOIN product Product_type.typeid = product.pid GROUP BY Product_type;
Let me know if it works or not.
you fetch the results of this query in $row
and then,
$product_type1 = array();
$product_type2 = array();
while ($row) {
if($row['Product_type'] == '1') {
array_push($product_type1, $row['Product']);
}
else if($row['Product_type'] == '2') {
array_push($product_type2, $row['Product']);
}
}
and now use this arrays to display the values in front end.
Fetch the result and loop through it to get the desired result
$sql = "SELECT Product_type FROM product INNER JOIN product Product_type.typeid = product.pid";
$result = $conn->query($sql);
while ($row = $result->fetch_assoc()) {
$newarray[$row['product_type']][] = $row['product'];
}
print_r($newarray);
Now, my question is how to get list of product based on product type. Is there any way to get such result with sql query? Or how can i store result in multi-dimational array based on product_type. Can anyone help.
$mysqli = new mysqli("localhost", "my_user", "my_password", "mydb");
$query = "put your query string here";
$output = array();
// From your sql_query result list
if ($result = $mysqli->query($query))
{
/* fetch object array */
while ($row = $result->fetch_assoc())
{
if(isset( $output[ $row['product_type'] ] ))
{
$output[ $row['product_type'] ][] = $row['product'];
continue;
}
$output[ $row['product_type'] ] = array( $row['product'] );
}
print_r( $output ); // Here is your multi-dimational array
}