ling
ling

Reputation: 155

How to store multiple value inside array and show them

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/>';
        }
    }

Upvotes: 1

Views: 1802

Answers (1)

ASR
ASR

Reputation: 1811

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>

Upvotes: 3

Related Questions