Php 从数据库中获取值并在数组中显示结果

Php 从数据库中获取值并在数组中显示结果,php,mysql,sql,arrays,mysqli,Php,Mysql,Sql,Arrays,Mysqli,我有一张像这样的购物车桌子 id catid catname userid productid prodimg prodname prodsize prodcost quantity datee timee totalamount 1 1 CN1 1 1 ABC P1 small 10 1 3/10/2015 10:00a

我有一张像这样的购物车桌子

id  catid  catname  userid  productid  prodimg  prodname   prodsize   prodcost  quantity    datee       timee     totalamount
1    1      CN1      1        1         ABC       P1        small       10        1        3/10/2015    10:00am      10       
2    1      CN1      1        1         ABC       P1        medium      20        1        4/10/2015    10:00am      20  
3    1      CN1      1        1         ABC       P1        large       30        1        3/10/2015    10:00am      30  
4    1      CN1      1        1         ABC       P1        perpiece    5         1        3/10/2015    10:00am      5   
5    1      CN1      1        2         CDF       P2        small       6         1        3/10/2015    10:00am      6       
6    1      CN1      1        2         CDF       P2        large       14        1        4/10/2015    10:00am      14  
7    1      CN1      2        1         ABC       P1        small       10        2        3/10/2015    10:00am      20        
我希望以一种特定的方式在数组中显示它的数据(根据userid),我想要的结果数组如下所示

Array
(
    [0] => Array
        (
            [catid] => 1
            [catname] => CN1
            [userid] => 1
            [productid] => 1
            [prodimg] => ABC
            [prodname] => P1
            [prodsize] => Array
                (
                    [0] => small
                    [1] => medium
                    [2] => large
                    [3] => perpiece
                )
            [prodcost] => Array
                (
                    [0] => 10
                    [1] => 20
                    [2] => 30
                    [3] => 5
                )   
            [quantity] => Array
                (
                    [0] => 1
                    [1] => 1
                    [2] => 1
                    [3] => 1
                )
            [datee] => Array
                (
                    [0] => 3/10/2015
                    [1] => 4/10/2015
                    [2] => 3/10/2015
                    [3] => 3/10/2015
                )
            [timee] => Array
                (
                    [0] => 10:00am
                    [1] => 10:00am
                    [2] => 10:00am
                    [3] => 10:00am
                )   
            [totalamount] => Array
                (
                    [0] => 10
                    [1] => 20
                    [2] => 30
                    [3] => 5
                )
        )

    [1] => Array
        (
            [catid] => 1
            [catname] => CN1
            [userid] => 1
            [productid] => 2
            [prodimg] => CDF
            [prodname] => P2
            [prodsize] => Array
                (
                    [0] => small
                    [1] => 0
                    [2] => large
                    [3] => 0
                )
            [prodcost] => Array
                (
                    [0] => 6
                    [1] => 0
                    [2] => 14
                    [3] => 0
                )   
            [quantity] => Array
                (
                    [0] => 1
                    [1] => 0
                    [2] => 1
                    [3] => 0
                )
            [datee] => Array
                (
                    [0] => 3/10/2015
                    [1] => 0
                    [2] => 4/10/2015
                    [3] => 0
                )
            [timee] => Array
                (
                    [0] => 10:00am
                    [1] => 0
                    [2] => 10:00am
                    [3] => 0
                )   
            [totalamount] => Array
                (
                    [0] => 6
                    [1] => 0
                    [2] => 14
                    [3] => 0
                )
        )
)    
我使用的代码是

$userid = $_REQUEST['userid'];

$sql= "SELECT catid, catname, productid, prodimg, GROUP_CONCAT(prodsize ORDER BY id ASC) as prodsize,  GROUP_CONCAT(prodcost ORDER BY id ASC) as prodcost, GROUP_CONCAT(quantity ORDER BY id ASC) as quantity, GROUP_CONCAT(datee ORDER BY id ASC) as datee,  GROUP_CONCAT(timee ORDER BY id ASC) as timee, 
      GROUP_CONCAT(totalamount ORDER BY id ASC) as totalamount   from cart WHERE userid ='$userid'";

        $result = mysqli_query($con, $sql);
        if (mysqli_num_rows($result) > 0) 
            {
                while($result = mysqli_fetch_assoc($result)) 
                    {
                        echo "<pre>";
                        print_r($result);
                        echo "</pre>";
                    }
            }

谁能告诉我如何以正确的方式获取阵列。该数组的另一个重要方面是,prodsize中的数组应该是固定的,即如果prod大小较小,则它将存储在[0]、[1]中、[2]中的较大值和[3]中的每件中,如果其中一个值不存在,则它应该是0,并且prodcost,quantity Date和timee也将遵循相同的方法来获取数组中sql select查询的所有结果,只需使用下面的fetch all函数

