Jump to content

Session Array not working properly


tonyhodge
Go to solution Solved by trq,

Recommended Posts

I am making a small shopping basket using sessions and arrays and cannot seem to get the below working. 

<?php
// Start Session
session_start();
// Get product ID
$pid = $_GET['id'];
// Initate Array and add item.
$products = array();
$products[] = $pid
// Add to session
$_SESSION['ids'] = $products;
header("location:../auth/index.php");
?>

$pid is receiving the correct data. But when I attempt to add another item, It does not increment. 

function getBasket() {
    echo '<br />'.sizeof($_SESSION['ids']['$products']);  
}

Any help appreciated. 

 

Thanks 

Link to comment
Share on other sites

For more information, The function above is called in the index file. I still cannot get it to work though. I have played with the function and had no success. The table that is produced from the code below takes  me to the array addition then return to the index again where the above functions should display the size of the array. Hope this helps more

    include_once("dbcon.php");
    $query = 'SELECT product_id, product_type, product_name,size,colour,cost FROM products, product_types WHERE products.product_type_id = product_types.product_type_id';
    $result = mysql_query($query) or die('Query Failed'.mysql_error());
    echo '<form method="post" action="#">';
    echo '<table>';
    while ($row = mysql_fetch_array($result)) {
        echo '<tr>';
         echo  '<td>'.$row['product_id'].'</td>';         
         echo  '<td>'.$row['product_name'].'</td>';
         echo  '<td>'.$row['product_type'].'</td>';
         echo  '<td>'.$row['size'].'</td>';
         echo  '<td>'.$row['colour'].'</td>';
         echo  '<td>'.$row['cost'].'</td>';
         echo '<td><input type="text" name="quantity" /></td>';
         echo '<td><a href="../includes/addtocart.php?id='.$row['product_id'].'">Add to cart</a></td>';
         echo '<tr>';
    }
    echo '</table>';
    echo '</form>';
Link to comment
Share on other sites

I am creating a shopping basket storing the information in arrays and variables.   From the table produced below, users can select 'add to cart' that takes them to the addtocart.php page

// INDEX.php

 include_once("dbcon.php");
    $query = 'SELECT product_id, product_type, product_name,size,colour,cost FROM products, product_types WHERE products.product_type_id = product_types.product_type_id';
    $result = mysql_query($query) or die('Query Failed'.mysql_error());
    echo '<form method="post" action="#">';
    echo '<table>';
    while ($row = mysql_fetch_array($result)) {
        echo '<tr>';
         echo  '<td>'.$row['product_id'].'</td>';         
         echo  '<td>'.$row['product_name'].'</td>';
         echo  '<td>'.$row['product_type'].'</td>';
         echo  '<td>'.$row['size'].'</td>';
         echo  '<td>'.$row['colour'].'</td>';
         echo  '<td>'.$row['cost'].'</td>';
         echo '<td><input type="text" name="quantity" /></td>';
         echo '<td><a href="../includes/addtocart.php?id='.$row['product_id'].'">Add to cart</a></td>';
         echo '<tr>';
    }
    echo '</table>';
    echo '</form>';

The aim of the addtocart.php page is to store the item in an array then put the array in a session and redirect back to the item page above.  I want to add as many items to the cart as I wish. 

<?php

// ADD TO CART
// Start Session
session_start();
// Get product ID
$pid = $_GET['id'];
// Initate Array and add item.
$products = array();
$products[] = $pid
// Add to session
$_SESSION['ids'] = $products;
header("location:../auth/index.php");
?>

The error is that my code does not add to the array. It only adds one item and I am unsure if this is the correct one.

 

After I am printing out the size of the array with says 1 after adding my first item and still 1 after adding a second. The code for this is below 

echo '<br />'.sizeof($_SESSION['ids']['$products']);

Thank you for time with this.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.