What would be the best practice to add this fields

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?

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.";

Sometimes I just post to give an idea but definitely checking before posting need to get done, I have to be more specific.

thanks for the advice.

So,

@rajug

What I understand is to count the values price on the page where them has been pass to, in this case process.php.

Didn’t have that clear at all.

Have to say I am passing the values to a shopping cart, cart.php. So I don’t think the need of an else statement won’t be needed?

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.

It is up to you what to do with the else.

i will go to sleep and then tomorrow I will Analise the code better. I have been up since two days now.

Thanks.

Tomorrow.