Page 1 of 1

Need Help adding up prices.

Posted: Thu Jan 26, 2006 1:02 pm
by stylus
Hello,

I am trying to figure out how to add up the total cost of my accessories, and assign it to the value of $total_accessory_cost. Then add that cost with 2 other values. I can add the other two no problem, because they are just plain input box's. The accessories are different because they are in an array, and I don't know how to have it say add up $price_$i then assign it to a value.

here is my code that submits the information:

Code: Select all

<?PHP 
      $field_count = $_POST['field_count'];
            for($i= 1;$i <= $field_count;++$i) 
	{ 
	   echo "
      				
			<tr>
                           <td align=left width=90><input type=text size=10 name=\"model_$i\" value=\"".$_POST['model_'.$i]."\"></td>
                           <td align=left width=90><input type=text size=40 name=\"description_$i\" value=\"".$_POST['description_'.$i]."\"></td>
                           <td align=right>$<input type=text size=10 name=\"price_$i\" value=\"".$_POST['price_'.$i]."\"></td>
	       </tr> ";
	};
?>

On the next page I only have this so far because I don't even know where to start with the accessories.

Code: Select all

<?PHP 
  
            $total_equipment_cost = $_POST['machine_base_price'] + $_POST['surge_price']  + $total_accessory_cost ;	
  
  ?>

Posted: Thu Jan 26, 2006 2:00 pm
by Chris Corbyn
Your design isn't too good for handling this... you could actually make you name fields arrays on the form (name="foo[]").

Code: Select all

for ($i=0; $i<count($_POST['model']); $i++)
{
    echo "
    <tr>
        <td align=left width=90><input type=text size=10 name=\"model[$i]\" value=\"$_POST[model][$i]\"></td>
        <td align=left width=90><input type=text size=40 name=\"description[$i]\" value=\"$_POST[description][$i]\"></td>
        <td align=right><input type=text size=10 name=\"price[$i]\" value=\"$_POST[price][$i]\"></td>
    </tr>";
}

Code: Select all

$total_accessory_cost = array_sum($_POST['price']);
$total_equipment_cost = $_POST['machine_base_price'] + $_POST['surge_price']  + $total_accessory_cost;
I haven't tested that but you see the idea :)

Posted: Fri Jan 27, 2006 8:17 am
by stylus
Thanks,

I always appreciate when somebody can look at my code and say thats the rookie way of doing it, here is something that will work better. I had to make a few edits, but your code worked very well.

Thanks Again.