合并两个表并将它们的数据显示在一个数组中

Posted

技术标签:

【中文标题】合并两个表并将它们的数据显示在一个数组中【英文标题】:combine two tables and display their data in an array 【发布时间】:2015-12-21 21:31:58 【问题描述】:

我有2个表:product和cart,我想把这2个表组合起来,根据特定条件以数组的形式显示数据,如下:

应显示特定类别下的所有产品,如果 特定用户购买了给定产品中的任何产品 那么它的详细信息也应该显示在该产品的前面

到目前为止我所做的代码是

$catid = $_REQUEST['catid'];
$userid     = $_REQUEST['userid'];

$sql = "select * 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))
             
                $rows['catid'] =  $row['catid'];
                $rows['catname'] =  $row['catname'];
                $rows['productid'] =  $row['productid'];
                $rows['prodname'] =  $row['prodname'];
                $rows['prodimg'] =  $row['prodimg'];
                $row2[]=$rows;
               
    

echo "<pre>";
print_r($row2);
echo "</pre>";

它给出了一个这样的数组

 Array
(
    [0] => Array
        (
            [catid] => 2
            [catname] => C1
            [productid] => 13
            [prodname] => P1
            [prodimg] => 
        )

    [1] => Array
        (
            [catid] => 2
            [catname] => C1
            [productid] => 14
            [prodname] => P1
            [prodimg] => 
        )

    [2] => Array
        (
            [catid] => 2
            [catname] => C1
            [productid] => 15
            [prodname] => P3
            [prodimg] => 
        )

)

但我想要代替上述数组的最终数组是

Array
(
    [0] => Array
        (
            [catid] => 2
            [catname] => C1
            [productid] => 13
            [prodname] => P1
            [prodimg] => 
            [size] => Array
                (
                    [0] => small
                    [1] => medium
                    [2] => large
                    [3] => perpiece
                )
            [cost] => Array
                (
                    [0] => 10
                    [1] => 20
                    [2] => 30
                    [3] => 12
                )
            [purchasedsize] => Array
                (
                    [0] => small
                    [1] => 0
                    [2] => large
                    [3] => 0
                )
            [purchasedquantity] => Array
                (
                    [0] => 2
                    [1] => 0
                    [2] => 1
                    [3] => 0
                )
            [userid] => 1
        )

    [1] => Array
        (
            [catid] => 2
            [catname] => C1
            [productid] => 14
            [prodname] => P1
            [prodimg] => 
            [size] => Array
                (
                    [0] => small
                    [1] => medium
                    [2] => large
                    [3] => 0
                )
            [cost] => Array
                (
                    [0] => 15
                    [1] => 20
                    [2] => 25
                    [3] => 0
                )
            [purchasedsize] => Array
                (
                    [0] => 0
                    [1] => medium
                    [2] => 0
                    [3] => 0
                )
            [purchasedquantity] => Array
                (
                    [0] => 0
                    [1] => 1
                    [2] => 0
                    [3] => 0
                )
            [userid] => 1
        )

    [2] => Array
        (
            [catid] => 2
            [catname] => C1
            [productid] => 15
            [prodname] => P3
            [prodimg] => 
            [size] => Array
                (
                    [0] => 0
                    [1] => medium
                    [2] => 0
                    [3] => perpiece
                )
            [cost] => Array
                (
                    [0] => 0
                    [1] => 20
                    [2] => 0
                    [3] => 18
                )
            [purchasedsize] => Array
                (
                    [0] => 0
                    [1] => 0
                    [2] => 0
                    [3] => 0
                )
            [purchasedquantity] => Array
                (
                    [0] => 0
                    [1] => 0
                    [2] => 0
                    [3] => 0
                )
            [userid] => 0
        )

)

产品表视图(您将看到产品表带有一个productid,每个productid下最多有4个尺寸(不会超过4个))

id  catid  catname  productid  prodsize   cost  prodname  prodimg
1    2       C1        13        small     10    P1
2    2       C1        13        medium    20    P1
3    2       C1        13        large     30    P1
4    2       C1        13        perpiece  12    P1
5    2       C1        14        small     15    P2
6    2       C1        14        medium    20    P2
7    2       C1        14        large     25    P2
8    2       C1        15        perpiece  18    P3
9    2       C1        15        medium    20    P3

购物车表视图

id   catid  catname  userid  productid  prodname  prodsize   quantity  prodcost
1       2      C1       1        13       P1       large        1         30      
2       2      C1       1        13       P1       small        2         10    
3       2      C1       1        14       P2       medium       1         20

