#1
  1. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    667
    Rep Power
    6

    Loop to build drop-down


    Hey all. As the title states, I am aiming to build a loop to build the options for a drop-down menu for Month selection.

    I figure I can use an array for month names, and an if() statement to set 'selected' for current month.

    The question I am having is the value set for each item. I could use the $i var to set the 1-12, but I need 2-digit 01-12.

    Thanks in advance! (And sorry for such basic questions)
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,911
    Rep Power
    1045
    Hi,

    use sprintf() to pad the digits with zeros:
    PHP Code:
    <?php

    printf
    ('%02s'1);
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    667
    Rep Power
    6
    Am I blind? I get the following error:

    Code:
    Parse error: syntax error, unexpected ')', expecting ';' in D:\htdocs\triple_nothing\hellzoneinc.com\www\bills.php on line 13
    in the code:

    PHP Code:
    <form name="months" method="POST">
      <select name="mo" onChange="this.form.submit()">
        <?php
          $mo 
    = array("January","February","March","April","May","June","July","August","September","October","November","December");
          for(
    $i 1$i 13$i++) {
            
    $format "<option value=\"%02s\">%02s</option>";
            
    sprintf($format$i$mo);
          }
        
    ?>
      </select>
    </form>
    Line 13 is: for($i = 1, $i < 13, $i++) {
  6. #4
  7. JavaScript is not spelt java
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2011
    Location
    Landan, England
    Posts
    743
    Rep Power
    165
    PHP Code:
    for($i 1$i 13$i++) { 
    those should be semi-colons, not commas.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    667
    Rep Power
    6
    Thanks for that. Now, I can't seem to get anything to really print from this. I have tried to even just set the 2 variables as $i to avoid arrays. I have been able to purposely get errors to print from this, but not the actual note intended. I echo the variables each loop n they print out as desired on their own. Any ideas?

    To be created:

    Code:
            <option value="01">January</option>
            <option value="02">February</option>
            <option value="03">March</option>
            <option value="04">April</option>
            <option value="05">May</option>
            <option value="06">June</option>
            <option value="07">July</option>
            <option value="08">August</option>
            <option value="09">September</option>
            <option value="10">October</option>
            <option value="11">November</option>
            <option value="12" selected="selected">December</option>
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    667
    Rep Power
    6
    Nevermind... ^_^ I forgot to echo my sprintf(). Thanks all.
  12. #7
  13. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,911
    Rep Power
    1045
    sprintf() returns a formatted string (see the documentation). When you want to print the value, you either need printf() or echo sprintf().

    But there are several other problems:
    • $mo is the array of the month names. When you print it, you'll see nothing but "Array" in the drop-down menu. I guess you meant $mo[$i].
    • What's the point of padding the option values with zeros? The value is supposed to be easy to process, it's not supposed to be pretty.
    • The second format instruction %02s is wrong, because you certainly don't want to pad the month names with zeros. It's a simple string: %s
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    667
    Rep Power
    6
    Ya, some of my stuff is written sloppy just for dev sakes. ^_^ N I looked all over my work n there was a big reason I needed the 2 character item, n I have no clue what it was. I'm guessing maybe it got taken care of in another manner and just was no longer truely needed. *shrugz*

    Final code: (More or less)
    PHP Code:
    <?php
      $mo 
    = array("","January","February","March","April","May","June","July","August","September","October","November","December");
      for(
    $i 1$i 13$i++) {
        
    //$mono = sprintf("%02s", $i);
        
    if(isset($_POST["mo"]) && $_POST["mo"] == $i) {
          echo 
    "        <option value=\"$i\" selected=\"selected\">$mo[$i]</option>\n";
          
    //$format = "        <option value=\"%02s\" selected=\"selected\">%s</option>\n";
        
    } elseif(!isset($_POST["mo"]) && $i == date('m')) {
          echo 
    "        <option value=\"$i\" selected=\"selected\">$mo[$i]</option>\n";
          
    //$format = "        <option value=\"%02s\" selected=\"selected\">%s</option>\n";
        
    } else {
          echo 
    "        <option value=\"$i\">$mo[$i]</option>\n";
          
    //$format = "        <option value=\"%02s\">%s</option>\n";
        
    }
        
    //echo sprintf($format, $i, $mo[$i]);
      
    }
    ?>
  16. #9
  17. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,911
    Rep Power
    1045
    You're using the wrong format instruction for date() . The "m" instruction indeed has leading zeros. I guess what you wanted is "n".

    Actually, this problem already shows that date() isn't a good way of comparing dates, because the purpose of date() is to display a pretty string. To get the "technical" data for a date (i. e. month, year etc. as integers), use getdate().

    You should also clean up the loop:
    • The condition $i < 13 is a strange way of expressing "up to and including 12". Why not use i <= 12?
    • When two conditions have the same outcome, put them in one branch with an OR
    • You should escape every value that goes into the HTML markup, even when it's currently not "dangerous"; you never know what you might changes in the future, and chances are, you'll forget the escaping at some point


    PHP Code:
    <?php


    // change this according to your actual encoding
    function escape_html($input) {
        return 
    htmlentities($inputENT_QUOTES ENT_XHTML'utf-8');
    }

    $months = array(        // this should actually be done by the existing date functions
        
    'January'
        
    'February'
        
    'March'
        
    'April'
        
    'May'
        
    'June'
        
    'July'
        
    'August'
        
    'September'
        
    'October'
        
    'November'
        
    'December'
    );
    for (
    $i 1$i <= 12$i++) {
        
    $current_date getdate();
        
    $selected = (isset($_POST['mo']) && $_POST['mo'] == $i) || (!isset($_POST['mo']) && $i == $current_date['mon']);
        echo 
    '<option value="' escape_html($i)  . '"' . ($selected ' selected="selected"' '') . '>' escape_html($months[$i 1]) . "</option>\n"
    }
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    667
    Rep Power
    6
    Thank you. Good point with the simple OR line to set selected like a single true/false. I am just wondering about your escape_html function. I looked into it, and am not too sure its purpose/goal with $i just being a simple single digit. The only goal I can ASSUME is assuring it's in utf-8 format.

    As far as the $months array goes, I looked around and as far as building an array with those items, everywhere I seem to end up shows the same/similar. I even thought maybe a way to omit this variable and just set some date() function or something in the loop to output month according to $i, but no luck. :-/

    I'm not sure if this will help....

    Scipting: http://www.hellzoneinc.com/bills.txt
    To become: http://www.hellzoneinc.com/bills.php
    Replacing: http://www.hellzoneinc.com/bills.html

    Don't worry too much about slop. This still has a ways to go. Usually I work in sections. Random stuff gets added/removed, but once working as desired, I go through for a cleanup.
    Last edited by Triple_Nothing; December 5th, 2012 at 09:45 AM.
  20. #11
  21. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,911
    Rep Power
    1045
    Originally Posted by Triple_Nothing
    I am just wondering about your escape_html function. I looked into it, and am not too sure its purpose/goal with $i just being a simple single digit.
    Like I said, you should escape any value that goes into HTML markup, regardless of whether it's actually necessary in the particular case.

    This way you can be pretty sure that you don't miss a "dangerous" value. If you switch between raw values and escaped values, chances are high you get it wrong sooner or later. Maybe you refactor your code and the input changes from safe to unsafe, but you forget to add the escaping. Maybe you didn't see the value actually coming from an untrusted source. Whatever. If you simply escape all values, there's never any doubt.



    Originally Posted by Triple_Nothing
    As far as the $months array goes, I looked around and as far as building an array with those items, everywhere I seem to end up shows the same/similar. I even thought maybe a way to omit this variable and just set some date() function or something in the loop to output month according to $i, but no luck. :-/
    To be honest, I also can't think of a really good solution to get the month names -- apart from starting with an arbitrary date like "1.1.1900", adding a month each time and then getting the name through date_format() or something.

    So it's probably fine. I just didn't like the idea of writing down all month names when they already exist in every date function and class.
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    667
    Rep Power
    6
    Heh. Thanks for making me to want to look more into it. I got rid of the $mo variable and changed

    PHP Code:
    echo '<option value="' escape_html($i)  . '"' . ($selected ' selected="selected"' '') . '>' escape_html($mo[$i 1]) . "</option>\n"
    into

    PHP Code:
    echo '<option value="' escape_html($i)  . '"' . ($selected ' selected="selected"' '') . '>' date("F"mktime(000$i10)) . "</option>\n"
    Last edited by Triple_Nothing; December 5th, 2012 at 10:21 AM.

IMN logo majestic logo threadwatch logo seochat tools logo