简体   繁体   中英

php array stored in session

still no solution on this question

So this is what I'm trying to do:

Have a product in a shopping cart with options. the options will be selectable through <input type="checkbox/> And the products are stored based on id's in sessions with php.

Right now I have the candies submitting as an array (like below) But I'm not sure how to add it to that specific product within the session.

My Input for the options is like this:

<?php foreach($options as $option): ?>
   <input type="checkbox" name="candies[]" value="<?php echo $option['candy_name']; ?>" /><?php echo $option['candy_name']; ?><br/>
<?php endforeach; ?>

My current code for adding the product to the session is like this:

...
case "add":
 $_SESSION['cart'][$product_id]++;
 header('location: ../../cart.html');
break;
...

I've also placed $candies = $_GET["candies"]; above the above code to get the options.

How can I make it so the options array is stored in the session for each individual product?

In this image below, you can see what I'm talking about... I am wanting to display each selected option under the options column for the specific product.

It looks like you've got the correct HTML structure but your code is disjointed. The simplest answer is to do something like this

$_SESSION['cart'][$product_id] = array('options' => $_POST['candies']);

Then you would do something like

foreach($_SESSION['cart'][$product_id]['options'] as $opt) {
     echo $opt;
}

Code should look like this first html take checkbox

<form action="sample.php" method="post">
<input type="checkbox" name="candies[]" value="Candy1" />
<input type="checkbox" name="candies[]" value="Candy2" />
<input type="checkbox" name="candies[]" value="Candy3" />
<input type="submit" value="Submit" />
<form>

then session

<?php
session_start();
$result = array();
foreach ( $_POST['candies'] as $key=>$row ) :

    $result[$key] = $row[$key];
endforeach; 

$_SESSION['candies'] = $result;

print_r($_SESSION['candies']);

?>

this might not be the exact code you are looking for but this should serve as your guide to what you want to achieve.

Let's say you want to get the product options values from you database's productOptions.

$resultSet = mysql_query("SELECT * FROM productOptions");

Now we display the checkboxes to your form based on the result of the query.

while($row = mysql_fetch_assoc($resultSet)){
     echo "<input type='checkbox' name='options[]' value='{$row['optionsID']}' />";
}

After displaying the check boxes to our form, we get the checked value from those check boxes that where selected.

if(isset($_POST['yourSubmitButtonName'])){
     $productSelected = array();
     if(!empty($_POST['options'])){
          foreach($_POST['options'] as $options){
               $optionsSelected[] = $options;
          }
     }
}

Now, set the array of options the the $_SESSION variable.

$_SESSION['yourProductID'] = $optionsSelected;

Then we display the options for a product this way

foreach($_SESSION['yourProductID'] as $productOptions){
     echo $productOptions;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM