add values into session

kaits

Mitglied
Hi!

I make an e-shop but how I do add products into users cart?

I must add sessions together ot what?
I must add this information:

user ID
username
product ID
product price
product amount

and if the user adds a new product, the session with these values must be updated, we must add an extra product with his price an amount

how to do that?

and feel free to answer in German :)


thanks!
 
Use the superglobal array _SESSION!

Save userID and username as as string and the informations of the product in an array.
Like this:
PHP:
//initializing:
$_SESSION['userid'] = $yourid;
$_SESSION['username'] = $yourname;

$_SESSION['productid'] = array();
$_SESSION['productprice'] = array();
$_SESSION['productamount'] = array();

Later, you can add products:
PHP:
//adding product:
$_SESSION['productid'][] = $product_id;
$_SESSION['productprice'][] = $product_price;
$_SESSION['productamount'][] = $product_amount;
 
big thanks!

and how now get all the added products from the sessions with id's, etc to page? for example into a list

product 1
product 2
...
etc
 
PHP:
<?php session_start(); ?>
<table border="1">
<tr>
    <td>Product id:</td>
    <td>Product price</td>
    <td>Product amount</td>
</tr>
<?php
$pId = $_SESSION['productid'];
$pPrice = $_SESSION['productprice'];
$pAmount = $_SESSION['productamount'];

for ($i=0; $i<count($pId); $i++) {
    ?>
    <tr>
        <td><?php=$pId[$i]?></td>
        <td><?php=$pPrice[$i]?></td>
        <td><?php=$pAmount[$i]?></td>
    </tr>
    <?php
}
?>
</table>
 
Zuletzt bearbeitet von einem Moderator:
Zurück