简体   繁体   English

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

[英]fetch values from database and display result in array

I have a cart table that looks like this 我有一个看起来像这样的购物车桌

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        

I wish to display its data in an array (according to userid )in a particular manner, the resulting array that i want would look like this 我希望以特定的方式在数组中显示其数据(根据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
                )
        )
)    

code that i used is 我使用的代码是

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

but it didnt show the array in the way i wanted, it showed the following array 但它没有以我想要的方式显示数组,它显示了以下数组

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
        )

)

can anyone please tell how to get the array in correct manner. 任何人都可以告诉如何以正确的方式获得阵列。 One more important aspect of this array is that the array within prodsize should be fixed, ie if the prod size is small then it will be stored in [0], medium in [1], large in[2] and perpiece in[3], if one of these values is not present then it should be 0 and prodcost, quantity datee and timee will also follow the same method 这个数组的一个更重要的方面是prodsize中的数组应该是固定的,即如果prod尺寸小,那么它将被存储在[0]中,中[1]中,大中[2]和perpiece [3]中。 ],如果其中一个值不存在那么它应该是0和prodcost,数量datee和timee也将遵循相同的方法

I think you need al algorithm to reformat the results from the database. 我认为您需要al算法来重新格式化数据库中的结果。 I can see all you want is to get those concatenated results and separate them in independent array keys. 我可以看到你想要的只是得到那些连接的结果并将它们分开在独立的数组键中。

I haven't been able to try it, but I suspect you might or might not need the $i index to allocate the elements. 我无法尝试,但我怀疑你可能需要或不需要$ i索引来分配元素。 Try both. 试试两个。

Here's a way you might achieve that: 这是您可能实现的方式:

$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>';

To get all results of sql select query in an array , just use fetch all function as below 要获取数组中sql select query的所有结果,只需使用fetch all function,如下所示

$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);
        // Fetch all
        $r=mysqli_fetch_all($result,MYSQLI_ASSOC);
        print_r($r);
        // Free result set
        mysqli_free_result($result);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM