Jump to content

PHP idea help


mds1256

Recommended Posts

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

<?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

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.