谁能帮我得到所需的数组作为结果?

【问题讨论】:

【参考方案1】:

试试这个

 $catid = $_REQUEST['catid'];
 $userid     = $_REQUEST['userid'];

 $sql= "SELECT p.catid, p.catname, p.productid, p.prodimg, 
   GROUP_CONCAT(p.prodsize ORDER BY p.id ASC) as size, 
   GROUP_CONCAT(p.cost ORDER BY p.id ASC) as cost, p.prodname,
   GROUP_CONCAT(c.prodsize,'-',c.quantity) as cart_details, GROUP_CONCAT(DISTINCT(c.userid)) as user_id
   FROM products p
   LEFT JOIN cart c ON(c.productid = p.productid AND c.userid = '$userid' AND p.prodsize = c.prodsize)
   WHERE p.catid ='$catid'
   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]['catid'] =  $row['catid'];
                $rows[$i]['catname'] =  $row['catname'];
                $rows[$i]['productid'] =  $row['productid'];
                $rows[$i]['prodname'] =  $row['prodname'];
                $rows[$i]['prodimg'] =  $row['prodimg'];
                $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'); //Since you displayed this array has 4 values only
                $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++;
               
    

echo "<pre>";
print_r($rows);
    echo "</pre>";

输出数组

Array
(
[0] => Array
    (
        [catid] => 2
        [catname] => c1
        [productid] => 13
        [prodname] => P1
        [prodimg] => 
        [size] => Array
            (
                [0] => small
                [1] => medium
                [2] => large
                [3] => perpiece
            )

        [cost] => Array
            (
                [0] => 10
                [1] => 20
                [2] => 30
                [3] => 12
            )

        [purchasedsize] => Array
            (
                [0] => small
                [1] => 0
                [2] => large
                [3] => 0
            )

        [purchasedquantity] => Array
            (
                [0] => 2
                [1] => 0
                [2] => 1
                [3] => 0
            )

        [userid] => 1
    )

[1] => Array
    (
        [catid] => 2
        [catname] => c1
        [productid] => 14
        [prodname] => P2
        [prodimg] => 
        [size] => Array
            (
                [0] => small
                [1] => medium
                [2] => large
                [3] => 0
            )

        [cost] => Array
            (
                [0] => 15
                [1] => 20
                [2] => 25
                [3] => 0
            )

        [purchasedsize] => Array
            (
                [0] => 0
                [1] => medium
                [2] => 0
                [3] => 0
            )

        [purchasedquantity] => Array
            (
                [0] => 0
                [1] => 1
                [2] => 0
                [3] => 0
            )

        [userid] => 1
    )

[2] => Array
    (
        [catid] => 2
        [catname] => C1
        [productid] => 15
        [prodname] => P3
        [prodimg] => 
        [size] => Array
            (
                [0] => 0
                [1] => medium
                [2] => 0
                [3] => perpiece
            )

        [cost] => Array
            (
                [0] => 0
                [1] => 20
                [2] => 0
                [3] => 18
            )

        [purchasedsize] => Array
            (
                [0] => 0
                [1] => 0
                [2] => 0
                [3] => 0
            )

        [purchasedquantity] => Array
            (
                [0] => 0
                [1] => 0
                [2] => 0
                [3] => 0
            )

        [userid] => 
    )

)

【讨论】:

虽然我得到了一个数组,但其中的值不正确 @kavi:现在数组似乎按你的意愿输出了。检查我修改后的代码 我给出的输出与您发布的相似。 数组的其余部分似乎没问题,但是不同尺寸的成本仍然会出错,例如对于产品 id 13,尺寸成本是 opp,它应该是 10,20,30 和你的 o/p 显示 30,20,10。如果您能检查一下,将不胜感激 @kavi:感谢您指出这一点。我已将 GROUP_CONCAT(p.cost) 作为成本更改为 GROUP_CONCAT(p.cost ORDER BY p.id ASC) 作为成本。现在解决了

以上是关于合并两个表并将它们的数据显示在一个数组中的主要内容,如果未能解决你的问题,请参考以下文章

PDO 显示表数组

使用 react js 合并并显示表中的对象数据数组

如何创建点,显示它们并将它们存储在某种数组中

合并或合并索引不相等的两个或三个(多于三个)数组

SQL怎样合并显示两个没有关联的表

从数组中提取 CSV 值并将它们单独显示为列表