$formatedResult = [];
$i = 0;
while($result = mysqli_fetch_assoc($result)) 
{
    $formatedResult = $result;
    $formatedResult[$i]['prodsize'] = explode(',', $result[$i]['prodsize']);
    $formatedResult[$i]['prodcost'] = explode(',', $result[$i]['prodcost']);
    $formatedResult[$i]['quantity'] = explode(',', $result[$i]['quantity']);
    $formatedResult[$i]['datee'] = explode(',', $result[$i]['datee']);
    $formatedResult[$i]['timee'] = explode(',', $result[$i]['timee']);
    $formatedResult[$i]['totalamount'] = explode(',', $result[$i]['totalamount']);
    //Repeat with the other elements with similar formating
    $i++;
}

echo '<pre>';
print_r($formatedResult); //Now shows the array you want
echo '</pre>';

我认为您需要一种算法来重新格式化数据库中的结果。我可以看到你想要的就是得到那些连接的结果,并用独立的数组键将它们分开

我还没有尝试过,但我怀疑您可能需要也可能不需要$I索引来分配元素。两种都试试

以下是一种实现此目标的方法:

$formattedResult=[];
$i=0;
而($result=mysqli\u fetch\u assoc($result))
{
$FormattedResult=$result;
$formattedResult[$i]['prodsize']=分解(',',$result[$i]['prodsize']);
$formattedResult[$i]['prodcost']=explode(',',$result[$i]['prodcost']);
$FormattedResult[$i]['quantity']=分解(',',$result[$i]['quantity']);
$formattedResult[$i]['datee']=分解(',',$result[$i]['datee']);
$formattedResult[$i]['timee']=分解(',',$result[$i]['timee']);
$FormattedResult[$i]['totalamount']=分解(',',$result[$i]['totalamount']);
//对具有类似格式的其他元素重复此操作
$i++;
}
回声';
Array
(
    [0] => Array
        (
            [catid] => 1
            [catname] => CN1
            [productid] => 1
            [prodimg] => ABC
            [prodsize] => small,medium,large
            [prodcost] => 10,20,30
            [quantity] => 1,1,1,1
            [datee] => 3/10/2015,4/10/2015,3/10/2015,3/10/2015
            [timee] => 10:00am,10:00am,10:00am,10:00am
            [totalamount] => 10,20,30,5
        )

)
打印(FormattedResult)//现在显示所需的阵列 回声';
Array
(
    [0] => Array
        (
            [catid] => 1
            [catname] => CN1
            [productid] => 1
            [prodimg] => ABC
            [prodsize] => small,medium,large
            [prodcost] => 10,20,30
            [quantity] => 1,1,1,1
            [datee] => 3/10/2015,4/10/2015,3/10/2015,3/10/2015
            [timee] => 10:00am,10:00am,10:00am,10:00am
            [totalamount] => 10,20,30,5
        )

)

它以什么方式不起作用?结果如何?SQL错误?数组的格式不符合您的要求?但它不起作用。是否有任何错误/意外结果?是否尝试确保查询没有失败
echo$sql=“选择catid、catname、productid、prodimg、GROUP_CONCAT(按id排序的prodsize ORDER ASC)作为prodsize,GROUP_CONCAT(按id排序的prodcost ORDER ASC)作为prodcost,GROUP_CONCAT(按id排序的DATE ORDER ASC)作为datee,GROUP__CONCAT(按id排序的TIME ORDER ASC)作为timee,GROUP_CONCAT(totalamount ORDER BY id ASC)作为来自购物车的totalamount,其中userid='$userid''
你看到了什么?@Juan Bonnett没有出现错误,但数组的获取方式与我的不同wanted@HawasKaPujaari查询没有失败,但我没有按照我想要的方式获取数组。请在phpmyadmin或任何其他MySQL环境中执行查询,并向我们显示结果?它只提供了用户添加的1个产品的详细信息,但我我已经更新了我的帖子,当你的查询返回所有三行时,你可以在mysql控制台或phpMyAdmin上检查它
Array
(
    [0] => Array
        (
            [catid] => 1
            [catname] => CN1
            [productid] => 1
            [prodimg] => ABC
            [prodsize] => small,medium,large
            [prodcost] => 10,20,30
            [quantity] => 1,1,1,1
            [datee] => 3/10/2015,4/10/2015,3/10/2015,3/10/2015
            [timee] => 10:00am,10:00am,10:00am,10:00am
            [totalamount] => 10,20,30,5
        )

)