Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    Regular Coder
    Join Date
    Jun 2008
    Posts
    104
    Thanks
    71
    Thanked 0 Times in 0 Posts

    Question Return number based on how many checkboxes are selected

    Is it possible to return a number based on the number (or range) of checkboxes that are selected?

    For example, if I have 20 checkboxes and only 10 are selected, it would echo "5". If all 20 checkboxes are selected it would echo "10".

    Thanks!

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,865
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    For example, if I have 20 checkboxes and only 10 are selected, it would echo "5". If all 20 checkboxes are selected it would echo "10".
    Is your logic to echo the half of the selected items?
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • Users who have thanked abduraooft for this post:

    four0four (02-15-2010)

  • #3
    Regular Coder
    Join Date
    Jun 2008
    Posts
    104
    Thanks
    71
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by abduraooft View Post
    Is your logic to echo the half of the selected items?
    Yes, that's correct. Not exactly half all the time, but if I could "specify" a number to echo based on the range of checkboxes selected.

    Something like:

    If 5-10 checkboxes are selected echo a variable called $number1.

    If 10-15 checkboxes are selected echo a variable called $number2.

  • #4
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,865
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    Use simple if-else-if, like
    PHP Code:
    if($count<=5)
     
    //echo
    elseif($count>&& $count<=10)
     
    //echo
    elseif($count>10 && $count<=15)
    //echo
    //and so on 
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • Users who have thanked abduraooft for this post:

    four0four (02-15-2010)

  • #5
    Regular Coder
    Join Date
    Jun 2008
    Posts
    104
    Thanks
    71
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by abduraooft View Post
    Use simple if-else-if, like
    PHP Code:
    if($count<=5)
     
    //echo
    elseif($count>&& $count<=10)
     
    //echo
    elseif($count>10 && $count<=15)
    //echo
    //and so on 
    What is $count?

    I'm confused as to how I would calculate the number (range) of checkboxes selected.

    Thanks again!

  • #6
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,865
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    I'm confused as to how I would calculate the number (range) of checkboxes selected.
    Could you post your HTML?
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • Users who have thanked abduraooft for this post:

    four0four (02-15-2010)

  • #7
    Regular Coder
    Join Date
    Jun 2008
    Posts
    104
    Thanks
    71
    Thanked 0 Times in 0 Posts
    So far, I just have the basics:

    Code:
    <input type="checkbox" name="checkbox1">
    <input type="checkbox" name="checkbox2">
    <input type="checkbox" name="checkbox3">
    <input type="checkbox" name="checkbox4">
    <input type="checkbox" name="checkbox5">
    <input type="checkbox" name="checkbox6">
    <input type="checkbox" name="checkbox7">
    <input type="checkbox" name="checkbox8">
    <input type="checkbox" name="checkbox9">
    <input type="checkbox" name="checkbox10">
    To make things simple, I just have 10 checkboxes. Let's say only 5 of those checkboxes (in any order) are selected, it would echo a variable called $number1.

    If say, 8 of those checkboxes (in any order) are selected, it would echo a variable called $number2.

    ...and so on.

    I hope that makes sense.

  • #8
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Code:
    <input type="checkbox" name="checkbox[1]">
    <input type="checkbox" name="checkbox[2]">
    <input type="checkbox" name="checkbox[3]">
    <input type="checkbox" name="checkbox[4]">
    <input type="checkbox" name="checkbox[5]">
    <input type="checkbox" name="checkbox[6]">
    <input type="checkbox" name="checkbox[7]">
    <input type="checkbox" name="checkbox[8]">
    <input type="checkbox" name="checkbox[9]">
    <input type="checkbox" name="checkbox[10]">
    Then, in the processing section:

    Code:
    $count = ((isset($_POST['checkbox']) && !empty($_POST['checkbox'])) ? count($_POST['checkbox']) : 0);
    Last edited by MattF; 02-13-2010 at 09:25 AM.

  • Users who have thanked MattF for this post:

    four0four (02-15-2010)

  • #9
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,865
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    If you follow MattF's code, you may even avoid all integer indexes from the array notation checkbox[], like
    Code:
    <input type="checkbox" name="checkbox[]">
    ....................
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • Users who have thanked abduraooft for this post:

    four0four (02-15-2010)

  • #10
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Quote Originally Posted by abduraooft View Post
    If you follow MattF's code, you may even avoid all integer indexes from the array notation checkbox[], like
    Code:
    <input type="checkbox" name="checkbox[]">
    ....................
    God knows why, but I do that same trick every time with html arrays. Never with php arrays, just html. I will stop numbering them one day.

  • #11
    Senior Coder Len Whistler's Avatar
    Join Date
    Jul 2002
    Location
    Vancouver, BC Canada
    Posts
    1,323
    Thanks
    26
    Thanked 100 Times in 100 Posts
    Here is something to work with. Untested and might have syntax errors.



    PHP Code:
    $post_result $_POST['checkbox'];

    $counter 1;
    foreach (
    $post_result as $value) {
    echo 
    "$value";
    $counter++;
    }

    echo 
    "Total checkboxes checked: $counter";


    // Below is part of form to be posted to code above.

    $num_checkboxes 20;
    $counter 1;

    while (
    $counter <= $num_checkboxes) {
    echo 
    "<input type=\"checkbox\" name=\"checkbox[]\">";
    $counter++;



    ---------------
    Leonard Whistler

  • Users who have thanked Len Whistler for this post:

    four0four (02-15-2010)

  • #12
    Regular Coder
    Join Date
    Jun 2008
    Posts
    104
    Thanks
    71
    Thanked 0 Times in 0 Posts
    Thank you all for your help! That helped steer me in the right direction!

  • #13
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,537
    Thanks
    8
    Thanked 1,093 Times in 1,084 Posts
    Len,

    I think you can also do it like this ...
    PHP Code:
    <?php
    // this will tell you how many boxes were checked ...
    $count=count($_POST['checkbox']);

    echo 
    $count;

    // this works because only CHECKED checkboxes will be in the array.
    // all unchecked checkboxes won't be counted (they do not exist).

    ?>


  •  

    Posting Permissions

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