tonyhodge Posted August 14, 2013 Share Posted August 14, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/281163-session-array-not-working-properly/ Share on other sites More sharing options...
trq Posted August 14, 2013 Share Posted August 14, 2013 Variables are not interpolated when within single quoted strings. Quote Link to comment https://forums.phpfreaks.com/topic/281163-session-array-not-working-properly/#findComment-1444991 Share on other sites More sharing options...
tonyhodge Posted August 14, 2013 Author Share Posted August 14, 2013 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>'; Quote Link to comment https://forums.phpfreaks.com/topic/281163-session-array-not-working-properly/#findComment-1444993 Share on other sites More sharing options...
trq Posted August 14, 2013 Share Posted August 14, 2013 Either the descriptions of your problem is off or your posting code that doesn't seem relevant. What exactly is the issue at hand and can we see some relevant code? Quote Link to comment https://forums.phpfreaks.com/topic/281163-session-array-not-working-properly/#findComment-1444994 Share on other sites More sharing options...
tonyhodge Posted August 14, 2013 Author Share Posted August 14, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/281163-session-array-not-working-properly/#findComment-1444995 Share on other sites More sharing options...
trq Posted August 14, 2013 Share Posted August 14, 2013 Replace this: $products = array(); $products[] = $pid // Add to session $_SESSION['ids'] = $products; With: $_SESSION['ids'][] = $pid; Quote Link to comment https://forums.phpfreaks.com/topic/281163-session-array-not-working-properly/#findComment-1444997 Share on other sites More sharing options...
Solution trq Posted August 14, 2013 Solution Share Posted August 14, 2013 And this: echo '<br />'.sizeof($_SESSION['ids']['$products']); With: echo '<br />'.sizeof($_SESSION['ids']); Quote Link to comment https://forums.phpfreaks.com/topic/281163-session-array-not-working-properly/#findComment-1444998 Share on other sites More sharing options...
tonyhodge Posted August 14, 2013 Author Share Posted August 14, 2013 Something so simple. Believe it or not I have been working with PHP for a while now just sessions still confuse me.! Thank you for your help. Quote Link to comment https://forums.phpfreaks.com/topic/281163-session-array-not-working-properly/#findComment-1445000 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.