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?
This is what I was talking about in the last thread – you’ve come with a bit of code that doesn’t do anything, and a question about how you can fix it, but it can’t be fixed, because the whole premise is wrong.
Back up and read some tutorials on building forms with arrays of checkboxes, reading checkboxes in PHP, etc. There’s no way this form here can be used to do anything. You need a way to associate the checked boxes with what variety they correspond to. That query is not valid SQL, and based on your other post, this code would not give you the 3 names/prices for a product’s varieties.
I saw two things really strange for me, one is the SQL and the other is "$variety = mysql_fetch_array($sth)) { ". Do you think that there is some iteration happening what you are saying? As Dan said, your code is full of errors.
Try to analyze the following code to generate a form with checkbox array for price:
$sql = "SELECT
shoes.*
FROM
shoes
INNER JOIN tray ON tray.product_id = shoes.id";
$result = mysql_query($sql) or die(mysql_error());
if(mysql_num_rows($result) >= 1){ // if there are rows returned
while($row = mysql_fetch_array($result)){
echo '
<div>
<p>' . $variety['name'] . '</p>
<p class="price">' . $variety['price'] . '</p>
<input name="price[]" id="price_' . $variety['id'] . '" type="checkbox" value="' . $variety['price'] . '" />
</div>';
}
}
And when you post/submit the form (lets say you post the form to process.php) now write the following code in process.php and see:
$prices_checked = $_POST['price'];
if(count($prices_checked) >= 1)
echo array_sum($prices_checked);
else
echo "No prices were checked.";
How to manage and handle in processing that is up to you. You can store cart item id in the check box or like what i have store price itself in the check box. What is not clear here. I haven’t counted the price but array_sum function will sum up the price of checked boxes in the form.