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.
Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    Senior Coder
    Join Date
    May 2005
    Posts
    2,137
    Thanks
    96
    Thanked 72 Times in 72 Posts

    checkboxes, counting them on submit

    So I am building a roulette game, a very simple one where it has 35 numbers (35 checkboxes) and like if you select one number and get it, you win the biggest prize, and if you select 3 its smaller, and like if you select 20 numbers, you get a little prize.

    So anyways how do I know which numbers were checked, and how many in a single submit? I looked at this code

    PHP Code:
    Pizza:<input type="checkbox" value="Pizza" name="food[]"
    do I make the array the same on all of them? but say I check #7, #12 and #33. How do I do it on the $_POST those numbers, and for it to count 3? So 1 = 50 coins, 3 X 50 = 150 coin entry

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Thats what the value is. Checkboxes are unique in that they will provide no data if nothing is checked.
    PHP Code:
    <input type="checkbox" value="1" name="guess[]" />
    <
    input type="checkbox" value="2" name="guess[]" /> 
    $_POST / $_GET will contain an array for 'guess'. If you count $_POST['guess'] it will tell you how many selections were made, and each item in the $_POST['guess'] will correspond to the selected number.
    Its actually been awhile since I've used a checkbox with multidimensional selections. I'm about 95% certain that its sequentially index, so if you check only the second box, it will have a value like so:
    Code:
    //print_r($_POST);
    Array
    (
        'guess' => Array(
            0 => '2'
        );
    )
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    Senior Coder
    Join Date
    May 2005
    Posts
    2,137
    Thanks
    96
    Thanked 72 Times in 72 Posts
    I kind of understand, I've never done arrays before, they are extremely confusing.

  • #4
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    Fou-Lou is correct. If a box is not checked, it does not exist in the $_POST array.

  • #5
    Senior Coder
    Join Date
    May 2005
    Posts
    2,137
    Thanks
    96
    Thanked 72 Times in 72 Posts
    Yeah but what about this array thing?

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    I don't quite understand what you're question is.
    Consider:
    PHP Code:
    $aChecked = (isset($_POST['food']) && is_array($_POST['food'])) ? $_POST['food'] : array();
    $iItems count($aChecked); // Contains the number of items checked
    foreach ($aChecked AS $checked)
    {
        
    printf("%s\n"$checked); // Lists each of the items checked.
    }
    // or
    print implode("\n"$aChecked); 
    That make sense?
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #7
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    I think what will help you understand how a bunch of checkboxes with the name of "guess[]" gets transformed into the $_POST array is print_r the $_POST array. Use the <pre> tag to make the output readable.

    PHP Code:
    echo "<pre>".print_r($_POST,true)."</pre>"
    See what the output looks like if you check all boxes, check no boxes, and check a few boxes. You should get a pretty good idea of what's going on with $_POST by doing this.

  • #8
    Senior Coder
    Join Date
    May 2005
    Posts
    2,137
    Thanks
    96
    Thanked 72 Times in 72 Posts
    Quote Originally Posted by Fou-Lu View Post
    I don't quite understand what you're question is.
    Consider:
    PHP Code:
    $aChecked = (isset($_POST['food']) && is_array($_POST['food'])) ? $_POST['food'] : array();
    $iItems count($aChecked); // Contains the number of items checked
    foreach ($aChecked AS $checked)
    {
        
    printf("%s\n"$checked); // Lists each of the items checked.
    }
    // or
    print implode("\n"$aChecked); 
    That make sense?
    Try that and it does pull the numbers, however as you know I use a page array so all the content is in a certain box. This won't work with printf. When I use printf it acts as an error and displays at the top of the screen.

  • #9
    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 that might be close to what you want. DEMO: http://www.stubby.ca/test.php

    PHP Code:
    <?php

      
    if($_POST['food']) {

      
    $food_array $_POST['food'];
      
    $counter "0";

      foreach (
    $food_array as $value) {
      
    $counter++;
      echo 
    "Check box number $value checked<br>";
      }
      echo 
    "<br>$counter Check boxes checked<br><hr>";

      }
    ?>
    <form method="post" action="<?php echo $PHP_SELF;?>">
    <?php

      
    //add as many check boxes to array as you need.
      
    $array = array(=> 'Pizza','Hotdog','Hamburger','Cheeseburger');

      foreach(
    $array as $key => $value) {
      echo 
    "$key <input type=\"checkbox\" name=\"food[]\" value=\"$key\">$value</input><br>\n";
      }
    ?>
    <br><input type="submit" name="submit" value="Submit">
    </form>



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

  • #10
    Senior Coder
    Join Date
    May 2005
    Posts
    2,137
    Thanks
    96
    Thanked 72 Times in 72 Posts
    That may work, I will try that.

  • #11
    Senior Coder
    Join Date
    May 2005
    Posts
    2,137
    Thanks
    96
    Thanked 72 Times in 72 Posts
    Sweet it works, but how is it possible to get like 7 numbers in a row across and then start 8 to 15 below that? So it would be like

    PHP Code:
    <td>number 1</td><td>number 2</tdetc
    I tried that and it kept giving me the same numbers over.

    Here is my coding.

    PHP Code:
    echo '<table width="95%" align="center">';
            
    $array = array(=> '1','2','3','4','5');

      foreach(
    $array as $key => $value) {
            echo 
    "<tr><td><input type=\"checkbox\" name=\"num[]\" value=\"$key\">$value</input></td></tr>";
            } 

  • #12
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    You can use the modulus operator to start a new <tr> row every nth iteration.
    PHP Code:
    <table><tr>
    <?php
    for ($i 0$i 22$i++) {
        if (
    $i == && $i 1) {
            echo 
    "</tr><tr>";
        }
        echo 
    "<td>$i</td>\n";
    }  

    ?>
    </tr></table>
    Something like that. You'll need to incorporate your array if you want to, otherwise just add 1 to $i to get the checkbox value.

    (edit) messed something up.
    Last edited by Fumigator; 05-07-2009 at 10:37 PM. Reason: Try this version (the first ones were messed)

  • #13
    Senior Coder
    Join Date
    May 2005
    Posts
    2,137
    Thanks
    96
    Thanked 72 Times in 72 Posts
    Hmm really confusing, don't quite understand. Even doing PHP for 3 years, still lack much knowledge. Anyway you can break it down a little or comment what is doing what?

  • #14
    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
    I updated my original code with some added features and comments that should help. . DEMO: http://www.stubby.ca/test_two.php

    PHP Code:
    <?php

      
    if($_POST['output']) {

      
    $output $_POST['output'];
      
    $counter 0;

      foreach (
    $output as $value) {
      
    $counter++;
      echo 
    "Check box number $value checked<br>";
      }
      echo 
    "<br>$counter Check boxes checked<br><hr>";

      }
    ?>

    <form method="post" action="<?php echo $PHP_SELF;?>">
      <?php

      
    echo "<table><tr>";
      
    $array range(135); // Range 1-35 checkboxes.
      
    $counter 0;
      
    $number_of_columns 7;

      foreach(
    $array as $value) {
      
    $counter = ($counter $number_of_columns);  // Study modulus operator.
     
      // When counter equals 0 new row every 7th column.
      
    if ($counter == 0) {
      echo 
    "</tr><tr>";  
      }

      echo 
    "<td><input type=\"checkbox\" name=\"output[]\" value=\"$value\">$value</input></td>\n";
      
    $counter++;
      }
      echo 
    "</tr></table>";
      
    ?>
      <br><input type="submit" name="submit" value="Submit">
    </form>
    Last edited by Len Whistler; 05-08-2009 at 01:18 AM.
    Leonard Whistler

  • Users who have thanked Len Whistler for this post:

    masterofollies (05-08-2009)

  • #15
    Senior Coder
    Join Date
    May 2005
    Posts
    2,137
    Thanks
    96
    Thanked 72 Times in 72 Posts
    Excellent thank you!!


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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