(This post might get a big long but please bear with it and help me find the solution)
I have a product table that have various parameter, main parameter is its size and cost. Each products size is divided into 3 parts i.e small, medium and large and its cost also gets furtur divided into 3 parts according to the size of the product. For this purpose i created another table productsize that carries size and cost of that size for every product.
Small view of prodsize table is
I have another table cart, it holds the value of those product that a user has ordered
Small view of cart table is
Now I wish to combine these two tables and have an array that displays the size of each size and the quantity a user has ordered for every size of a particular product. for this purpose I wrote the code below
$sql = "SELECT catname,GROUP_CONCAT(prodsize) as sizes,GROUP_CONCAT(cost) as price,prodname,prodimg,catid,productid FROM productsize where catid='".$catid."' group by productid";
$result = mysqli_query($con, $sql);
if (mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_assoc($result))
{
$pid=$row['productid'];
$rows['catname'] = $row['catname'];
$rows['sizes'] = $row['sizes'];
$rows['price'] = $row['price'];
$rows['prodname'] = $row['prodname'];
$rows['prodimg'] = $row['prodimg'];
$rows['catid'] = $row['catid'];
$rows['productid'] = $row['productid'];
$user_sql = "SELECT group_concat(prodsize) as purchased_size,group_concat(quantity) as purchased_quantity,catid,userid FROM `cart` where userid='$userid' and catid='$catid' and productid='$pid'";
$user_result = mysqli_query($con, $user_sql);
if (mysqli_num_rows($user_result) > 0)
{
while($user_row = mysqli_fetch_assoc($user_result))
{
$rows['purchased_size'] = $user_row['purchased_size'];
$rows['purchased_quantity'] = $user_row['purchased_quantity'];
$rows['userid'] = $user_row['userid'];
}
}
$post_rows[]=$rows;
}
echo "<pre>";
print_r($post_rows);
echo "</pre>";
}
Array that I got through print_r($post_rows) is
Array
(
[0] => Array
(
[catname] => Main Course
[sizes] => small,medium,large
[price] => 130,170,200
[prodname] => pies
[prodimg] =>
[catid] => 2
[productid] => 13
[purchased_size] => small,large
[purchased_quantity] => 1,1
[userid] => 1
)
[1] => Array
(
[catname] => Main Course
[sizes] => small,medium,large
[price] => 50,90,110
[prodname] => Squash
[prodimg] =>
[catid] => 2
[productid] => 14
[purchased_size] => medium
[purchased_quantity] => 1
[userid] => 1
)
[2] => Array
(
[catname] => Main Course
[sizes] => small,medium
[price] => 70,110
[prodname] => corns
[prodimg] =>
[catid] => 2
[productid] => 15
[purchased_size] =>
[purchased_quantity] =>
[userid] =>
)
)
Now the change that i want is in [purchased_size] and [purchased_quantity] . I want that
if the user hasn't purchased anything for a particular product then the values in [purchased_size] and [purchased_quantity] should go 0,0,0 and if the user purchases one size of product as in the case of Squash, the user chooses medium then other values in [purchased_size] and [purchased_quantity] shouuld go 0,0 and if he has ordered 2 sizes then 3rd value in [purchased_size] and [purchased_quantity] should go 0
So the new array should look like the array given below (IMP point, the order for size and cost should be fixed i.e small, medium, large)
Array
(
[0] => Array
(
[catname] => Main Course
[sizes] => small,medium,large
[price] => 130,170,200
[prodname] => pies
[prodimg] =>
[catid] => 2
[productid] => 13
[purchased_size] => small,0,large
[purchased_quantity] => 1,0,1
[userid] => 1
)
[1] => Array
(
[catname] => Main Course
[sizes] => small,medium,large
[price] => 50,90,110
[prodname] => Squash
[prodimg] =>
[catid] => 2
[productid] => 14
[purchased_size] => 0,medium,0
[purchased_quantity] => 0,1,0
[userid] => 1
)
[2] => Array
(
[catname] => Main Course
[sizes] => small,medium
[price] => 70,110
[prodname] => corns
[prodimg] =>
[catid] => 2
[productid] => 15
[purchased_size] => 0,0,0
[purchased_quantity] => 0,0,0
[userid] =>
)
)

