1

I'm working on shopping cart and I'm storing car_id,price and add on items in session.I managed to store in the session but how do I retrieve them correctly as per id?

here's the code that stores inside the session:

$_SESSION['items'][$price]=array('total'=>$car_id,'add_on'=>$add_on);

This is how I retrieve them. I can show the id and price accodingly but not the addon ..Since one id can have multiple add on items and how do I store and retrieve accordingly in the same way?

<table>
<tr><th>price</th><th>Car id</th><th>Extras</th></tr>
        <?php
         foreach($_SESSION['items'] as $total=>$id)
          {

              ?>
              <tr>


                  <td width="100"><?php echo 'RM '.$total;?></td>
              <?php
              foreach($id as $ids)
              {


        ?>
              <td width="100"><?php  echo $ids;?></td>



                     <td width="100"><?php if(isset($add_on)){ echo $add_on;} ?></td>



              </tr>

        <?php
        }
          }

        ?>

        </table>   

EDITED PART**

How to access array inside an array?

I got the above questioned answered. Now I', trying to access multiple value of addons that stored inside array.

which looks like this:

$_SESSION['items'][$price]=array('total'=>$car_id,array('add_on'=>$add_on,'dep'=>$dep));

Edited How do I pass the variable values array via url

 if(isset($_POST['addon']))
{
    if(isset($_POST['add_item']))
    {

    foreach($_POST['add_item'] as $item)
    {
        $item.'<br/>';
        mysql_select_db($database);
        $query_item="SELECT * FROM tbl_addons WHERE addOns_id='$item'";
        $result_item=mysql_query($query_item);
        while($row_item=mysql_fetch_array($result_item))
        {
            $dep=$row_item['Deposit'];
            $ppd=$row_item['PricePerDay'];
            echo $dep.'<br/>';
            echo $ppd.'<br/>';
        }
    }
1
  • Show the structure and sample data of tbl_addons. Is addOns_id is unique? Commented Jul 30, 2014 at 4:35

1 Answer 1

3

Try this

<table>
<tr><th>price</th><th>Car id</th><th>Extras</th></tr>
        <?php
         foreach($_SESSION['items'] as $total=>$id)
          {
              ?>
              <tr>
                  <td width="100"><?php echo 'RM '.$total;?></td>             
                  <td width="100"><?php  echo $id['total'];?></td>
                  <td width="100"><?php if(isset($id['add_on'])){ echo $id['add_on'];} ?></td>
              </tr>
        <?php       
          }
        ?>
</table>   

You don't need the second loop to get values

Hope this is the way you want to store the add_on values

$_SESSION['items'][$price]= array('total'=>$car_id,
                              'add_on' => array( 'Add_on1' => array('deposite' => '20%',  'price' => 125),
                                                 'Add_on2' =>array( 'deposite' => '15%',  'price' => 234),
                                                 'Add_on3' =>array( 'deposite' => '5%',  'price' => 54))

                            );

Use this to retrieve the values

<td width="100"><?php if(isset($id['add_on'])){ 

                  foreach($id['add_on'] as $key => $value){                          
                      echo $key. ' ; Deposite - '.$value['deposite'] .' ; Price : '.$value['price'] ;
                      echo '<br>';
                  }

              } ?></td>
Sign up to request clarification or add additional context in comments.

4 Comments

@ ASR thanks alot..it worked..can you also show me how to get deposit and price for each addon please?
I'm having a headache on passing the addons array value via url ,then store inside session ,later to retrieve them.Would you be able to help me? I need urgent help.
You can use http_build_query() to do this - php.net/http_build_query and you may find this answer helpful stackoverflow.com/questions/7206978/…
,Thanks it is helpful indeed. BUt for my case now,I'm at wits end and totally no idea how to solve it.If you dont mind, can I paste the code here and could you help to sort it out? I need to get it done within an hour now...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.