Jump to content

Dynamic Associative Arrays and Sessions


dbell23

Recommended Posts

Probably easiest to explain what I am trying to do, to illustrate the impasse I have came to. I have online store With a number of products. Visitors can click on products, clicking brings details of the product came up in a frame on the right (with a field to enter the quantity), which can than be added to the shopping cart in a separate frame below it bottom right (passed using a different script (the one which I am having issues with)). So each time a product is added to the cart, three dynamic fields are passed via post script, which are named "product_name", "unit_quantity" and "unit_price" (these are all hidden fields called from a sql database based on what product the user clicked on).

 

So for the shopping cart, I want to incorporate an associative array using the three dynamic fields described above("product_name", "unit_quantity" and "unit_price") within a session so that a number of products can be added (hence using an array rather than a variable).

 

So shopping cart i have done so far reads:

 

<?php

session_start();

 

if (!isset ($product_name) && !empty($product_name))

{

 

if (!session_is_registered("product_info"))

{

$pos=0;

session_register("pos");

$product_info[$pos]= array ( 'product_name'=>$product_name, 'unit_quantity'=>$unit_quantity,

'product_quantity'=>$unit_quantity * $unit_price);

session_register("product_info");

}

 

 

?>

 

Not sure (as I am new to sessions, how to call the array to the screen). I have read through past threads but have not been able to found anything similar to what I am trying to achieve).

 

Link to comment
Share on other sites

session_register() and session_is_registered() are both deprecated. Use the $_SESSION array like any other array instead.

 

Currently this line reads "if the field is not set AND the field is not empty". I think you can see the logical problem with that . . .

if (!isset ($product_name) && !empty($product_name))

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.