My question is a little long, so plz carry it
I have 2 tables productsizeand cart. I joined the two tables and created one single array, but there is a part of this array where I do not get the data correctly. In the product table there are products whose size is 4 (small, medium, large, size).
The array that I get from the two tables looks like the following array, the main part here is that in 4 arrays: size, cost, acquired, purchased quantum 0 index value should be reserved for small, 1 for medium 2 for large and 3 for perforation.
Array
(
[0] => Array
(
[productid] => 1
[size] => Array
(
[0] => small
[1] => medium
[2] => large
[3] => 0
)
[cost] => Array
(
[0] => 10
[1] => 20
[2] => 30
[3] => 0
)
[purchasedsize] => Array
(
[0] => small
[1] => 0
[2] => large
[3] => 0
)
[purchasedquantity] => Array
(
[0] => 2
[1] => 0
[2] => 3
[3] => 0
)
)
)
, , buysize, purchasequantity, , 0
, 3
Array
(
[0] => Array
(
[productid] => 2
[size] => Array
(
[0] => 0
[1] => 0
[2] => 0
[3] => perpiece
)
[cost] => Array
(
[0] => 0
[1] => 0
[2] => 0
[3] => 40
)
[purchasedsize] => Array
(
[0] => perpiece
[1] => 0
[2] => 0
[3] => 0
)
[purchasedquantity] => Array
(
[0] => 1
[1] => 0
[2] => 0
[3] => 0
)
)
)
id productid prodsize cost
1 1 small 10
2 1 medium 20
3 1 large 30
4 2 perpiece 40
id productid prodsize prodcost quantity
1 1 small 10 2
2 1 large 30 3
3 2 perpiece 40 1
, ,
$sql= "SELECT p.productid, GROUP_CONCAT(p.prodsize ORDER BY p.id ASC) as size, GROUP_CONCAT(p.cost ORDER BY p.id ASC) as cost, GROUP_CONCAT(c.prodsize,'-',c.quantity) as cart_details, GROUP_CONCAT(DISTINCT(c.userid)) as user_id FROM productsize p LEFT JOIN cart c ON(c.productid = p.productid AND p.prodsize = c.prodsize) GROUP BY p.productid ORDER BY user_id DESC, p.productid ASC";
$result = mysql_query($sql);
if (mysql_num_rows($result) > 0)
{
$i = 0;
while($row = mysql_fetch_assoc($result))
{
$rows[$i]['productid'] = $row['productid'];
$final_size = array_fill(0, 4, '0');
$final_cost = array_fill(0, 4, '0');
$size = explode(',', $row['size']);
$cost = explode(',', $row['cost']);
foreach($size as $k=>$sizecol) {
switch($sizecol) {
case 'Small':
$array_key = '0';
break;
case 'Medium':
$array_key = '1';
break;
case 'Large':
$array_key = '2';
break;
case 'Perpiece':
$array_key = '3';
break;
}
$final_size[$array_key] = $sizecol;
$final_cost[$array_key] = $cost[$k];
}
$cart_details = explode(',', $row['cart_details']);
$purchasedsize = array_fill(0, 4, '0');
$purchasedquantity = array_fill(0, 4, '0');
foreach($cart_details as $cart) {
if($cart != '') {
$details = explode('-', $cart);
$key = array_search($details[0], $size);
$purchasedsize[$key] = $details[0];
$purchasedquantity[$key] = $details[1];
}
}
$rows[$i]['size'] = $final_size;
$rows[$i]['cost'] = $final_cost;
$rows[$i]['purchasedsize'] = $purchasedsize;
$rows[$i]['purchasedquantity'] = $purchasedquantity;
$rows[$i]['userid'] = $row['user_id'];
$i++;
}
}