$_SESSION['items_' . $restaurant_id]["menu_0"] = array(
order_id" => $order_id,
'user_order_id' => $last_order["user_order_id"],
"menu_id" => $menu_id,
"qty" => $qty,
);
that code is my session and will be return become this :
["items_250"]=>
array(2) {
["menu_0"]=>
array(3) {
["user_order_id"]=>
string(2) "85"
["menu_id"]=>
array(3) {
[0]=>
string(2) "236"
[1]=>
string(2) "357"
[2]=>
string(2) "232"
}
["qty"]=>
array(3) {
[0]=>
string(1) "1"
[1]=>
string(1) "1"
[2]=>
string(1) "1"
}
}
in my `$menu_id is array :
array(4) {
[0]=>
string(3) "236"
[1]=>
string(3) "357"
[3]=>
string(3) "232"
}
this is my $qty :
array(3) {
[0]=>
string(1) "1"
[1]=>
string(1) "1"
[2]=>
string(1) "1"
}
i want trying to make my session will be return like this :
["items_250"]=>
array(3) {
["menu_0"]=>
array(3) {
["user_order_id"]=>
string(2) "85"
["menu_id"]=>
string(3) "236"
["qty"]=>
int(1)
}
["menu_1"]=>
array(3) {
["user_order_id"]=>
string(2) "85"
["menu_id"]=>
string(3) "357"
["qty"]=>
int(1)
}
["menu_2"]=>
array(3) {
["user_order_id"]=>
string(2) "85"
["menu_id"]=>
string(3) "232"
["qty"]=>
int(1)
}
guys can you help me how to make my session become like that?
thank you (:
Just use good ol' for loop. First get the count, then create the necessary rows:
$count = count($menu_id); // get count
for($i = 0; $i < $count; $i++) {
$_SESSION['items_' . $restaurant_id]['menu_' . $i] = array(
'user_order_id' => $last_order['user_order_id'],
'menu_id' => $menu_id[$i],
'qty' => $qty[$i],
);
}
Related
I have an SQL query :
SELECT
DATE,
ID_DOMAIN,
SUM(CLICKS) AS CLICKS,
SUM(ADS) AS ADS,
SUM(CONVERSION) AS CONVERSION,
FROM website_stats
WHERE ID_USER_PRODUCT_DIFFUSER = :id_user_product_diffuser
AND DATE >= :date_begin
AND DATE <= :date_end
AND ID_DOMAIN != 0
GROUP BY DATE, ID_DOMAIN;
In my PDO fetchAll, I use $sth->fetchAll(PDO::FETCH_ASSOC | PDO::FETCH_GROUP). Then I have this output :
array(2) {
["2018-09-14"]=>
array(2) {
[0]=>
array(4) {
["ID_DOMAIN"]=>
string(1) "7"
["CLICKS"]=>
string(4) "1092"
["ADS"]=>
string(4) "1070"
["CONVERSION"]=>
string(3) "269"
}
[1]=>
array(4) {
["ID_DOMAIN"]=>
string(1) "9"
["CLICKS"]=>
string(3) "459"
["ADS"]=>
string(3) "452"
["CONVERSION"]=>
string(2) "56"
}
}
["2018-09-15"]=>
array(2) {
[0]=>
array(4) {
["ID_DOMAIN"]=>
string(1) "7"
["CLICKS"]=>
string(3) "914"
["ADS"]=>
string(3) "880"
["CONVERSION"]=>
string(3) "166"
}
[1]=>
array(4) {
["ID_DOMAIN"]=>
string(1) "9"
["CLICKS"]=>
string(3) "365"
["ADS"]=>
string(3) "361"
["CONVERSION"]=>
string(2) "57"
}
}
}
But for my code, I need the ID_DOMAIN a key in my sub array. It's possible without looping ? I means, just with PDO::FETCH* ? In fact, I need an array just like this :
array(2) {
["2018-09-14"]=>
array(3) {
[7]=>
array(3) {
["CLICKS"]=>
string(4) "1092"
["ADS"]=>
string(4) "1070"
["CONVERSION"]=>
string(3) "269"
}
[9]=>
array(3) {
["ID_DOMAIN"]=>
string(1) "9"
["CLICKS"]=>
string(3) "459"
["ADS"]=>
string(3) "452"
["CONVERSION"]=>
string(2) "56"
}
}
["2018-09-15"]=>
array(2) {
[7]=>
array(3) {
["ID_DOMAIN"]=>
string(1) "7"
["CLICKS"]=>
string(3) "914"
["ADS"]=>
string(3) "880"
["CONVERSION"]=>
string(3) "166"
}
[9]=>
array(3) {
["CLICKS"]=>
string(3) "365"
["ADS"]=>
string(3) "361"
["CONVERSION"]=>
string(2) "57"
}
}
}
I search for the good PDO fetch mode for long time but it seems like nobody has this problem in this website :/
Thank you in advance :)
this is my session which will be show by $order_id
["items_250"]=>
array(4) {
["menu_0"]=>
array(4) {
["user_order_id"]=>
string(3) " 86"
["menu_id"]=>
string(3) "231"
["qty"]=>
string(1) "1"
["choice"]=>
array(1) {
[0]=>
string(1) "3"
}
}
["menu_1"]=>
array(4) {
["user_order_id"]=>
string(3) " 86"
["menu_id"]=>
string(3) "236"
["qty"]=>
string(1) "1"
["choice"]=>
array(1) {
[0]=>
string(0) ""
}
}
["menu_2"]=>
array(4) {
["user_order_id"]=>
string(3) " 86"
["menu_id"]=>
string(3) "232"
["qty"]=>
string(1) "1"
["choice"]=>
array(1) {
[0]=>
string(0) ""
}
}
["menu_3"]=>
array(4) {
["user_order_id"]=>
string(3) " 86"
["menu_id"]=>
string(3) "357"
["qty"]=>
string(1) "1"
["choice"]=>
array(1) {
[0]=>
string(0) ""
}
}
}
i want if i used another $oder_id it will show only the session by the $order_id, example my first order_id is 86, it will showed as the first session on top,
than if i used new $order_id example 83, it will show :
["items_250"]=>
array(4) {
["menu_0"]=>
array(4) {
["user_order_id"]=>
string(2) "83"
["menu_id"]=>
string(3) "236"
["qty"]=>
string(1) "1"
["choice"]=>
array(1) {
[0]=>
string(0) ""
}
}
["menu_1"]=>
array(4) {
["user_order_id"]=>
string(2) "83"
["menu_id"]=>
string(3) "357"
["qty"]=>
string(1) "1"
["choice"]=>
array(1) {
[0]=>
string(0) ""
}
}
but if i used another $order_id
it will be showed this :
["items_250"]=>
array(4) {
["menu_0"]=>
array(4) {
["user_order_id"]=>
string(2) "83"
["menu_id"]=>
string(3) "236"
["qty"]=>
string(1) "1"
["choice"]=>
array(1) {
[0]=>
string(0) ""
}
}
["menu_1"]=>
array(4) {
["user_order_id"]=>
string(2) "83"
["menu_id"]=>
string(3) "357"
["qty"]=>
string(1) "1"
["choice"]=>
array(1) {
[0]=>
string(0) ""
}
}
["menu_2"]=>
array(4) {
["user_order_id"]=>
string(3) " 86"
["menu_id"]=>
string(3) "232"
["qty"]=>
string(1) "1"
["choice"]=>
array(1) {
[0]=>
string(0) ""
}
}
["menu_3"]=>
array(4) {
["user_order_id"]=>
string(3) " 86"
["menu_id"]=>
string(3) "357"
["qty"]=>
string(1) "1"
["choice"]=>
array(1) {
[0]=>
string(0) ""
}
}
}
as you can see from new session, the session of user_order_id are different, but it still showed the last session of user_order_id
this is how i set my session:
if($order_id !== $this->session->userdata('user_order_id')) {
$count = count($menu_id); // get count
for ($i = 0; $i < $count; $i++) {
$_SESSION['items_' . $restaurant_id]['menu_' . $i] = array(
'user_order_id' => $order_id,
'menu_id' => $menu_id[$i],
'qty' => $qty[$i],
"choice" => array($variant_name[$i]['variant_menu_id']),
);
}
}else{
}
guys can you help me how to update the session?
I'm attempting to create a multidimensional array which should have the ID and quantity from the $_POST array. At the moment it seems to put every quantity into each an element with each ID.However I want it to take the first elements from each array and then add them together to a new array and so on.
Whereas it should be
ID 1 - Quantity 100
ID 2 - Quantity 50
etc
But at the moment I get this
array(16) {
[0]=>
array(2) {
["id"]=>
string(1) "1"
["quantity"]=>
string(2) "50"
}
[1]=>
array(2) {
["id"]=>
string(1) "1"
["quantity"]=>
string(3) "100"
}
[2]=>
array(2) {
["id"]=>
string(1) "1"
["quantity"]=>
string(3) "100"
}
[3]=>
array(2) {
["id"]=>
string(1) "1"
["quantity"]=>
string(3) "100"
}
[4]=>
array(2) {
["id"]=>
string(2) "12"
["quantity"]=>
string(2) "50"
}
[5]=>
array(2) {
["id"]=>
string(2) "12"
["quantity"]=>
string(3) "100"
}
[6]=>
array(2) {
["id"]=>
string(2) "12"
["quantity"]=>
string(3) "100"
}
[7]=>
array(2) {
["id"]=>
string(2) "12"
["quantity"]=>
string(3) "100"
}
[8]=>
array(2) {
["id"]=>
string(1) "2"
["quantity"]=>
string(2) "50"
}
[9]=>
array(2) {
["id"]=>
string(1) "2"
["quantity"]=>
string(3) "100"
}
[10]=>
array(2) {
["id"]=>
string(1) "2"
["quantity"]=>
string(3) "100"
}
[11]=>
array(2) {
["id"]=>
string(1) "2"
["quantity"]=>
string(3) "100"
}
[12]=>
array(2) {
["id"]=>
string(1) "6"
["quantity"]=>
string(2) "50"
}
[13]=>
array(2) {
["id"]=>
string(1) "6"
["quantity"]=>
string(3) "100"
}
[14]=>
array(2) {
["id"]=>
string(1) "6"
["quantity"]=>
string(3) "100"
}
[15]=>
array(2) {
["id"]=>
string(1) "6"
["quantity"]=>
string(3) "100"
}
}
Here is my PHP code.
foreach($_POST['sweetids'] as $id) {
foreach($_POST['quantites'] as $quantity) {
$stock_array[] = array(
"id"=> $id,
"quantity" => $quantity
);
}
}
I think this is what you're trying to achieve:
foreach($_POST['sweetids'] as $key=>$id) {
$stock_array[] = array(
"id"=> $id,
"quantity" => $_POST['quantities'][$key]
);
}
You're iterating $_POST['quantities'] for every $_POST['sweetids'] which is probably not what you intend. When you iterate both, your result will be every combination of sweetids and quantities, not each pair of them.
I'm guessing you meant something more like:
// Assuming you already verified that $_POST['quantities'] and $_POST['sweetids'] exist
// and that both of them have the same number of elements
for ( $i = 0, $len = count($_POST['sweetids']); $i < $len; $i++ ) {
$stock_array[] = array(
'id' => $_POST['sweetids'][$i],
'quantity' => $_POST['quantities'][$i]
);
}
I have 7 array fetched by mysql and its working correctly each array have exactly 9 rows which are city_names but one of the array has only 7 rows since two values on the results where null . This causes confunsion while dumping file in csv. I use the below code. Is there any way to check it by city name without using foreach for eact array ?
for($i=0 ; $i <= $count-2 ; $i++)
{
$data[] = $all_restaurants_opr_no_temp_off[$i]['city_name'];
$data[] = $all_restaurants_opr[$i]['total'];
$data[] = $all_restaurants_opr_no_temp_off[$i]['total'];
$data[] = $restaurants_opr_temp_off[$i]['total'];
$data[] = $restaurants_opr_operations_closed[$i]['total'];
$data[] = $restaurants_opr_automated[$i]['total'];
$data[] = $restaurants_opr_automated_working[$i]['total'];
$data[] = $restaurants_opr_online_payment[$i]['total'];
$data[] = $restaurants_opr_online_payment_with_tempoff[$i]['total'];
$data[] = $restaurants_opr_atleast_one_order[$i]['total'];
fputcsv($fp, $data,",");
unset($data);
}
Array 1:
array(9) {
[0]=>
array(2) {
["city_name"]=>
string(9) "Bangalore"
["total"]=>
string(3) "687"
}
[1]=>
array(2) {
["city_name"]=>
string(9) "Hyderabad"
["total"]=>
string(2) "16"
}
[2]=>
array(2) {
["city_name"]=>
string(6) "Mumbai"
["total"]=>
string(3) "568"
}
[3]=>
array(2) {
["city_name"]=>
string(7) "Chennai"
["total"]=>
string(3) "139"
}
[4]=>
array(2) {
["city_name"]=>
string(4) "Pune"
["total"]=>
string(3) "232"
}
[5]=>
array(2) {
["city_name"]=>
string(9) "Ghaziabad"
["total"]=>
string(2) "57"
}
[6]=>
array(2) {
["city_name"]=>
string(5) "Noida"
["total"]=>
string(2) "77"
}
[7]=>
array(2) {
["city_name"]=>
string(9) "Faridabad"
["total"]=>
string(1) "4"
}
[8]=>
array(2) {
["city_name"]=>
string(7) "Gurgaon"
["total"]=>
string(3) "113"
}
}
Array 2:
array(9) {
[0]=>
array(2) {
["city_name"]=>
string(9) "Bangalore"
["total"]=>
string(3) "674"
}
[1]=>
array(2) {
["city_name"]=>
string(9) "Hyderabad"
["total"]=>
string(2) "16"
}
[2]=>
array(2) {
["city_name"]=>
string(6) "Mumbai"
["total"]=>
string(3) "547"
}
[3]=>
array(2) {
["city_name"]=>
string(7) "Chennai"
["total"]=>
string(3) "135"
}
[4]=>
array(2) {
["city_name"]=>
string(4) "Pune"
["total"]=>
string(3) "202"
}
[5]=>
array(2) {
["city_name"]=>
string(9) "Ghaziabad"
["total"]=>
string(2) "56"
}
[6]=>
array(2) {
["city_name"]=>
string(5) "Noida"
["total"]=>
string(2) "77"
}
[7]=>
array(2) {
["city_name"]=>
string(9) "Faridabad"
["total"]=>
string(1) "4"
}
[8]=>
array(2) {
["city_name"]=>
string(7) "Gurgaon"
["total"]=>
string(3) "111"
}
}
Array 3:(Problem is here)
array(6) {
[0]=>
array(2) {
["city_name"]=>
string(9) "Bangalore"
["total"]=>
string(2) "13"
}
[1]=>
array(2) {
["city_name"]=>
string(6) "Mumbai"
["total"]=>
string(2) "21"
}
[2]=>
array(2) {
["city_name"]=>
string(7) "Chennai"
["total"]=>
string(1) "4"
}
[3]=>
array(2) {
["city_name"]=>
string(4) "Pune"
["total"]=>
string(2) "30"
}
[4]=>
array(2) {
["city_name"]=>
string(9) "Ghaziabad"
["total"]=>
string(1) "1"
}
[5]=>
array(2) {
["city_name"]=>
string(7) "Gurgaon"
["total"]=>
string(1) "2"
}
}
I'm not sure I understand the question 100%, but perhaps you can test each value if it is null and put a default value there if it is, for example, for this row, you could use this above your for loop:
foreach($badArray as $b){
$reformattedArray[$b['city_name']] = $b['total'];
}
then replace the bad array line with this in your for loop
data[] = is_null($reformattedArray[$all_restaurants_opr_no_temp_off[$i]['city_name']]) ? 'Default' : $reformattedArray[$all_restaurants_opr_no_temp_off[$i]['city_name']];
I currently have an array that looks like the one below but I only want to display show results in the array from which are unique (description), I presume with array_unique? but I keep getting the same results?
Here is my array:
$taglist = array(5) {
[0]=> array(5) {
["id"]=> string(2) "27"
["page_id"]=> string(2) "18"
["description"]=> string(10) "Web Design"
["slug"]=> string(10) "web-design"
["visibility"]=> string(7) "visible"
}
[1]=> array(5) {
["id"]=> string(2) "29"
["page_id"]=> string(2) "18"
["description"]=> string(3) "Tutorials"
["slug"]=> string(3) "tutorials"
["visibility"]=> string(7) "visible"
}
[2]=> array(5) {
["id"]=> string(2) "31"
["page_id"]=> string(2) "21"
["description"]=> string(3) "tag"
["slug"]=> string(3) "tag"
["visibility"]=> string(7) "visible"
}
[3]=> array(5) {
["id"]=> string(2) "32"
["page_id"]=> string(2) "21"
["description"]=> string(10) "Web Design"
["slug"]=> string(10) "web-design"
["visibility"]=> string(7) "visible"
}
}
Here is my while:
$items = array();
$results = $taglist;
foreach ($results as $result)
{
$items[]= $result['description'];
$items = array_unique($items);
}
echo '<ul>';
while ($tag_item = current($items))
{
echo '<li>'.$tag_item['description'].'</li>';
next($items);
}
echo '</ul>';
$taglist = array(
0 => array('description'=>'one'),
1 => array('description'=>'two'),
2 => array('description'=>'one'),
3 => array('description'=>'three'),
4 => array('description'=>'one'),
);
// echo var_export($taglist, 1); // uncomment to see the diff vs var_dump()
foreach($taglist as $tag){
$new[] = $tag['description'];
}
var_dump(array_unique($new));