Page 2 of 2 FirstFirst 12
Results 11 to 20 of 20

Thread: Interesting behaviour with inputs and arrays.

  1. #11
    Join Date
    Jun 2004
    Posts
    296
    Rep Power
    0

    Default

    Back on topic:

    Code:
    <?php
    
      echo '<pre>';
      print_r($_GET);
      echo '</pre>';
    ?>
    <form name="test" method="GET" action="updating.php">
      <input name="input[]" type="checkbox" value="1" />
      <input name="input[]" type="checkbox" value="2"/>
      <input name="input[]" type="checkbox" value="3"/>
      <input name="input[]" type="checkbox" value="4"/>
      <input type="submit" value="Test"/>
    </form>
    will give you the following when 1 and 3 are selected:

    Code:
    Array
    (
        [input] => Array
            (
                [0] => 1
                [1] => 3
            )
    
    )

  2. #12
    Join Date
    Jun 2004
    Posts
    296
    Rep Power
    0

    Default

    Its not that exciting, but with a little more php we get:

    Code:
    <form name="test" method="GET" action="updating.php">
      <input name="input[1]" type="checkbox" value="1" <?php echo (!empty($_GET['input'][1])) ? "checked=\"checked\"" : "" ?> />
      <input name="input[2]" type="checkbox" value="2" <?php echo (!empty($_GET['input'][2])) ? "checked=\"checked\"" : "" ?> />
      <input name="input[3]" type="checkbox" value="3" <?php echo (!empty($_GET['input'][3])) ? "checked=\"checked\"" : "" ?> />
      <input name="input[4]" type="checkbox" value="4" <?php echo (!empty($_GET['input'][4])) ? "checked=\"checked\"" : "" ?> />
      <input type="submit" value="Test"/>
    </form>

  3. #13
    Join Date
    Dec 2005
    Posts
    41
    Rep Power
    0

    Default

    Quote Originally Posted by alexdevmaster
    Its not that exciting, but with a little more php we get:....
    Yeah man, nice aint it?

  4. #14
    Join Date
    Feb 2003
    Posts
    3,184
    Rep Power
    0

    Default

    Please, please, use a loop when ever possible. there is nothing more annoying than 31 lines of if statments when a for loop could suffice.

  5. #15
    Join Date
    Jun 2004
    Posts
    296
    Rep Power
    0

    Default

    Quote Originally Posted by owen
    Please, please, use a loop when ever possible. there is nothing more annoying than 31 lines of if statments when a for loop could suffice.
    LOL, I prefer to copy & paste. Besides it was just test code. But bad habit, I suppose

  6. #16
    Join Date
    Dec 2005
    Posts
    41
    Rep Power
    0

    Default

    Quote Originally Posted by alexdevmaster
    LOL, I prefer to copy & paste. Besides it was just test code. But bad habit, I suppose
    Uhu. Still you must try to avoid copy&paste. To date I am no saint myself on this, especially when conducting a test.

    Remember that habits dictate our way of doing things in the real (non-testing) world too!
    Last edited by fabian; Jan 18, 2006 at 09:07 PM.

  7. #17
    Join Date
    Jan 2005
    Posts
    3,151
    Rep Power
    0

    Default

    copy&paste vs. looping

  8. #18
    Join Date
    Jun 2004
    Posts
    296
    Rep Power
    0

    Default

    Quote Originally Posted by pogi_2nr
    copy&paste vs. looping
    For 'Proof of concept', copy&paste usually wins

  9. #19
    Join Date
    Jan 2005
    Posts
    3,151
    Rep Power
    0

    Default

    Copy and pasting produces code that runs faster if it replaces loops, from my experience that is.

  10. #20
    Join Date
    Jun 2004
    Posts
    296
    Rep Power
    0

    Default

    Quote Originally Posted by pogi_2nr
    Copy and pasting produces code that runs faster if it replaces loops, from my experience that is.
    Yup, thats part of game programming 101. When optimizing, many times the over head of setting up a looping construct (especially for code that is called thousands of times in secs), calls for the expansion of the loop.

Posting Permissions

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