SitePoint Sponsor

User Tag List

Results 1 to 15 of 15
  1. #1
    SitePoint Addict AshleyH's Avatar
    Join Date
    Mar 2005
    Posts
    260
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Adding up form variables

    I have a form which has a selection of checkboxes on. Each checkbox obviously has it's own numeric value.

    I have found a way to submit the form and add up the values to give me a total but it seems like there should be a more efficient way of doing it.

    At the moment I pass the form value to addup.php which has this code:

    <?php
    $item1 = $_GET['item1'];
    $item2 = $_GET['item2'];
    $item3 =$_GET['item3'];
    $total = $item1 + $item2 + $item2;
    ?>

    What I'm struggling with is...there are a lot of checkboxes on the form which people may select which means this script would be quite long with a to accommodated each checkbox.

    The other element I'm struggling with is if somebody doesn't need 'item2' and only selects 'item1' and 'item2' - will I need to use an if/else statement here?

    As I'm sure you can gather I'm a newbie with php so any help appreciated.

    Many thanks

  2. #2
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,087
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Are you also interested in which checkboxes are checked, or only how many checkboxes are checked?
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  3. #3
    SitePoint Addict AshleyH's Avatar
    Join Date
    Mar 2005
    Posts
    260
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It will be which ones are checked.

  4. #4
    SitePoint Wizard Ren's Avatar
    Join Date
    Aug 2003
    Location
    UK
    Posts
    1,060
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you give all the checkboxes the same name, item[], it will create an array of the values when submitted.

    Then working out the sum is...
    PHP Code:
    $total array_sum($_GET['item']); 

  5. #5
    SitePoint Addict AshleyH's Avatar
    Join Date
    Mar 2005
    Posts
    260
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    We do need to know what items have been passed through - it's a list where people select the items they want to order and an email is sent through with a list of items.

    Many thanks

  6. #6
    SitePoint Wizard Ren's Avatar
    Join Date
    Aug 2003
    Location
    UK
    Posts
    1,060
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by AshleyH View Post
    We do need to know what items have been passed through - it's a list where people select the items they want to order and an email is sent through with a list of items.

    Many thanks
    Ah, K.

    So name them, item[0], item[1], ... item[n]

    the array_sum() will still work, and can still check if an item has been checked by isset($_GET['item'][$i]))

  7. #7
    SitePoint Addict AshleyH's Avatar
    Join Date
    Mar 2005
    Posts
    260
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for coming back to me again.

    Being an ultra newbie - is there anyway you could give me an example of how I would use the array_sum() and the isset?

    Many thanks

  8. #8
    SitePoint Wizard Ren's Avatar
    Join Date
    Aug 2003
    Location
    UK
    Posts
    1,060
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by AshleyH View Post
    Thanks for coming back to me again.

    Being an ultra newbie - is there anyway you could give me an example of how I would use the array_sum() and the isset?

    Many thanks
    PHP Code:
    array_sum($_GET['item']) 
    Will return the sum of all the checked items.

  9. #9
    SitePoint Addict AshleyH's Avatar
    Join Date
    Mar 2005
    Posts
    260
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm getting an error:

    array_sum() [function.array-sum]: The argument should be an array in...

    Sorry, like I say - I'm a complete newbie to php.

  10. #10
    SitePoint Wizard Ren's Avatar
    Join Date
    Aug 2003
    Location
    UK
    Posts
    1,060
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmm.

    Does

    PHP Code:
    $total array_sum($_POST[''item']) 
    work?

    If the HTML form containing the checkboxes, has an attribute method with value of "POST", then using $_POST instead of $_GET would be correct.

  11. #11
    SitePoint Member JAY6390's Avatar
    Join Date
    Oct 2009
    Location
    UK
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code above should read
    PHP Code:
    $total array_sum($_POST['item']); 
    (note the double apostrophe error)

  12. #12
    Spirit Coder allspiritseve's Avatar
    Join Date
    Dec 2002
    Location
    Ann Arbor, MI (USA)
    Posts
    648
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by AshleyH View Post
    I'm getting an error:

    array_sum() [function.array-sum]: The argument should be an array in...

    Sorry, like I say - I'm a complete newbie to php.
    Can you show us what your HTML code looks like for the checkboxes?

  13. #13
    SitePoint Addict AshleyH's Avatar
    Join Date
    Mar 2005
    Posts
    260
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Checkboxes on the form:

    <input name="ROSE" type="checkbox" id="ROSE" value="120" />
    <input name="RED" type="checkbox" id="RED" value="110" />
    <input name="WHITE" type="checkbox" id="WHITE" value="110" />

    The next page is where we need to add these form fields up.

    It's the next page where I need to take all the check boxes that a user has selected and add up to give me the $total.

    But I'm struggling with the part that checks to see which checkboxes have been passed.

    Clients can select 'rose', 'red' or 'rose' or any combination of them.

    Thanks for reading!

  14. #14
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <input name="item[rose]" type="checkbox" id="ROSE" value="120" />
    <
    input name="item[red]" type="checkbox" id="RED" value="110" />
    <
    input name="item[white]" type="checkbox" id="WHITE" value="110" /> 
    Then you can use the above to get the sum - if all are checked in the above, it will return 340.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  15. #15
    SitePoint Guru bronze trophy TomB's Avatar
    Join Date
    Oct 2005
    Location
    Milton Keynes, UK
    Posts
    989
    Mentioned
    9 Post(s)
    Tagged
    2 Thread(s)
    By adding those up you'll have no way to know which ones were checked (you'll only have the total)

    You may be better off using bit flags for the values, e.g.

    PHP Code:
    <input name="item[rose]" type="checkbox" id="ROSE" value="1" />
    <
    input name="item[red]" type="checkbox" id="RED" value="2" />
    <
    input name="item[white]" type="checkbox" id="WHITE" value="4" /> 
    Then add them up:

    PHP Code:
    $total 0;
    foreach (
    $_POST['items'] as $value) {
        
    $total |= $value;

    Now you can do:

    PHP Code:
    //Was rose checked?
    if ($total) {
    //...
    }

    //were both red and white checked?
    if ($total) { 
    //..
    }

    //were all 3 checked?
    if ($total) {
    //..

    It also provides a convenient way to store the selection in a single database field.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •