Checkout Using 2D Array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mythoslife
    New Member
    • Mar 2013
    • 6

    Checkout Using 2D Array

    I'm trying to convert this into 2d array using while, list, each...
    Code:
    <B>Checkout</B><br>
    Below is a summary of the products you wish to purchase, along with totals:
    <?php
    
    #tax rate is constant
    $tax = 0.08;
    $total_price = 0;
    $total_tax = 0;
    $total_shipping = 0;
    $grand_total = 0;
    ?><ul><?
    
    //A function that does the calculations
    function calculation($price, $shipping, $tax) {
    $total_price += $price;
    $total_tax += $tax * $price;
    $total_shipping += $shipping * $price; 
    $grand_total = ($total_price + $total_tax + $total_shipping);
    return $grand_total;
    }
    
    //Items 
    $product = "Candle Holder";
    $price = 11.95;
    $shipping = 0; //free shipping
    echo "<li>".$product.": $".$price;
    $grand_total += calculation($price, $shipping, $tax);
    
    $product = "Coffee Table";
    $price = 99.50;
    $shipping = 0.10; //shipping as a percentage of price
    echo "<li>".$product.": $".$price;
    $grand_total += calculation($price, $shipping, $tax);
    
    
    $product = "Floor Lamp";
    $price = 44.99;
    $shipping = 0.10; //shipping as a percentage of price
    echo "<li>".$product.": $".$price;
    $grand_total += calculation($price, $shipping, $tax);
    
    
    ?>
    
    <hr>
    <br>
    <B>Total (including tax and shipping): $<? echo number_format($grand_total, 2); ?></B>
    This is what I got so far but I got stock? Any ideas?
    Code:
     $products = array( array( product => 'Candle Holder', 
                 price => 11.95, 
                 shipping => 0 
                ),
              array( product => 'Coffee Table', 
                 price => 99.50, 
                 shipping => 0.10 
                ),
              array( product => 'Floor Lamp', 
                 price => 44.99, 
                 shipping => 0.10 
                ) 
             );
    		 
    for ( $row = 0; $row < 3; $row++ )
    {
     while (list($key, $value ) = each($products[$row]))
     {
      echo " | $value";
     }
     echo ' <br />';
    }
  • divideby0
    New Member
    • May 2012
    • 131

    #2
    Are you wanting to print out $products? If so, maybe something like.

    Code:
    for($i = 0; $i < count($products); ++$i)
        foreach($products[$i] as $key => $value)
            echo $value . "<br />";

    Comment

    • mythoslife
      New Member
      • Mar 2013
      • 6

      #3
      Thank you that works. I'm looking to use a while loop instead that displays the products and later on can use it to calculate. Ugh. I'm so lost.

      Comment

      • mythoslife
        New Member
        • Mar 2013
        • 6

        #4
        What I want to do is to traverse 2d array and calculate the product, the output should be:
        • Candle Holder: 11.95
        • Candle Holder: 11.95
        Coffee Table: 99.5
        • Floor Lamp: 44.99


        TOTAL: (Total of the item above + shipping + tax)

        Here's what I have so far:
        Code:
        <B>Checkout</B><br>
        Below is a summary of the products you wish to purchase, along with totals:
        <?php
        
        #tax rate is constant
        $tax = 0.08;
        $total_price = 0;
        $total_tax = 0;
        $total_shipping = 0;
        $grand_total = 0;
        ?><ul><?
        
        //A function that does the calculations
        function calculation($price, $shipping, $tax) {
        $total_price += $price;
        $total_tax += $tax * $price;
        $total_shipping += $shipping * $price; 
        $grand_total = ($total_price + $total_tax + $total_shipping);
        return $grand_total;
        }
        
        
        $products = array( array( product => 'Candle Holder', 
                     price => 11.95, 
                     shipping => 0 
                    ),
                  array( product => 'Coffee Table', 
                     price => 99.50, 
                     shipping => 0.10 
                    ),
                  array( product => 'Floor Lamp', 
                     price => 44.99, 
                     shipping => 0.10 
                    ) 
                 );
        		 
        for ( $row = 0; $row < 3; $row++ )
        {
         while (list($key, $value ) = each($products[$row]))
         {
          echo "<li>$value";
         }
         echo ' <br />';
         $grand_total += calculation($price, $shipping, $tax);
        } 
        /* 
        //Items 
        $product = "Candle Holder";
        $price = 11.95;
        $shipping = 0; //free shipping
        echo "<li>".$product.": $".$price;
        $grand_total += calculation($price, $shipping, $tax);
        
        $product = "Coffee Table";
        $price = 99.50;
        $shipping = 0.10; //shipping as a percentage of price
        echo "<li>".$product.": $".$price;
        $grand_total += calculation($price, $shipping, $tax);
        
        
        $product = "Floor Lamp";
        $price = 44.99;
        $shipping = 0.10; //shipping as a percentage of price
        echo "<li>".$product.": $".$price;
        $grand_total += calculation($price, $shipping, $tax);
        */
        
        ?>
        
        <hr>
        <br>
        <B>Total (including tax and shipping): $<? echo number_format($grand_total, 2); ?></B>

        Comment

        • divideby0
          New Member
          • May 2012
          • 131

          #5
          There's no attempt at error checking, but what about something like below?

          Code:
          $total = 0;
          
          for($i=0; $i < count($products); ++$i)
          {
             $p_tmp = 0; // price 
             $s_tmp = 0; // shipping
          
             while(list($key, $val) = each($products[$i]))
             {
                 if($key === price)
                    $p_tmp = $val;
                 if($key === shipping)
                    $s_tmp = $val;
          
                 // output item info 
             }
          
             $total += compute($p_tmp, $s_tmp, $tax);
          }
          
          echo number_format($total, 2);

          Comment

          • mythoslife
            New Member
            • Mar 2013
            • 6

            #6
            Thank you! This should work!

            Comment

            • mythoslife
              New Member
              • Mar 2013
              • 6

              #7
              Do you have any ideas to take off the third item?

              Item 1: $00.00: $0
              Item 2: $00.00: $0
              Item 3: $00.00: $0

              Total = $some amount

              Goal: is to not echo the third value or the shipping amount.

              So far this is what I have using two nested while loop:

              Code:
              $products = array( array( product => "Candle Holder", 
                           "price" => 11.95, 
                           "shipping" => 0 
                          ),
                        array( product => "Coffee Table", 
                           "price" => 99.50, 
                           "shipping" => 0.10 
                          ),
                        array( product => "Floor Lamp", 
                           "price" => 44.99, 
                           "shipping" => 0.10 
                          ) 
                       );
              
              while (list($key, $value) = each($products))
              {
                 $p_tmp = 0; // price 
                 $s_tmp = 0; // shipping
               
                 while(list($key2, $value2) = each($value))
                 {
                     if($key2 == "price")
                        $p_tmp = $value2;
                        
                     if($key2 == "shipping")
                        $s_tmp = $value2;      
                     echo "$".$value2.": ";
                 }
                 echo "<br>";
                 $grand_total += calculation($p_tmp, $s_tmp, $tax);
              }

              Comment

              • divideby0
                New Member
                • May 2012
                • 131

                #8
                by remove third item, do you mean not print the shipping value or do you mean to actually remove the third array in products? for the former, you might try something like

                Code:
                if($key2 == "price")
                   $p_tmp = $value2;
                  
                if($key2 == "shipping")
                   $s_tmp = $value2;
                
                if($key2 == "product")
                   echo $value2 . " ";
                else if($key2 == "price")
                   echo "$" . $value2;
                the output I got was

                Candle Holder $11.95
                Coffee Table $99.5
                Floor Lamp $44.99

                for the latter, you *should* be able to use array_pop on products before you enter the outer while loop.

                Code:
                array_pop($products);
                
                while(...)
                {
                   ...
                }

                Comment

                • mythoslife
                  New Member
                  • Mar 2013
                  • 6

                  #9
                  I got it thanks for your help though.. this is what I figured:

                  Code:
                  while (list($key, $value) = each($products)) {
                  	echo '<li>'.$value['product'] . ': ' . $value['price'] . '<br />', "\n";
                  	$grand_total += calculation($value['price'], $value['shipping'], $tax);

                  Comment

                  Working...