无法正确创建阵列

时间:2022-03-28 02:50:15

My question is a bit long, so plz bear it

我的问题有点长,所以请耐心等待

I have 2 tables productsize and cart. I have joined the 2 tables and created one single array, but there is a part of this array where i am not getting data properly. The productsize table has products that have 4 sizes (small, medium, large, perpiece).

我有2个表产品和购物车。我加入了2个表并创建了一个单个数组,但是这个数组的一部分我没有正确获取数据。 productsize表有4种尺寸(小,中,大,perpiece)的产品。

The array that i am getting from the 2 tables is like the following array, here the main part is that in the 4 arrays:- size, cost, purchasedsize, purchasedquantity 0 index value should be reserved for small, 1 for medium 2 for large and 3 for perpiece.

我从2个表中得到的数组就像下面的数组一样,这里的主要部分是4个数组: - size,cost,purchasesize,purchasequantity 0索引值应保留为small,1为medium 2为large和3为perpiece。

Array
(
    [0] => Array
        (
            [productid] => 1
            [size] => Array
                (
                    [0] => small //reserved for small
                    [1] => medium //reserved for medium
                    [2] => large //reserved for large
                    [3] => 0 //reserved for perpiece
                )
            [cost] => Array
                (
                    [0] => 10 //reserved for small
                    [1] => 20 //reserved for medium
                    [2] => 30 //reserved for large
                    [3] => 0 //reserved for perpiece
                )
            [purchasedsize] => Array
                (
                    [0] => small //reserved for small
                    [1] => 0 //reserved for medium
                    [2] => large //reserved for large
                    [3] => 0 //reserved for perpiece
                )
            [purchasedquantity] => Array
                (
                    [0] => 2 //reserved for small
                    [1] => 0 //reserved for medium
                    [2] => 3 //reserved for large
                    [3] => 0 //reserved for perpiece
                )
        )
)

Issue is that this positioning is working fine for array size and cost, but in purchasedsize, purchasedquantity if the cart has a product with size perpiece, then it shifts to index value 0

问题是这种定位对于阵列尺寸和成本来说工作得很好,但是如果购物车有购买数量,如果购物车的尺寸为尺寸,那么它会转移到指数值0

Array becomes like the following one, but under purchasedsize and purchased quantity the perpiece should be at index value 3

数组变得如下所示,但在购买量和购买数量下,每件应该在索引值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
                )
        )
)

productsize table

产品化表

id  productid   prodsize   cost
1     1          small      10
2     1          medium     20
3     1          large      30
4     2          perpiece   40

cart table

购物车桌

id  productid  prodsize  prodcost  quantity
1     1         small      10        2
2     1         large      30        3
3     2         perpiece   40        1

the code that i am using is

我正在使用的代码是

$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++; 
            }   
    }

1 个解决方案

#1


0  

Removed the user details as I dont have and tried the data with lower case. Please have a look

删除了我没有的用户详细信息,并尝试使用小写的数据。请看一看

$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 FROM productsize p 
    LEFT JOIN cart c ON(c.productid = p.productid AND p.prodsize = c.prodsize) 
    GROUP BY p.productid ORDER BY p.productid ASC";
$result = mysql_query($sql);
    if (mysql_num_rows($result) > 0) {
        $i = 0;
        while($row = mysql_fetch_assoc($result))
            {
                $purchasedData  = array();                
                $purchasedsize  =  $purchasedquantity =  $final_size =  $final_cost = array_fill(0, 4, '0');
                $size           = explode(',', $row['size']);
                $cost           = explode(',', $row['cost']);                
                $cart_details   = explode(',', $row['cart_details']);
                foreach($cart_details as $cart) {
                    if($cart != '') {
                        $details = explode('-', $cart);
                        $key     = array_search($details[0], $size);
                        $purchasedData[$details[0]] = $details[1];
                    }
                }
                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];
                    if ($purchasedData[$sizecol]) {
                        $purchasedsize[$array_key] = $sizecol;
                        $purchasedquantity[$array_key] = $purchasedData[$sizecol];
                    }
                }
                $rows[$i]['productid']          =  $row['productid'];
                $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++; 
            }   
    }
    echo "<pre>";
    print_r($rows);
    echo "</pre>";

#1


0  

Removed the user details as I dont have and tried the data with lower case. Please have a look

删除了我没有的用户详细信息,并尝试使用小写的数据。请看一看

$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 FROM productsize p 
    LEFT JOIN cart c ON(c.productid = p.productid AND p.prodsize = c.prodsize) 
    GROUP BY p.productid ORDER BY p.productid ASC";
$result = mysql_query($sql);
    if (mysql_num_rows($result) > 0) {
        $i = 0;
        while($row = mysql_fetch_assoc($result))
            {
                $purchasedData  = array();                
                $purchasedsize  =  $purchasedquantity =  $final_size =  $final_cost = array_fill(0, 4, '0');
                $size           = explode(',', $row['size']);
                $cost           = explode(',', $row['cost']);                
                $cart_details   = explode(',', $row['cart_details']);
                foreach($cart_details as $cart) {
                    if($cart != '') {
                        $details = explode('-', $cart);
                        $key     = array_search($details[0], $size);
                        $purchasedData[$details[0]] = $details[1];
                    }
                }
                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];
                    if ($purchasedData[$sizecol]) {
                        $purchasedsize[$array_key] = $sizecol;
                        $purchasedquantity[$array_key] = $purchasedData[$sizecol];
                    }
                }
                $rows[$i]['productid']          =  $row['productid'];
                $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++; 
            }   
    }
    echo "<pre>";
    print_r($rows);
    echo "</pre>";