SitePoint Sponsor

User Tag List

Results 1 to 21 of 21
  1. #1
    SitePoint Evangelist
    Join Date
    Apr 2005
    Location
    London, UK
    Posts
    505
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    foreach/session help needed

    hi guys,

    since 2 days i've not been to resolve this, your help will save me further days of torture.

    The below code shows the cart of a customerís session: it shows each product, quantity and price and then saveís it to a session. It all works fine.

    The first problem starts when the product is a shoe. So if the product is a shoe I allow them to enter the shoe size, next to the quantity. I tried many using a foreach loop within another, but thatís just not working.

    The second problem is saving the shoe size for each product the session too.

    dug

    output_cart.php
    PHP Code:
    function display_cart($cart$change true$images 1)
    {
      
    // display items in shopping cart
      // optionally allow changes (true or false)
      // optionally include images (1 - yes, 0 - no)

      
    global $HTTP_SESSION_VARS;

      echo 
    '<table border = 0 width = 100% cellspacing = 0>
            <form action = show_cart.php method = post>
            <tr>
            <th colspan = '
    . (1+$images) .' bgcolor="#cccccc">Item</th>
            <th bgcolor="#cccccc">Price</th>
            <th bgcolor="#cccccc" align="left">Quantity</th>
            <th bgcolor="#cccccc" align="left">Shoesize</th>'
    ;
            
          echo
    '  <th bgcolor="#cccccc">Total</th></tr>';

      
    //display each item as a table row
      
    foreach ($cart as $prdnumber => $qty)
      {
           foreach (
    $cart as $prdnumber => $shoesize)
      {
        
    $products get_book_details($prdnumber);
        echo 
    '<tr>';
        if(
    $images ==true)
        {
          echo 
    '<td align = left width=5%>';
          if (
    file_exists("images/$prdnumber.jpg"))
          {
             
    $size GetImageSize('images/'.$prdnumber.'.jpg');  
             if(
    $size[0]>&& $size[1]>0)
             {
               echo 
    '<img src="images/'.$prdnumber.'.jpg" border=0 ';
               echo 
    'width = '$size[0]/.' height = ' .$size[1]/'>';
             }
          }
          else
             echo 
    ' ';
          echo 
    '</td>';
        }
            
        echo 
    '<td align = left>'.$products['title'].'</a></td>';
        
        
        echo 
    '<td align = left>£'.number_format($products['price'], 2).'</td>';
        
       
        echo 
    '<td align = left>';
        
    // if we allow changes, quantities are in text boxes
        
    if ($change == true)
         echo 
    "<input type = text name = \"$prdnumber\" value = \"$qty\" size = 3 align = left>";
        else
          echo 
    $qty.'</td>';
          

          
          echo 
    '<td align = left>';
        
    // if we allow changes, quantities are in text boxes
        
    if ($change == true)
         echo 
    "<input type = text name = \"$prdnumber\" value = \"$shoesize\" size = 3 align = left>";
        else
          echo 
    $qty.'</td>';
        
               
              
        echo 
    '<td align = left>£'.number_format($products['price']*$qty,2)."</td></tr>\n";
      }
      
    // display total row
      
    echo '<tr>
              <th colspan = '
    . (2+$images) .' bgcolor=#cccccc>
              <th align = left bgcolor=cccccc> '
    ;
                echo  
    $HTTP_SESSION_VARS['items'];
             echo
    ' </th>';
             
            echo
    '<th align = left bgcolor=cccccc> ';
                echo  
    $HTTP_SESSION_VARS['shoesize'];
             echo
    ' </th>';
              
              echo
    '<th align = left bgcolor=#cccccc>
                  £'
    ; echo number_format($HTTP_SESSION_VARS['total_price'], 2);
              echo
    '</th>
            </tr>'
    ;
      
    // display save change button
      
    if($change == true)
      {
        echo 
    '<tr>
                <td colspan = '
    . (2+$images) .'> </td>
                <td align = center>
                  <input type = hidden name = save value = true>  
                  <input type = image src = "images/save-changes.gif" 
                         border = 0 alt = "Save Changes">
                </td>
                <td> </td>
            </tr>'
    ;
      }
      echo 
    '</form></table>';
    }

    show_cart.php
    PHP Code:
     $new $HTTP_GET_VARS['new'];

      if(
    $new)
      {
        
    //new item selected
        
    if(!isset($HTTP_SESSION_VARS['cart']))
        {
          
    $HTTP_SESSION_VARS['cart'] = array();
          
    $HTTP_SESSION_VARS['items'] = 0;
          
    $HTTP_SESSION_VARS['shoesize'] = 0;
          
    $HTTP_SESSION_VARS['total_price'] ='0.00';
          
              }
        if(isset(
    $HTTP_SESSION_VARS['cart'][$new]))
          
    $HTTP_SESSION_VARS['cart'][$new]++;
        else 
          
    $HTTP_SESSION_VARS['cart'][$new] = 1;
          
    $HTTP_SESSION_VARS['total_price'] = calculate_price($HTTP_SESSION_VARS['cart']);
          
    $HTTP_SESSION_VARS['items'] = calculate_items($HTTP_SESSION_VARS['cart']);
          
    $HTTP_SESSION_VARS['shoesize'] =  calculate_shoesize($HTTP_SESSION_VARS['cart']);
        
      }
      if(isset(
    $HTTP_POST_VARS['save']))
      {   
        foreach (
    $HTTP_SESSION_VARS['cart'] as $prdnumber => $qty)
        {
             foreach (
    $HTTP_SESSION_VARS['cart'] as $prdnumber => $shoesize)
        {
                 
          if(
    $HTTP_POST_VARS[$prdnumber]=='0')
            unset(
    $HTTP_SESSION_VARS['cart'][$prdnumber]);
          else 
            
    $HTTP_SESSION_VARS['cart'][$prdnumber] = $HTTP_POST_VARS[$prdnumber];
            
    //$HTTP_SESSION_VARS['cart'][$shoesize] = $HTTP_POST_VARS[$shoesize];
        
    }
        
    $HTTP_SESSION_VARS['total_price'] = calculate_price($HTTP_SESSION_VARS['cart']);
        
    $HTTP_SESSION_VARS['items'] = calculate_items($HTTP_SESSION_VARS['cart']);
        
    $HTTP_SESSION_VARS['shoesize'] = calculate_shoesize($HTTP_SESSION_VARS['cart']);
      
      }
    }

      
    do_html_header('Your shopping cart');

      if(
    $HTTP_SESSION_VARS['cart']&&array_count_values($HTTP_SESSION_VARS['cart']))
        
    display_cart($HTTP_SESSION_VARS['cart']);
      else
      {
        echo 
    '<p>There are no items in your cart</p>';
        echo 
    '<hr />';
      } 

  2. #2
    SitePoint Evangelist
    Join Date
    Aug 2005
    Posts
    453
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You are working at this problem the hard way, instead of using arrays of arrays create an item object that has attributes such as item ID, size, color, description, etc. Put your information in the object and then store the object in your session cart array.
    Computers and Fire ...
    In the hands of the inexperienced or uneducated,
    the results can be disastrous.
    While the professional can tame, master even conquer.

  3. #3
    SitePoint Evangelist
    Join Date
    Apr 2005
    Location
    London, UK
    Posts
    505
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks.....for the tip

    can anyone-else help?

  4. #4
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I think your mistake is that you assume the same shoe size for every item in first place. Byron's tip would really help, but of course you can use an array.

    What you need to do is instead of adding the number of items to $HTTP_SESSION_VARS['cart'][$new], you have to add an array which holds the shoe size (or any other attributes you may add). That is:
    Code php:
    if(isset($HTTP_SESSION_VARS['cart'][$new])){
          $HTTP_SESSION_VARS['cart'][$new][]=array('shoesize'=>calculate_shoesize($HTTP_SESSION_VARS['cart']));
    }else{
          $HTTP_SESSION_VARS['cart'][$new] = array(array('shoesize'=>calculate_shoesize($HTTP_SESSION_VARS['cart'])));
          $HTTP_SESSION_VARS['total_price'] = calculate_price($HTTP_SESSION_VARS['cart']);
          $HTTP_SESSION_VARS['items'] = calculate_items($HTTP_SESSION_VARS['cart']);
    }

    Which turns your foreach loop into:
    Code php:
    foreach ($cart as $prdnumber => $items){
        $qty=count($items);
        foreach($items as $item){
            $shoesize=$item['shoesize'];
        }
    }

    Hope that makes sense.
    Saul

  5. #5
    SitePoint Evangelist
    Join Date
    Apr 2005
    Location
    London, UK
    Posts
    505
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hmmmm, yes i think you right. i amended the code to what you suggested, but this part of the code is playing crazy.

    click here, go on Mens >> Trainers >> select any products >> add to cart

    if you enter the shoesize and save it updates the quanity, if you add a second product it displays the whole form twice (i know this is because i am using two foreach loops). how to fix it?

    PHP Code:
    function display_cart($cart$change true$images 1)
    {
      
    // display items in shopping cart
      // optionally allow changes (true or false)
      // optionally include images (1 - yes, 0 - no)

      
    global $HTTP_SESSION_VARS;

      echo 
    '<table border = 0 width = 100% cellspacing = 0>
            <form action = show_cart.php method = post>
            <tr>
            <th colspan = '
    . (1+$images) .' bgcolor="#cccccc">Item</th>
            <th bgcolor="#cccccc">Price</th>
            <th bgcolor="#cccccc" align="left">Quantity</th>
    <th bgcolor="#cccccc" align="left">Shoesize</th>'
    ;
            
          echo
    '  <th bgcolor="#cccccc">Total</th></tr>';

      
    //display each item as a table row
      
    foreach ($cart as $prdnumber => $qty)
      {
      foreach (
    $cart as $prdnumber => $shoesize)
      {
        
    $products get_book_details($prdnumber);
        echo 
    '<tr>';
        if(
    $images ==true)
        {
          echo 
    '<td align = left width=5%>';
          if (
    file_exists("images/$prdnumber.jpg"))
          {
             
    $size GetImageSize('images/'.$prdnumber.'.jpg');  
             if(
    $size[0]>&& $size[1]>0)
             {
               echo 
    '<img src="images/'.$prdnumber.'.jpg" border=0 ';
               echo 
    'width = '$size[0]/.' height = ' .$size[1]/'>';
             }
          }
          else
             echo 
    ' ';
          echo 
    '</td>';
        }
        
        echo 
    '<td align = left>'.$products['title'].'</a></td>';
        
        
        echo 
    '<td align = left>'.number_format($products['price'], 2).'</td>';
        
       
        echo 
    '<td align = left>';
        
    // if we allow changes, quantities are in text boxes
        
    if ($change == true)
         echo 
    "<input type = text name = \"$prdnumber\" value = \"$qty\" size = 3 align = left>";
        else
          echo 
    $qty.'</td>';

           if (
    $change == true)
         echo 
    "<td align = left><input type = text name = \"$prdnumber\" value = \"$qty\" size = 3 align = left>";
        else
          echo 
    $qty.'</td>';
         
    // echo"<input type = text name = \"$prdnumber\" value = \"$qty\" size = 3 align = left></td>";
        
    echo '<td align = left>'.number_format($products['price']*$qty,2)."</td></tr>\n";
      }
      
    // display total row
      
    echo '<tr>
              <th colspan = '
    . (2+$images) .' bgcolor=#cccccc>
              <th align = left bgcolor=cccccc> '
    ;
                echo  
    $HTTP_SESSION_VARS['items'];
             echo
    ' </th>';
             
            echo
    ' <th align = left bgcolor=cccccc> ';
                echo  
    $HTTP_SESSION_VARS['shoesize'];
             echo
    ' </th>';
              
              echo
    '<th align = left bgcolor=#cccccc>
                  '
    ; echo number_format($HTTP_SESSION_VARS['total_price'], 2);
              echo
    '</th>
            </tr>'
    ;
      
    // display save change button
      
    if($change == true)
      {
        echo 
    '<tr>
                <td colspan = '
    . (2+$images) .'> </td>
                <td align = center>
                  <input type = hidden name = save value = true>  
                  <input type = image src = "images/save-changes.gif" 
                         border = 0 alt = "Save Changes">
                </td>
                <td> </td>
            </tr>'
    ;
      }
      echo 
    '</form></table>';
    }


  6. #6
    SitePoint Evangelist
    Join Date
    Aug 2005
    Posts
    453
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:

     
    //display each item as a table row
      
    foreach ($cart as $prdnumber => $qty)
      {
      foreach (
    $cart as $prdnumber => $shoesize)
      { 
    In the above snippet of code you are referencing the same information, you are just calling it by a different name.
    Computers and Fire ...
    In the hands of the inexperienced or uneducated,
    the results can be disastrous.
    While the professional can tame, master even conquer.

  7. #7
    SitePoint Evangelist
    Join Date
    Apr 2005
    Location
    London, UK
    Posts
    505
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes, i know. i just dont konow how else to do it? any ideas?

  8. #8
    SitePoint Evangelist
    Join Date
    Aug 2005
    Posts
    453
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah, see my first post, that is the way most programmers would tackle it. The second option is going to be make an array to contain item attributes. No amount of coding will fix bad design.
    Computers and Fire ...
    In the hands of the inexperienced or uneducated,
    the results can be disastrous.
    While the professional can tame, master even conquer.

  9. #9
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Well, I don't quite see what you changed, but I imagine it something like:
    Code php:
    foreach ($cart as $prdnumber => $items)
      {
          $qty=count($items);
     
    	  foreach ($items as $item)
    	  {
    	    $products = get_book_details($prdnumber);
     
    	    echo '<tr>';
     
    	    if($images ==true)
    	    {
    	      echo '<td align = left width=5%>';
     
    	      if (file_exists("images/$prdnumber.jpg"))
    	      {
    	         $size = GetImageSize('images/'.$prdnumber.'.jpg');  
    	         if($size[0]>0 && $size[1]>0)
    	         {
    	           echo '<img src="images/'.$prdnumber.'.jpg" border=0 ';
    	           echo 'width = '. $size[0]/3 .' height = ' .$size[1]/3 . '>';
    	         }
    	      } else {
    	         echo ' ';
    	      }
    	      echo '</td>';
    	    }
    	    echo '<td align = left>'.$products['title'].'</a></td>';
     
    	    echo '<td align = left>'.number_format($products['price'], 2).'</td>';
     
    	    echo '<td align = left>';
     
    	    // if we allow changes, quantities are in text boxes
    	    if ($change == true)
    	    	echo "<input type = text name = \"$prdnumber\" value = \"$qty\" size = 3 align = left>";
    	    else
    	    	echo $qty.'</td>';
     
    	    if ($change == true)
    	     	echo "<td align = left><input type = text name = \"$prdnumber\" value = \"$qty\" size = 3 align = left>";
    	    else
    	    	echo $qty.'</td>';
    	    // echo"<input type = text name = \"$prdnumber\" value = \"$qty\" size = 3 align = left></td>";
    	    echo '<td align = left>'.number_format($products['price']*$qty,2)."</td></tr>\n";
    	  }
     
    	  // display total row
    	  echo '<tr>
    	          <th colspan = '. (2+$images) .' bgcolor=#cccccc>
    	          <th align = left bgcolor=cccccc> ';
     
         echo  $HTTP_SESSION_VARS['items'];
         echo' </th>';
     
         echo' <th align = left bgcolor=cccccc> ';
         echo  $HTTP_SESSION_VARS['shoesize'];
         echo' </th>';
     
          echo'<th align = left bgcolor=#cccccc>
              '; 
          echo number_format($HTTP_SESSION_VARS['total_price'], 2);
          echo'</th>
        </tr>';
     
    	  // display save change button
    	  if($change == true)
    	  {
    	    echo '<tr>
    	            <td colspan = '. (2+$images) .'> </td>
    	            <td align = center>
    	              <input type = hidden name = save value = true>  
    	              <input type = image src = "images/save-changes.gif" 
    	                     border = 0 alt = "Save Changes">
    	            </td>
    	            <td> </td>
    	        </tr>';
    	  }
    	  echo '</form></table>';
    	}
    I didn't analyze your output it produces, but double check in which loop which output should be, if it produces duplicate forms or something else.
    Saul

  10. #10
    SitePoint Evangelist
    Join Date
    Apr 2005
    Location
    London, UK
    Posts
    505
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i am getting an error:

    Warning: Invalid argument supplied for foreach() on lin e279

    line 279 >> foreach ($items as $item)

  11. #11
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Hm, what's your show_cart.php that you're currently using?
    Saul

  12. #12
    SitePoint Evangelist
    Join Date
    Apr 2005
    Location
    London, UK
    Posts
    505
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $new $HTTP_GET_VARS['new']; 

      if(
    $new
      { 
        
    //new item selected 
        
    if(!isset($HTTP_SESSION_VARS['cart'])) 
        { 
          
    $HTTP_SESSION_VARS['cart'] = array(); 
          
    $HTTP_SESSION_VARS['items'] = 0
          
    $HTTP_SESSION_VARS['shoesize'] = 0
          
    $HTTP_SESSION_VARS['total_price'] ='0.00'
           
              } 
        if(isset(
    $HTTP_SESSION_VARS['cart'][$new])){
          
    $HTTP_SESSION_VARS['cart'][$new][]=array('shoesize'=>calculate_shoesize($HTTP_SESSION_VARS['cart']));
    }else{
          
    $HTTP_SESSION_VARS['cart'][$new] = array(array('shoesize'=>calculate_shoesize($HTTP_SESSION_VARS['cart'])));
          
    $HTTP_SESSION_VARS['total_price'] = calculate_price($HTTP_SESSION_VARS['cart']);
          
    $HTTP_SESSION_VARS['items'] = calculate_items($HTTP_SESSION_VARS['cart']);

         
      } 
      if(isset(
    $HTTP_POST_VARS['save'])) 
      {    
        foreach (
    $HTTP_SESSION_VARS['cart'] as $prdnumber => $qty
        { 
             foreach (
    $HTTP_SESSION_VARS['cart'] as $prdnumber => $shoesize
        { 
                 
          if(
    $HTTP_POST_VARS[$prdnumber]=='0'
            unset(
    $HTTP_SESSION_VARS['cart'][$prdnumber]); 
          else 
            
    $HTTP_SESSION_VARS['cart'][$prdnumber] = $HTTP_POST_VARS[$prdnumber]; 
            
    //$HTTP_SESSION_VARS['cart'][$shoesize] = $HTTP_POST_VARS[$shoesize]; 
        

        
    $HTTP_SESSION_VARS['total_price'] = calculate_price($HTTP_SESSION_VARS['cart']); 
        
    $HTTP_SESSION_VARS['items'] = calculate_items($HTTP_SESSION_VARS['cart']); 
        
    $HTTP_SESSION_VARS['shoesize'] = calculate_shoesize($HTTP_SESSION_VARS['cart']); 
       
      } 


      
    do_html_header('Your shopping cart'); 

      if(
    $HTTP_SESSION_VARS['cart']&&array_count_values($HTTP_SESSION_VARS['cart'])) 
        
    display_cart($HTTP_SESSION_VARS['cart']); 
      else 
      { 
        echo 
    '<p>There are no items in your cart</p>'
        echo 
    '<hr />'
      } 

  13. #13
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I believe the last if should be also modified to meet the new model:
    Code php:
    if(isset($HTTP_POST_VARS['save'])) 
    {    
        foreach ($HTTP_SESSION_VARS['cart'] as $prdnumber => $items) 
        { 
            if(count($items)==0)unset($HTTP_SESSION_VARS['cart'][$prdnumber]);
        }
     
        $HTTP_SESSION_VARS['total_price'] = calculate_price($HTTP_SESSION_VARS['cart']); 
        $HTTP_SESSION_VARS['items'] = calculate_items($HTTP_SESSION_VARS['cart']);  
    }
    Saul

  14. #14
    SitePoint Evangelist
    Join Date
    Apr 2005
    Location
    London, UK
    Posts
    505
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    still same error @ line

    >> foreach ($items as $item)

    it only seems to work when it is modified to foreach ($cart as $items => $item)

    but then, when you input into shoesize it updates quanity?? i've had this problem since the begining...

    check the above link to the site as i cant explain it as good as if you see it for yourself

    dug

  15. #15
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Well, firstly, since you've changed it yuor calculate_price(), calculate_items(), etc. functions won't work. You need to adapt those too.

    Not sure about that error, yet. Perhaps you should modify those functions then see.
    Saul

  16. #16
    SitePoint Evangelist
    Join Date
    Apr 2005
    Location
    London, UK
    Posts
    505
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    maybe i am just being thick, but I dont see anything wrong:

    PHP Code:
    function calculate_price($cart)
    {
      
    // sum total price for all items in shopping cart
      
    $price 0.0;
      if(
    is_array($cart))
      {
        
    $conn db_connect();
        foreach(
    $cart as $prdnumber => $qty)
        {  
          
    $query "select price from products where prdnumber='$prdnumber'";
          
    $result mysql_query($query);
          if (
    $result)
          {
            
    $item_price mysql_result($result0'price');
            
    $price +=$item_price*$qty;
          }
        }
      }
      return 
    $price;
    }

    function 
    calculate_items($cart)
    {
      
    // sum total items in shopping cart
      
    $items 0;
      if(
    is_array($cart))
      {
        foreach(
    $cart as $prdnumber => $qty)
        {  
          
    $items += $qty;
        }
      }
      return 
    $items;
    }

    function 
    calculate_shoesize($cart)
    {
      
      
    $shoesize 0;
      if(
    is_array($cart))
      {
        foreach(
    $cart as $prdnumber => $shoesize)
        {  
          
    $shpesize $shoesize;
        }
      }
      return 
    $items;


  17. #17
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Everything is wrong. You see, now your $cart does not have a quantity of each item, but rather an array with attributes of each item.

    Before you had:
    Code:
    Array (
        'prdnumber1' => 1,
        'prdnumber2' => 2
    )
    Now you have (or at least should):
    Code:
    Array (
        'prdnumber1' => Array (
                                  0 => Array (
                                             'shoesize' => 43
                                         )
                              ),
        'prdnumber2' => Array (
                                  0 => Array (
                                             'shoesize' => 43
                                         ),
                                  1 => Array (
                                             'shoesize' => 43
                                         )
                              )
    )
    See the difference?

    So those should be:
    Code php:
    function calculate_price($cart)
    {
      // sum total price for all items in shopping cart
      $price = 0.0;
      if(is_array($cart))
      {
        $conn = db_connect();
        foreach($cart as $prdnumber => $items)
        {  
          $query = "select price from products where prdnumber='$prdnumber'";
          $result = mysql_query($query);
          if ($result)
          {
            $item_price = mysql_result($result, 0, 'price');
            $price +=$item_price*count($items);
          }
        }
      }
      return $price;
    }
     
     
     
    function calculate_items($cart)
    {
      // sum total items in shopping cart
      $items = 0;
      if(is_array($cart))
      {
        foreach($cart as $prdnumber => $items)
        {  
          $items += count($items);
        }
      }
      return $items;
    }

    BTW, I don't think you need calculate_shoesize
    Saul

  18. #18
    SitePoint Evangelist
    Join Date
    Apr 2005
    Location
    London, UK
    Posts
    505
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok, thanks for that.

    but still i get the same error, and when i update shoesize it updates the quanity field, and when i update the quanity field it set all 0.

    here is all the code:

    show_cart.php
    PHP Code:
    $new $HTTP_GET_VARS['new']; 

      if(
    $new
      { 
        
    //new item selected 
        
    if(!isset($HTTP_SESSION_VARS['cart'])) 
        { 
          
    $HTTP_SESSION_VARS['cart'] = array(); 
          
    $HTTP_SESSION_VARS['items'] = 0
          
    $HTTP_SESSION_VARS['shoesize'] = 0
          
    $HTTP_SESSION_VARS['total_price'] ='0.00'
           
              } 
        if(isset(
    $HTTP_SESSION_VARS['cart'][$new])){
          
    $HTTP_SESSION_VARS['cart'][$new][]=array('shoesize'=>($HTTP_SESSION_VARS['cart']));
    }else{
          
    $HTTP_SESSION_VARS['cart'][$new] = array(array('shoesize'=>($HTTP_SESSION_VARS['cart'])));
          
    $HTTP_SESSION_VARS['total_price'] = calculate_price($HTTP_SESSION_VARS['cart']);
          
    $HTTP_SESSION_VARS['items'] = calculate_items($HTTP_SESSION_VARS['cart']);

         
      } 
     if(isset(
    $HTTP_POST_VARS['save'])) 

    foreach (
    $HTTP_SESSION_VARS['cart'] as $prdnumber => $items

    if(
    count($items)==0)unset($HTTP_SESSION_VARS['cart'][$prdnumber]);

    $HTTP_SESSION_VARS['total_price'] = calculate_price($HTTP_SESSION_VARS['cart']); 
    $HTTP_SESSION_VARS['items'] = calculate_items($HTTP_SESSION_VARS['cart']); 

    }


      
    do_html_header('Your shopping cart'); 

      if(
    $HTTP_SESSION_VARS['cart']&&array_count_values($HTTP_SESSION_VARS['cart'])) 
        
    display_cart($HTTP_SESSION_VARS['cart']); 
      else 
      { 
        echo 
    '<p>There are no items in your cart</p>'
        echo 
    '<hr />'
      } 
    output_cart.php
    PHP Code:

     
    // declare the session variables we want access to inside the function 
      
    global $HTTP_SESSION_VARS;
      
      if(!
    $HTTP_SESSION_VARS['items']) $HTTP_SESSION_VARS['items'] = '0';
      if(!
    $HTTP_SESSION_VARS['total_price']) $HTTP_SESSION_VARS['total_price'] = '0.00';

    function 
    display_cart($cart$change true$images 1)
    {
      
    // display items in shopping cart
      // optionally allow changes (true or false)
      // optionally include images (1 - yes, 0 - no)

      
    global $HTTP_SESSION_VARS;

      echo 
    '<table border = 0 width = 100% cellspacing = 0>
            <form action = show_cart.php method = post>
            <tr>
            <th colspan = '
    . (1+$images) .' bgcolor="#cccccc">Item</th>
            <th bgcolor="#cccccc">Price</th>
            <th bgcolor="#cccccc" align="left">Quantity</th>
    <th bgcolor="#cccccc" align="left">Shoesize</th>'
    ;
            
          echo
    '  <th bgcolor="#cccccc">Total</th></tr>';

      foreach (
    $cart as $prdnumber => $items)
      {
          
    $qty=count($items);
          
          foreach (
    $cart as $items => $item)
          {
            
    $products get_book_details($prdnumber);
        
            echo 
    '<tr>';
        
            if(
    $images ==true)
            {
              echo 
    '<td align = left width=5%>';
        
              if (
    file_exists("images/$prdnumber.jpg"))
              {
                 
    $size GetImageSize('images/'.$prdnumber.'.jpg');  
                 if(
    $size[0]>&& $size[1]>0)
                 {
                   echo 
    '<img src="images/'.$prdnumber.'.jpg" border=0 ';
                   echo 
    'width = '$size[0]/.' height = ' .$size[1]/'>';
                 }
              } else {
                 echo 
    ' ';
              }
              echo 
    '</td>';
            }
            echo 
    '<td align = left>'.$products['title'].'</a></td>';
        
            echo 
    '<td align = left>'.number_format($products['price'], 2).'</td>';
        
            echo 
    '<td align = left>';
        
            
    // if we allow changes, quantities are in text boxes
            
    if ($change == true)
                echo 
    "<input type = text name = \"$prdnumber\" value = \"$qty\" size = 3 align = left>";
            else
                echo 
    $qty.'</td>';
        
            if (
    $change == true)
                echo 
    "<td align = left><input type = text name = \"$prdnumber\" value = \"$shoesize\" size = 3 align = left>";
            else
                echo 
    $qty.'</td>';
            
    // echo"<input type = text name = \"$prdnumber\" value = \"$qty\" size = 3 align = left></td>";
            
    echo '<td align = left>'.number_format($products['price']*$qty,2)."</td></tr>\n";
          }
        
          
    // display total row
          
    echo '<tr>
                  <th colspan = '
    . (2+$images) .' bgcolor=#cccccc>
                  <th align = left bgcolor=cccccc> '
    ;
        
         echo  
    $HTTP_SESSION_VARS['items'];
         echo
    ' </th>';
     
         echo
    ' <th align = left bgcolor=cccccc> ';
         echo  
    $HTTP_SESSION_VARS['shoesize'];
         echo
    ' </th>';
          
          echo
    '<th align = left bgcolor=#cccccc>
              '

          echo 
    number_format($HTTP_SESSION_VARS['total_price'], 2);
          echo
    '</th>
        </tr>'
    ;
        
          
    // display save change button
          
    if($change == true)
          {
            echo 
    '<tr>
                    <td colspan = '
    . (2+$images) .'> </td>
                    <td align = center>
                      <input type = hidden name = save value = true>  
                      <input type = image src = "images/save-changes.gif" 
                             border = 0 alt = "Save Changes">
                    </td>
                    <td> </td>
                </tr>'
    ;
          }
          echo 
    '</form></table>';
        }


  19. #19
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Oh no, everything is wrong It's hard this way, not seeing the complete code, I made some wrong assumptions.

    OK, so you need a quantity as an attribute:

    Code php:
    if(!isset($HTTP_SESSION_VARS['cart'][$new]) || $HTTP_SESSION_VARS['cart'][$new]['shoesize']!=$HTTP_GET_VARS['shoesize']){
       $HTTP_SESSION_VARS['cart'][$new]=array(
            'shoesize' => $HTTP_GET_VARS['shoesize'],
            'qty' => 1
        );
     
    }else{
          $HTTP_SESSION_VARS['cart'][$new]['qty']++;
          $HTTP_SESSION_VARS['total_price'] = calculate_price($HTTP_SESSION_VARS['cart']);
          $HTTP_SESSION_VARS['items'] = calculate_items($HTTP_SESSION_VARS['cart']);
     
    }
    Code php:
    if(isset($HTTP_POST_VARS['save'])) 
    { 
        foreach ($HTTP_SESSION_VARS['cart'] as $prdnumber => $item) 
        { 
     
            if($item['qty']==0) unset($HTTP_SESSION_VARS['cart'][$prdnumber]);
     
            $HTTP_SESSION_VARS['total_price'] = calculate_price($HTTP_SESSION_VARS['cart']); 
            $HTTP_SESSION_VARS['items'] = calculate_items($HTTP_SESSION_VARS['cart']); 
        } 
    }

    BTW, you've some errors in shoe size drop down list, check the html output to see what I mean.

    Now that makes output_cart.php different again:
    Code php:
    / declare the session variables we want access to inside the function 
    <?
      global $HTTP_SESSION_VARS;
     
      if(!$HTTP_SESSION_VARS['items']) $HTTP_SESSION_VARS['items'] = '0';
      if(!$HTTP_SESSION_VARS['total_price']) $HTTP_SESSION_VARS['total_price'] = '0.00';
     
    function display_cart($cart, $change = true, $images = 1)
    {
      // display items in shopping cart
      // optionally allow changes (true or false)
      // optionally include images (1 - yes, 0 - no)
     
      global $HTTP_SESSION_VARS;
     
      echo '<table border = 0 width = 100% cellspacing = 0>
            <form action = show_cart.php method = post>
            <tr>
            <th colspan = '. (1+$images) .' bgcolor="#cccccc">Item</th>
            <th bgcolor="#cccccc">Price</th>
            <th bgcolor="#cccccc" align="left">Quantity</th>
    <th bgcolor="#cccccc" align="left">Shoesize</th>';
          echo'  <th bgcolor="#cccccc">Total</th></tr>';
     
      foreach ($cart as $prdnumber => $item)
      {
          $qty=count($item['qty']);
          $shoesize=$item['shoesize'];
     
            $products = get_book_details($prdnumber);
     
            echo '<tr>';
     
            if($images ==true)
            {
              echo '<td align = left width=5%>';
     
              if (file_exists("images/$prdnumber.jpg"))
              {
                 $size = GetImageSize('images/'.$prdnumber.'.jpg');  
                 if($size[0]>0 && $size[1]>0)
                 {
                   echo '<img src="images/'.$prdnumber.'.jpg" border=0 ';
                   echo 'width = '. $size[0]/3 .' height = ' .$size[1]/3 . '>';
                 }
              } else {
                 echo ' ';
              }
              echo '</td>';
            }
            echo '<td align = left>'.$products['title'].'</a></td>';
     
            echo '<td align = left>'.number_format($products['price'], 2).'</td>';
     
            echo '<td align = left>';
     
     
            // if we allow changes, quantities are in text boxes
            if ($change == true)
                echo "<input type = text name = \"$prdnumber\" value = \"$qty\" size = 3 align = left>";
            else
                echo $qty.'</td>';
     
            if ($change == true)
                echo "<td align = left><input type = text name = \"$prdnumber\" value = \"$shoesize\" size = 3 align = left>";
            else
                echo $qty.'</td>';
            // echo"<input type = text name = \"$prdnumber\" value = \"$qty\" size = 3 align = left></td>";
            echo '<td align = left>'.number_format($products['price']*$qty,2)."</td></tr>\n";
     
          // display total row
          echo '<tr>
                  <th colspan = '. (2+$images) .' bgcolor=#cccccc>
                  <th align = left bgcolor=cccccc> ';
     
         echo  $HTTP_SESSION_VARS['items'];
         echo' </th>';
     
         echo' <th align = left bgcolor=cccccc> ';
         echo  $shoesize;
         echo' </th>';
     
          echo'<th align = left bgcolor=#cccccc>
              '; 
          echo number_format($HTTP_SESSION_VARS['total_price'], 2);
          echo'</th>
        </tr>';
     
     
          // display save change button
          if($change == true)
          {
            echo '<tr>
                    <td colspan = '. (2+$images) .'> </td>
                    <td align = center>
                      <input type = hidden name = save value = true>  
                      <input type = image src = "images/save-changes.gif" 
                             border = 0 alt = "Save Changes">
                    </td>
                    <td> </td>
                </tr>';
          }
          echo '</form></table>';
        }
    }

    And the functions:
    Code php:
    <?
    function calculate_price($cart)
    {
      // sum total price for all items in shopping cart
      $price = 0.0;
      if(is_array($cart))
      {
        $conn = db_connect();
        foreach($cart as $prdnumber => $item)
        {  
          $query = "select price from products where prdnumber='$prdnumber'";
          $result = mysql_query($query);
          if ($result)
          {
            $item_price = mysql_result($result, 0, 'price');
            $price +=$item_price*$item['qty'];
          }
        }
      }
      return $price;
    }
     
     
     
    function calculate_items($cart)
    {
      // sum total items in shopping cart
      $items = 0;
      if(is_array($cart))
      {
        foreach($cart as $prdnumber => $qty)
        {  
          $items += $item['qty'];
        }
      }
      return $items;
    }
    ?>

    It's actually not so different from what you had in the beginning at all. You had the cart array elements hold quantity, not it holds an array of quantity and shoe size.

    P.S. You should really use $_GET and $_SESSION variables
    Saul

  20. #20
    SitePoint Evangelist
    Join Date
    Apr 2005
    Location
    London, UK
    Posts
    505
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    haha...the error message is gone, but still when we update the shoesize it updates the quantity its wiered? ??

    the shoesize list was a mistake i took it off now...it should onle be updated when you add a product to the cart

    i took lot of your time...many many thanks so far...any more help would be greatly appriciated, but you done more then enough...

    dug

  21. #21
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Well, that's because you use the same name for quantity and shoe size fields.
    Saul


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •