开发者

Adding an extra value to an array in a foreach loop for MYSQL INSERT

开发者 https://www.devze.com 2023-03-07 02:39 出处:网络
right now I have an array that stores a fruitname, price, quantity. My code looks like this: <?php session_start();

right now I have an array that stores a fruitname, price, quantity. My code looks like this:

<?php
session_start();
echo $_SESSION['SHOPPING_CART'];

$multi = $_SESSION['SHOPPING_CART'];
$new = array();
foreach($multi as $key=>$value) {
    $new[] = "'".implode("', '", $value)开发者_C百科."'";
}
$query = "(".implode("), (", $new).")";
echo $query;

?>

An example of what echo $query does is as follows :

Array('Apple', '1.00', '1')

What i want is for my array to look like this:

Array('Alex<todaystimestamp>','Apple','1')

for each key in the array.

What I need to accomplish is to be able to insert multiple rows into my table with the unique identifier being the username+timestamp.

Any help is much appreciated!


$inserts = array();
foreach($_SESSION['SHOPPING_CART'] as $cart_item) {
    list($fruit_name, $price, $quantity) = $cart_item;
    $username_and_timestamp = $_SESSION['username'].time();
    $inserts[] = "('$username_and_timestamp', '$fuit_name', $quantity)";
}
$query = "INSERT INTO $table_name (username_and_timestamp, fruit_name, quantity) VALUES".implode(',',$inserts);


use array_unshift to insert username as the first element of the array $value

foreach($multi as $key=>$value) {
    array_unshift($value, $_SESSION['username']);
    $new[] = "'".implode("', '", $value)."'";
}
0

精彩评论

暂无评论...
验证码 换一张
取 消