Mysql
 sql >> Base de Dados >  >> RDS >> Mysql

combinar duas tabelas e exibir seus dados em uma matriz


Tente isso
 $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>";
";
A matriz de saída
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] => 
    )

)