pauldonnelly23 Posted February 16, 2010 Share Posted February 16, 2010 Hi, I hope you can help. I have a form with about 15 checkboxes. I am using the following code to count the checkboxes and multiply it by £200. My problem is two of the 15 checkboxes have different prices and I dont know how to add them to the price. Current Code: $checkboxCount = isset($_POST['course1']) ? count($_POST['course1']) : 0; $checkboxCount1 = isset($_POST['course2']) ? count($_POST['course2']) : 0; $checkboxCount2 = isset($_POST['course3']) ? count($_POST['course3']) : 0; $checkboxCount3 = isset($_POST['course4']) ? count($_POST['course4']) : 0; $countchecked = $checkboxCount+$checkboxCount1+$checkboxCount2+$checkboxCount3; $total = $countchecked*200; I hope you understand what I need and I hope you can help. Kind regards, Paul Quote Link to comment https://forums.phpfreaks.com/topic/192279-form-calculation/ Share on other sites More sharing options...
aeroswat Posted February 16, 2010 Share Posted February 16, 2010 $checkboxCount = isset($_POST['course1']) ? count($_POST['course1']) : 0; $checkboxCount1 = isset($_POST['course2']) ? count($_POST['course2']) : 0; $checkboxCount2 = isset($_POST['course3']) ? count($_POST['course3']) : 0; $checkboxCount3 = isset($_POST['course4']) ? count($_POST['course4']) : 0; $countchecked = $checkboxCount+$checkboxCount1+$checkboxCount2+$checkboxCount3; $checkBoxCountExtra = $extracheckone * $price + $extrachecktwo * $pricetwo; $total = $countchecked*200 + $checkBoxCountExtra; That should do it Quote Link to comment https://forums.phpfreaks.com/topic/192279-form-calculation/#findComment-1013263 Share on other sites More sharing options...
pauldonnelly23 Posted February 16, 2010 Author Share Posted February 16, 2010 Thanks for your prompt reply Areoswat. The checkboxes are in an array. e.g. course1, course2, course3 and course4. Will your code work for this? How do I specify a specific checkbox from an array? Quote Link to comment https://forums.phpfreaks.com/topic/192279-form-calculation/#findComment-1013264 Share on other sites More sharing options...
aeroswat Posted February 16, 2010 Share Posted February 16, 2010 Thanks for your prompt reply Areoswat. The checkboxes are in an array. e.g. course1, course2, course3 and course4. Will your code work for this? How do I specify a specific checkbox from an array? I guess i'm sort of confused. course1, course2, course3 and course4 are sets of checkboxes? That would explain why you are counting them Quote Link to comment https://forums.phpfreaks.com/topic/192279-form-calculation/#findComment-1013267 Share on other sites More sharing options...
aeroswat Posted February 16, 2010 Share Posted February 16, 2010 If what I said was the case then after you do your calculations you need to check to see if the value of each of the checkboxes that have different prices are a part of whichever array they belong to. If they are then you need to decrement that count by 1 and add the price of whatever it is to the total. Do that for each of the two. Quote Link to comment https://forums.phpfreaks.com/topic/192279-form-calculation/#findComment-1013270 Share on other sites More sharing options...
Psycho Posted February 16, 2010 Share Posted February 16, 2010 It looks to me that "most" of the checkboxes represent items that each cost £200. That would explain why you are counting up all the checked items and multiplying by 200. Are you even using the value of the checkboxes? If no, then use the value for the prices of the checkboxes and then sum up all the values to get the total price. If you are using the values of the checkboxes for some other purpose, then I have to assume you have a way of knowing what the cost of each item is that you can tie back to the actual value (e.g. title?). You would want to determine the price individually by looking up each checked value. Quote Link to comment https://forums.phpfreaks.com/topic/192279-form-calculation/#findComment-1013274 Share on other sites More sharing options...
pauldonnelly23 Posted February 18, 2010 Author Share Posted February 18, 2010 If they are then you need to decrement that count by 1 and add the price of whatever it is to the total. Do that for each of the two. Aeroswat, how would i go about this? The ids of the two checkboxes in the array are: pap4cbcs (which has a price of 270) pap4fau (which has a price of 70) The code im using to count at the minute is: $checkboxCount = isset($_POST['course1']) ? count($_POST['course1']) : 0; $checkboxCount1 = isset($_POST['course2']) ? count($_POST['course2']) : 0; $checkboxCount2 = isset($_POST['course3']) ? count($_POST['course3']) : 0; $checkboxCount3 = isset($_POST['course4']) ? count($_POST['course4']) : 0; $countchecked = $checkboxCount+$checkboxCount1+$checkboxCount2+$checkboxCount3; $total = $countchecked*200; If you can help, id be very grateful. kind regards, Paul Quote Link to comment https://forums.phpfreaks.com/topic/192279-form-calculation/#findComment-1014362 Share on other sites More sharing options...
Psycho Posted February 18, 2010 Share Posted February 18, 2010 You cannot determine the ID of a checkbox in the POST data - only the Values. What are the values of the checkboxes? It would be very helpful if you showed the form that creates the checkboxes on the form. As I stated previously, you should be calculating the cost more explicitly instead of just counting the checkboxes. Quote Link to comment https://forums.phpfreaks.com/topic/192279-form-calculation/#findComment-1014423 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.