php - fetch values from database and display result in array -
i have cart table looks
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 display data in array (according userid )in particular manner, resulting array want this
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 used is
$userid = $_request['userid']; $sql= "select catid, catname, productid, prodimg, group_concat(prodsize order id asc) prodsize, group_concat(prodcost order id asc) prodcost, group_concat(quantity order id asc) quantity, group_concat(datee order id asc) datee, group_concat(timee order id asc) timee, group_concat(totalamount order id asc) totalamount cart 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 didnt show array in way wanted, showed 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 please tell how array in correct manner. 1 more important aspect of array array within prodsize should fixed, i.e if prod size small stored in [0], medium in [1], large in[2] , perpiece in[3], if 1 of these values not present should 0 , prodcost, quantity datee , timee follow same method
i think need al algorithm reformat results database. can see want concatenated results , separate them in independent array keys.
i haven't been able try it, suspect might or might not need $i index allocate elements. try both.
here's way 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 other elements similar formating $i++; } echo '<pre>'; print_r($formatedresult); //now shows array want echo '</pre>';
Comments
Post a Comment