Forum Moderators: coopster

Message Too Old, No Replies

Stuck on basic PHP - sum if

         

EmmaH

4:31 pm on May 26, 2010 (gmt 0)

10+ Year Member



Hi Everyone,

Sorry - I'm still fairly new to this.

I've been going through the manual and forums trying to piece together if I can do a sum of the values of variables where the checkbox in a table is checked. I've set up the variables as an array, and then assigned each with a name and numeric value, but when I try and do an if(isset($_POST....) it fails.

Can anyone point me in the right direction?

Thanks so much

Emma

xclamationdesign

5:36 pm on May 26, 2010 (gmt 0)

10+ Year Member



Hi,

Can you post the code you have tried?

EmmaH

6:03 pm on May 26, 2010 (gmt 0)

10+ Year Member



This is the code, please excuse my basic skills! thank you for replying!


<?php

$contact_form_price = 40;
$slideshow_gallery_price = 100;
$advanced_menu_price = 150;
$portfolio_basic_price = 200;
$portfolio_advanced_price = 250;
$googlemap_price = 300;
$additional_pages2_price = 350;
$additional_pages5_price = 400;
$registration_functionality_price = 450;
$product_database_price = 500;
$shopping_cart_paypal_price = 550;
$shopping_cart_other_price = 600;

$webservices["contactform"]["service"] = "Online Contact Form";
$webservices["contactform"]["price"] = "£40";

$webservices["slideshowgallery"]["service"] = "Slideshow / Gallery";
$webservices["slideshowgallery"]["price"] = "£50";

$webservices["advancedmenu"]["service"] = "Advanced Navigation Menu";
$webservices["advancedmenu"]["price"] = "£100";

$webservices["portfoliobasic"]["service"] = "Basic Portfolio";
$webservices["portfoliobasic"]["price"] = "£100";

$webservices["portfolioadvanced"]["service"] = "Advanced Functionality Portfolio";
$webservices["portfolioadvanced"]["price"] = "£200";

$webservices["googlemap"]["service"] = "Embedded Googlemap";
$webservices["googlemap"]["price"] = "£40";

$webservices["additionalpages2"]["service"] = "Two Additional Pages";
$webservices["additionalpages2"]["price"] = "£150";

$webservices["additionalpages5"]["service"] = "Five Additional Pages";
$webservices["additionalpages5"]["price"] = "£400";

$webservices["registration"]["service"] = "Registration Functionality";
$webservices["registration"]["price"] = "£400";

$webservices["productdatabase"]["service"] = "Product Database";
$webservices["productdatabase"]["price"] = "£500";

$webservices["shoppingcartpaypal"]["service"] = "Paypal Shopping Cart";
$webservices["shoppingcartpaypal"]["price"] = "£250";

$webservices["shoppingcartother"]["service"] = "Paypal Shopping Cart";
$webservices["shoppingcartother"]["price"] = "£500";

echo $webservices["slideshowgallery"]["service"].
" is ".$webservices["slideshowgallery"]["price"].
" to add on.";

if(isset($_POST['contactform']) {$contact_form=$webservices["contactform"]["price"];}

$total = $contact_form + $slideshow_gallery + $advanced_menu + $portfolio_basic + $portfolio_advanced + $googlemap + $Additional_pages2 + $Additional_pages5 + $Registration_functionality + $Product_database + $Shopping_cart_paypal + $Shopping_cart_other;
?>
<form action="services.php" method="post">
<table width="350" border="0" cellspacing="0" cellpadding="0">
<tr>
<th scope="col">&nbsp;</th>
<th scope="col">&nbsp;</th>
<th scope="col">&nbsp;</th>
</tr>
<tr>
<th scope="row"><?php echo $webservices["contactform"]["service"]; ?></th>
<td><?php echo $webservices["contactform"]["price"]; ?></td>
<td><input name="webservices[]" type="checkbox" value="contact form" id="contactform" /></td>
</tr>
<tr>
<th scope="row"><?php echo $webservices["slideshowgallery"]["service"]; ?></th>
<td><?php echo $webservices["slideshowgallery"]["price"]; ?></td>
<td><input name="webservices[]" type="checkbox" value="slideshowgallery" id="slideshowgallery" /></td>
</tr>
<tr>
<th scope="row"><?php echo $webservices["advancedmenu"]["service"]; ?></th>
<td><?php echo $webservices["advancedmenu"]["price"]; ?></td>
<td><input name="webservices[]" type="checkbox" value="advancedmenu" id="advancedmenu" /></td>
</tr>
<tr>
<th scope="row"><?php echo $webservices["portfoliobasic"]["service"]; ?></th>
<td><?php echo $webservices["portfoliobasic"]["price"]; ?></td>
<td><input name="webservices[]" type="checkbox" value="portfoliobasic" id="portfoliobasic" /></td>
</tr>
<tr>
<th scope="row"><?php echo $webservices["portfolioadvanced"]["service"]; ?></th>
<td><?php echo $webservices["portfolioadvanced"]["price"]; ?></td>
<td><input name="webservices[]" type="checkbox" value="portfolioadvanced" id="portfolioadvanced" /></td>
</tr>
<tr>
<th scope="row"><?php echo $webservices["googlemap"]["service"]; ?></th>
<td><?php echo $webservices["googlemap"]["price"]; ?></td>
<td><input name="webservices[]" type="checkbox" value="googlemap" id="googlemap" /></td>
</tr>
<tr>
<th scope="row"><?php echo $webservices["productdatabase"]["service"]; ?></th>
<td><?php echo $webservices["productdatabase"]["price"]; ?></td>
<td><input name="webservices[]" type="checkbox" value="productdatabase" id="productdatabase" /></td>
</tr>
<tr>
<th scope="row"><?php echo $webservices["shoppingcartpaypal"]["service"]; ?></th>
<td><?php echo $webservices["shoppingcartpaypal"]["price"]; ?></td>
<td><input name="webservices[]" type="checkbox" value="shoppingcartpaypal" id="shoppingcartpaypal" /></td>
</tr>
<tr>
<th scope="row"><?php echo $webservices["shoppingcartother"]["service"]; ?></th>
<td><?php echo $webservices["shoppingcartother"]["price"]; ?></td>
<td><input name="webservices[]" type="checkbox" value="shoppingcartother" id="shoppingcartother" /></td>
</tr>
</table>
</form>
<?php
print"$total";
?>

Matthew1980

6:58 pm on May 26, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there EmmaH,

As you are using the <form method="post"> approach, your form is missing a vital ingredient; The submit button, ie:-

<input type="submit" name="submit" value="add up!">

Once you have that placed in the code you can then see what you are posting. First thing as you will need to do is something like this:-

//This snippet goes into the receiving file that you
//specified in the action attribute (servies.php) so that
//the data is sent to the correct place :)

<?php
//Check to see if the form is submitted
if(isset($_POST['submit']) && ($_POST['submit'] == "add up!")){
//What ever is sent from form print to screen..
echo "<pre>";
print_r($_POST);
echo "</pre>";
}
?>

From that you can see what elements are set, then from that you can work out the rest.

Hope that helps you out ;)

Cheers,
MRb

[edited by: Matthew1980 at 7:12 pm (utc) on May 26, 2010]

EmmaH

7:08 pm on May 26, 2010 (gmt 0)

10+ Year Member



Thank you so much for your help! Just one question, what does the <pre> do?

Matthew1980

7:28 pm on May 26, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there EmmaH,

<pre> just means pre-formatted, just makes the data printed by the print_r() function more easy to read. So all I have done is encased the print_r($_POST) in the <pre> tags, Also, just specifying the $_POST global means that everything being sent from the form will be printed for you to scrutinise - an excellent debugging method IMO.

Cheers,
MRb

EmmaH

8:02 pm on May 26, 2010 (gmt 0)

10+ Year Member



Fantastic, thanks again,

Emma