co.ador Posted February 10, 2010 Share Posted February 10, 2010 I looking to sum three values of the price field if selected. $sql = "SELECT * FROM shoes WHERE tray on tray.product_id = shoes.id"; $sth = mysql_query($sql); $variety = mysql_fetch_array($sth)) { echo'<div> <p>'. $variety['name']. '</p> <p class="price">'. $variety['price']. '</p> <input name="price" type="checkbox" value="" /> </div> <div> <p>'. $variety['name']. '</p> <p class="price">'. $variety['price']. '</p> <input name="price" type="checkbox" value="" /> </div> <div> <p>'. $variety['name']. '</p> <p class="price">'. $variety['price']. '</p> <input name="price" type="checkbox" value="" /> </div> } if you notice each iteration will have a checkbox input and users can choose one or the three if they one. Now how can I sum how if user chooses more than one price? Quote Link to comment Share on other sites More sharing options...
Catfish Posted February 10, 2010 Share Posted February 10, 2010 you are naming all the price values 'price'. this means you will not get the three values passed to the script that handles the form but only the last one (afaik). first step is to name the variables different like price01, price02, price03 so the script can hold three values not one. you could then, in the script that handles the form, use functions like is_null, is_float, is_int, is_numeric to check if any combination of the values are set and are valid etc. then work upon the values (sum them up) http://www.php.net/manual/en/ref.var.php Quote Link to comment Share on other sites More sharing options...
co.ador Posted February 10, 2010 Author Share Posted February 10, 2010 thank you catfish I am using another approach like a tree like array where each price is in one row instead of different fields in a row. I am doing it like that because I am using other tables which has the same fields names. so far I have this foreach($product['varieties'] as $variety){ <p>' . $variety['price']. '</p> <p class="price">' . $variety['price']. '</p> $price = $variety['price']; $total = 0; foreach( $price as $p ) { $total += $p; } echo $total; } But There is an argument which is failing and it's probably the definition of price which fails at the foreach. The code below is a representatino of what I have don't know what's could be wrong int he set up. Error is as: Warning: Invalid argument supplied for foreach() in /home3/stores/public_html/example2.php on line 303 0 Quote Link to comment Share on other sites More sharing options...
Catfish Posted February 10, 2010 Share Posted February 10, 2010 what is $product['varieties']? where does it come from? how does it get created? from your above code i can detect that you are not aware of what $product['price'] is. You are treating it like an array, but also like a string which is going to fail at at least one point in your code (the foreach error) which leads me to believe it is _not_ an array. so, why are you trying to use it like it is an array? Quote Link to comment Share on other sites More sharing options...
Catfish Posted February 10, 2010 Share Posted February 10, 2010 sorry not $product['price'] i meant $variety['price'] Quote Link to comment Share on other sites More sharing options...
co.ador Posted February 10, 2010 Author Share Posted February 10, 2010 I am using an array inside of an array $variety comes from the array $products. $sql ="SELECT * FROM product, tray WHERE product.colon_id=" .(int) $_GET['menu']. " AND tray.product_id = product.id"; $sth = mysql_query($sql); while($row = mysql_fetch_array($sth)) { $idc = $row['id']; $tree[$idc]['name'] = $row['name']; if($row['variety']) $tree[$idc]['varieties'][] = $row; } foreach($tree as $product){ echo'<p class="name">Name:</p><p class="p">'. $product['name'] . '</p>'; foreach($product['varieties'] as $variety) { <input style="width:10px; margin-left:9px; " name="price[]" type="checkbox" value="" /> <p class="price">' . $variety['price']. '</p> $price = $variety['price']; $total = 0; foreach( $price as $p ) { $total += $p; } echo $total; } } } Quote Link to comment Share on other sites More sharing options...
co.ador Posted February 10, 2010 Author Share Posted February 10, 2010 price comes from the table tray. Quote Link to comment Share on other sites More sharing options...
co.ador Posted February 10, 2010 Author Share Posted February 10, 2010 check the checkbox input inside the second foreach which will be use to checked the price and then sum it. Quote Link to comment Share on other sites More sharing options...
Catfish Posted February 10, 2010 Share Posted February 10, 2010 foreach($product['varieties'] as $variety) { <p>' . $variety['price']. '</p> <p class="price">' . $variety['price']. '</p> $price = $variety['price']; $total = 0; foreach( $price as $p ) { $total += $p; } echo $total; is this a direct copy and paste? you are not escaping from php to do your HTML codes (<p> etc.) these lines: <p class="price">' . $variety['price']. '</p> $price = $variety['price']; foreach( $price as $p ) do not make sense. if $variety['price'] is an array, you are trying to output the entire array (incorrect) then loop through it (correct). If it is not an array (string/int/some other printable type) you are trying to output it (correct) then loop through it like an array (incorrect). Do a print_r($price) and paste the results here. Quote Link to comment Share on other sites More sharing options...
teamatomic Posted February 10, 2010 Share Posted February 10, 2010 Looking at your original post. Your form is flawed. You have multiple check boxes with the same name and no value <input name="price" type="checkbox" value="" /> You need to give a checkbox a value to return when it is checked. If you want multile boxes selected you need to return an array, you do that by making name= and arrary name=price[] so what you need is <input name="price[]" type="checkbox" value="9.99" /> <input name="price[]" type="checkbox" value="12.99" /> <input name="price[]" type="checkbox" value="14.99" /> now when it is posted $_POST['price'] is an array with one or more elements. HTH Teamatomic Quote Link to comment Share on other sites More sharing options...
co.ador Posted February 10, 2010 Author Share Posted February 10, 2010 @teamatic i didn't understood the last point you have make, which is : now when it is posted $_POST['price'] is an array with one or more elements. Where should I refer as $_POST['price']? Well I have done a var_dump and print_r and it is printing the different values. Check this link. http://www.nyhungry.com/example2.php?subject=4&id=2®ister=&menu=38 Quote Link to comment Share on other sites More sharing options...
Catfish Posted February 10, 2010 Share Posted February 10, 2010 It is telling you the problem: Warning: Invalid argument supplied for foreach() in /home3/nyhungry/public_html/example2.php on line 297 00.00string(4) "0.00" the value you are using for the foreach loop on line 297 of file example2.php is invalid. On this iteration, the value was a string, with a value of "0.00". $price is not an array. it is a string value. you need to check your data and its formatting and rebuild your code to suit it or change the formatting of your data to suit your code. Quote Link to comment Share on other sites More sharing options...
co.ador Posted February 10, 2010 Author Share Posted February 10, 2010 $price = $variety['price']; $total = 0; foreach($price as $p ) { $total += $p; print_r($price); var_dump($price); } } $price as in the script above won't pass through the foreach so it means that var_dump or print when the $price variable is inside the for each loop. If I take the print_r and the var_dump outsie the foreach then $price will test as true and it will print or var dump a value which is in the case of the link I gave you guys above. $price = $variety['price']; $total = 0; foreach($price as $p ) { $total += $p; } print_r($price); var_dump($price); } Now I wonder why $price is not passing through the foreach loop. Quote Link to comment Share on other sites More sharing options...
co.ador Posted February 10, 2010 Author Share Posted February 10, 2010 don't worry about the second iteration I have it as NULL that's why it is displaying 0.00 But if you notice the first iteration do have values. So you say $price becomes an string value so therefore I have to iliminate the for each loop in this case. Quote Link to comment Share on other sites More sharing options...
co.ador Posted February 10, 2010 Author Share Posted February 10, 2010 Al so the third warning for large tray will display below and it will make you think it's the warning error for the first iteration in the second row.. Quote Link to comment Share on other sites More sharing options...
teamatomic Posted February 10, 2010 Share Posted February 10, 2010 $variety = mysql_fetch_array($sth)) { echo'<div> <p>'. $variety['name']. '</p> <p class="price">'. $variety['price']. '</p> <input name="price" type="checkbox" value="" /> </div> <div> <p>'. $variety['name']. '</p> <p class="price">'. $variety['price']. '</p> <input name="price" type="checkbox" value="" /> </div> <div> <p>'. $variety['name']. '</p> <p class="price">'. $variety['price']. '</p> <input name="price" type="checkbox" value="" /> </div> } You pull an array out of your DB. Check with a print_r is it really returns an array. If it does then you have to look at how you are building your form. As it stands each checkbox is identical. you need yo make them reflect the value of your array(if it is in fact an array). <b>So the first thing in order to fix your problem is to print_r $variety and post the results.</b> HTH Teamatomic Quote Link to comment Share on other sites More sharing options...
Catfish Posted February 10, 2010 Share Posted February 10, 2010 So you say $price becomes an string value so therefore I have to iliminate the for each loop in this case. this is correct. remove the inner foreach loop, as you don't need it to sum a total of the price values. Quote Link to comment Share on other sites More sharing options...
co.ador Posted February 10, 2010 Author Share Posted February 10, 2010 @teamatomic The code you have post in your last post is not the one, I have post the wrong code in the first post and as you say it won't pull an array out of the database. I did a mistake the real code comes from reply #5, variety comes from products as I said and variety is an array inside of the products array. you can go the this link the one I posted before http://www.nyhungry.com/example2.php?subject=4&id=2®ister=&menu=38 check the print_r. It will be kind of confusing since I have quite amount of fields plus remember it will list the products array then inside the variery array that will contain most of the fields. @catfish I will eliminate the foreach and move into carrying this value to cart.php on #reply 5 am i missing the <form> tags? will I have to put some form tags around the input tags? Quote Link to comment Share on other sites More sharing options...
Catfish Posted February 10, 2010 Share Posted February 10, 2010 yes you need <form> tags and they will need to specify the script to handle the form data that is posted. Quote Link to comment Share on other sites More sharing options...
co.ador Posted February 10, 2010 Author Share Posted February 10, 2010 by the script to handle you mean the action form? action=cart.php? Quote Link to comment Share on other sites More sharing options...
Catfish Posted February 10, 2010 Share Posted February 10, 2010 yes Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.