Can I use array_push on a SESSION array in php?
Yes, you can. But First argument should be an array.
So, you must do it this way
$_SESSION['names'] = array();
array_push($_SESSION['names'],$name);
Personally I never use array_push as I see no sense in this function. And I just use
$_SESSION['names'][] = $name;
How to push multiple values to a session array
The issue you are having is that it is re-declaring $_SESSION['name'] as a new array every time this code is run.
I suggest using something along the lines of:
<?php
session_start();
if(isset($_GET['title']) && $_GET['title'] !== ""){
if(!isset($_SESSION['name']) && !is_array($_SESSION['name'])){
$_SESSION['name'] = array();
}
array_push($_SESSION['name'], $_GET['title']);
}
?>
This does your previous check of seeing if $_GET['title'] is set, but then does another check to ensure if $_SESSION['name'] exists and is an array. If it's not an array it declares it.
The last thing it does is add your value to the array.
array_push pushes to many items in my session array PHP
your original php is correct just make script like this in the javascript
function addToChart(deze) {
var addToCartProductArr=[];
const brandContainer = deze.parentNode;
const productName = brandContainer.children[0].innerHTML;
addToCartProductArr.push(productName);
$.ajax({
url: "addToCart.php",
method: "POST",
data: {arr:addToCartProductArr},
});
}
What we just do is that we init the array every time the function is called by :
var addToCartProductArr=[];
php Post arrays in Session array
Either add the value to your array:
$_SESSION['answers'][] = $_POST;
or use array_push
array_push($_SESSION['answers'], $_POST);
You try to do to much at once :)
php session array add an element if same ID
Yes, you can.
You can do this by adding the value, while retrieving it from the database. For example:
while($res = $shipment->fetch_object()){
$seller_id = $res->seller;
$shipment_cost = $res->shipment_fee;
foreach ($_SESSION['items'] as $key => $item) {
if ($item['p_seller'] === $seller_id) {
$_SESSION['items'][$key]['p_fee'] = $shipment_cost;
}
}
}
A better (faster) way is to create a mapping between the sellerId and costs, and afterwards put it in the session:
$costMapping = array();
while($res = $shipment->fetch_object()){
$seller_id = $res->seller;
$shipment_cost = $res->shipment_fee;
$costMapping[$seller_id] = $shipment_cost;
}
foreach ($_SESSION['items'] as $key => $item) {
if (isset($costMapping[$item['p_seller']])) {
$_SESSION['items'][$key]['p_fee'] = $costMapping[$item['p_seller']];
}
}
This way you only have 2 loops, whatever the amount of sellers is that you have.
How can I add new items to my session array if value is not already in it? PHP
You can use array_push():
if(empty($_SESSION['coupon'])){
// Create array from session
$_SESSION['coupon']['couponcode'] = array();
}
else
{
if(!in_array( $coupon,$_SESSION['coupon']['couponcode'])) //check in array available
{
array_push($_SESSION['coupon']['couponcode'], $coupon); //push to array
}
}
Pushing an array to a session variable without re-instantiating the variable
You have to call session_start()
before you reference $_SESSION
. The output you posted indicates you don't do that.
Notice: Undefined variable: _SESSION in C:\inetpub\wwwroot\domain\store\cart.php on line 5
Related Topics
Constant Expression Contains Invalid Operations
Is MySQL_Real_Escape_String() Necessary When Using Prepared Statements
PHP & Sessions: How to Disable PHP Session Locking
Changing the Root Folder via .Htaccess
Handling Big User Ids Returned by Fql in PHP
Which Is the Best Way to Generate Excel Output in PHP
How to Remove Attributes from an HTML Tag
$_Files Field 'Tmp_Name' Has No Value on .Jpg File Extension
How to Separate Letters and Digits from a String in PHP
PHP on Windows with Xampp Running 100 Times Too Slow
Detect MySQL Update/Insertion Failure Due to Violated Unique Constraint
How to Set Selected Value of HTML Select Box with PHP