mds1256 Posted September 29, 2009 Share Posted September 29, 2009 Im in the middle of setting up a car configurator website and just wondering how i can accomplish(sp) this. My idea is that there will be a running costing total at the bottom of the page, then above that there will be option that you can add and remove e.g. add metallic paint + £250. What would be the easiest way to code this. My idea was to add each option as a variable and just use the GET to retrieve this from the url so it would be page.php?paint=yes&cost=250 but cost would be a running total but just retrieved from the URL. But i have realised that there are around 60 different options so it would be a nightmare to code this any ideas to make it easier Quote Link to comment https://forums.phpfreaks.com/topic/175910-php-idea-help/ Share on other sites More sharing options...
Alex Posted September 29, 2009 Share Posted September 29, 2009 What would the options be check boxes? If that's possible for what you're wanting to do that's what I'd do because you could do it like this: <input type="checkbox" name="option[]" value="paint,250" /> <input type="checkbox" name="option[]" value="something,200" /> session_start(); foreach($_POST['option'] as $option) { $values = explode(',' $option); $_SESSION['prices'][] = $values[1]; $_SESSION['items'][] = $values[0]; } echo array_sum($_SESSION['prices']); // Total Quote Link to comment https://forums.phpfreaks.com/topic/175910-php-idea-help/#findComment-926948 Share on other sites More sharing options...
mds1256 Posted September 29, 2009 Author Share Posted September 29, 2009 What would the options be check boxes? If that's possible for what you're wanting to do that's what I'd do because you could do it like this: <input type="checkbox" name="option[]" value="paint,250" /> <input type="checkbox" name="option[]" value="something,200" /> session_start(); foreach($_POST['option'] as $option) { $values = explode(',' $option); $_SESSION['prices'][] = $values[1]; $_SESSION['items'][] = $values[0]; } echo array_sum($_SESSION['prices']); // Total yeah i think that is what i want but when coping your code i get Parse error: parse error, unexpected T_VARIABLE in c:\inetpub\wwwroot\array.php on line 12 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Untitled Document</title> </head> <body><?php session_start(); foreach($_POST['option'] as $option) { $values = explode(',' $option); $_SESSION['prices'][] = $values[1]; $_SESSION['items'][] = $values[0]; } echo array_sum($_SESSION['prices'][]); // Total ?> <input type="checkbox" name="option[]" value="paint,250" /> <input type="checkbox" name="option[]" value="something,200" /> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/175910-php-idea-help/#findComment-926951 Share on other sites More sharing options...
Alex Posted September 29, 2009 Share Posted September 29, 2009 I edited my code for a small error (If you look at your quote you even quoted the fixed code). Quote Link to comment https://forums.phpfreaks.com/topic/175910-php-idea-help/#findComment-926953 Share on other sites More sharing options...
mds1256 Posted September 29, 2009 Author Share Posted September 29, 2009 I edited my code for a small error (If you look at your quote you even quoted the fixed code). not too sure what you mean as i have re-copied your code and still getting it. Am i missing something Quote Link to comment https://forums.phpfreaks.com/topic/175910-php-idea-help/#findComment-926956 Share on other sites More sharing options...
Alex Posted September 29, 2009 Share Posted September 29, 2009 Sorry. session_start(); foreach($_POST['option'] as $option) { $values = explode(',', $option); $_SESSION['prices'][] = $values[1]; $_SESSION['items'][] = $values[0]; } echo array_sum($_SESSION['prices']); // Total Btw, you can't have any output before session_start() or you'll get a headers already sent error. Quote Link to comment https://forums.phpfreaks.com/topic/175910-php-idea-help/#findComment-926957 Share on other sites More sharing options...
mds1256 Posted September 29, 2009 Author Share Posted September 29, 2009 <?php session_start(); foreach($_POST['option'] as $option) { $values = explode(',', $option); $_SESSION['prices'][] = $values[1]; $_SESSION['items'][] = $values[0]; } echo array_sum($_SESSION['prices']); // Total ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Untitled Document</title> </head> <body> <input type="checkbox" name="option[]" value="paint,250" /> <input type="checkbox" name="option[]" value="something,200" /> </body> </html> now i get Warning: Invalid argument supplied for foreach() in c:\inetpub\wwwroot\array.php on line 3 Warning: array_sum() [function.array-sum]: The argument should be an array in c:\inetpub\wwwroot\array.php on line 9 Quote Link to comment https://forums.phpfreaks.com/topic/175910-php-idea-help/#findComment-926958 Share on other sites More sharing options...
mds1256 Posted September 29, 2009 Author Share Posted September 29, 2009 sorry i forgot some additional coding lol. i see how this works but when submitting it unchecks the boxes so i have no way to remove that option <?php session_start(); foreach($_POST['option'] as $option) { $values = explode(',', $option); $_SESSION['prices'][] = $values[1]; $_SESSION['items'][] = $values[0]; } echo array_sum($_SESSION['prices']); // Total ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Untitled Document</title> </head> <body> <form action="array.php" onsubmit="array.php" method="post"> <p> <input type="checkbox" name="option[]" value="paint,250" /> <input type="checkbox" name="option[]" value="something,200" /> </p> <p> <label for="Submit"></label> <input type="submit" name="Submit" value="Submit" id="Submit" /> </p> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/175910-php-idea-help/#findComment-926962 Share on other sites More sharing options...
PFMaBiSmAd Posted September 29, 2009 Share Posted September 29, 2009 To allow you to uncheck an option, you need two things - 1) You need to display and propagate the current 'checked' value for each checkbox in the form, and 2) On each form submission, you need to loop through a list of the available checkboxes and for any that are not set, unset whatever data you are storing that indicates they are checked. Making a list someplace (database, array...) of the list of available checkboxes will also allow you to dynamically generate the checkboxes on the form so that you don't need to actually type a bunch of lines of HTML that you must edit every time you add, change, or remove a choice. Quote Link to comment https://forums.phpfreaks.com/topic/175910-php-idea-help/#findComment-926981 Share on other sites More sharing options...
Alex Posted September 29, 2009 Share Posted September 29, 2009 My example was just to give you an idea, not to actually use. If you want a more detailed explanation / example just ask. Quote Link to comment https://forums.phpfreaks.com/topic/175910-php-idea-help/#findComment-927206 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.