Jump to content

Shopping Cart Question with Cookies!


wkohusjr

Recommended Posts

I'm passing cookies from one page to another.  Then I'm taking the value of a drop down menu (for quantity) the Price (a cookie) and doing the math to get the total.  My submit buttons will calculate and print the total into a <td>. 

 

My issue is when I have two items once I click submit it will remove the value of the one and calculate and print the other.

 

Any help would be greatly appreciated!

                global $tax;
                $tax = .06;
                
                if (isset($_COOKIE['shoe1'])) {
                    print "<tr><td>";

                    print "<img src=\"Images/1.jpg\" width=\"100px\" height=\"75px\">";

                    print "</td><td>";

                        echo $_COOKIE['shoe1']['Description'];

                    print "</td><td>";

                        echo $_COOKIE['shoe1']['Item#'];

                    print "</td><td>";

                        echo "$" . $_COOKIE['shoe1']['Price'];

                    print "</td><td>
                    
                        <form action=\"cart.php\" method=\"POST\">
                            <select name=\"dropdown1\" id=\"dropdown1\">
                                <option value=\"1\">1</option>
                                <option value=\"2\">2</option>
                                <option value=\"3\">3</option>
                                <option value=\"4\">4</option>
                            </select>";
                            print "<input type=\"submit\" name=\"Calculate\" value=\"Calculate\">
                            
                        </form></td>
                        
                    <td style=\"align:center;\" width=\"100px\">";
                    
                    $amount = $_POST['dropdown1'];
                    
                    $price = $_COOKIE['shoe1']['Price'];
                    
                    $total = $price * $amount + $tax;
//                     
                    echo $total;
                    
                    
                    print "</td></tr>";

                }

                if (isset($_COOKIE['shoe2'])) {
                    print "<tr><td>";

                    print "<img src=\"Images/2.jpg\" width=\"100px\" height=\"75px\">";

                    print "</td><td>";

                        echo $_COOKIE['shoe2']['Description'];

                    print "</td><td>";

                        echo $_COOKIE['shoe2']['Item#'];

                    print "</td><td>";

                        echo "$" . $_COOKIE['shoe2']['Price'];

                    print "</td><td>
                    
                        <form action=\"cart.php\" method=\"POST\">
                            <select name=\"dropdown2\" id=\"dropdown2\">
                                <option value=\"1\">1</option>
                                <option value=\"2\">2</option>
                                <option value=\"3\">3</option>
                                <option value=\"4\">4</option>
                            </select>";
                    print "<input type=\"submit\" name=\"Calculate\" value=\"Calculate\">
                            
                        </form></td>
                        
                    <td style=\"align:center;\" width=\"100px\">";

                    $amount = $_POST['dropdown2'];

                    $price = $_COOKIE['shoe2']['Price'];

                    $total = $price * $amount + $tax;

                    echo $total;

                    print "</td></tr>";

                }

Link to comment
https://forums.phpfreaks.com/topic/280810-shopping-cart-question-with-cookies/
Share on other sites

I have not tested it...

global $tax;
				
                $tax = .06;
                
				
				//You should make each item added go into one cookie as an array
				//example say i add shoe 2 and three this is what is could look like
				//$_COOKIE['Products']['shoe1'] = array('Price' => '', 'Description' => '');
				//$_COOKIE['Products']['shoe2'] = array('Price' => '', 'Description' => '');
				
				//then you can run an foreach loop
				if(!empty($_COOKIE['Products'])){
					
					$price = 0; //scope
					$total = 0; //scope
					foreach($_COOKIE['Products'] as $Product => $Info){
						
						print "<tr><td>";

	                    print "<img src=\"Images/1.jpg\" width=\"100px\" height=\"75px\">";

	                    print "</td><td>";

	                        echo $Info['Description'];

	                    print "</td><td>";

	                        echo $Info['Item#'];

	                    print "</td><td>";

	                        echo "$" . $Info['Price'];

	                    print "</td><td>
	                    
	                        <form action=\"cart.php\" method=\"POST\">
	                            <select name=\"dropdown1\" id=\"dropdown1\">
	                                <option value=\"1\">1</option>
	                                <option value=\"2\">2</option>
	                                <option value=\"3\">3</option>
	                                <option value=\"4\">4</option>
	                            </select>";
	                            print "<input type=\"submit\" name=\"Calculate\" value=\"Calculate\">
	                            
	                        </form></td>
	                        
	                    <td style=\"align:center;\" width=\"100px\">";
	                    
	                    $amount = $_POST['dropdown1'];
	                    
	                    $price .= $Info['Price'];
	                    
	                    $total .= $price * $amount + $tax;  
	                      
	                    print "</td></tr>";
						
					}
					
				}
				
			}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.