#1
  1. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,650
    Rep Power
    171

    Shrinking the view


    Hello friends;

    Lates say I want to write some code that creates a drop down that shows months of the year.

    I can easily write loose php in view which sucks:
    Code:
    <select name="post_month">
        <option selected="selected"></option>
        <?php
            $m = 0;
            while($m<12)
                {
                    ++$m;
                    ?><option value="<?php echo $m;?>"                 <?php if($post_m ?> selected="selected" <?php } ?>
                    ><?php echo date("F", mktime(0, 0, 0, $m));?></option><?php 
                }
        ?>
    </select>
    Now in order to keep my views clean for genious desginers, would this be the right thing to do?

    Controller:
    PHP Code:
    $this->load->library('months_drop_down','','drop_down');
    $data['months'] = $this->drop_down->index('month'); 
    And in libraries folder I ll have:
    PHP Code:
    class Months_drop_down
     
    {
          protected 
    $posted_month "";
          public function 
    index($name=NULL
              {
                
    $CI =& get_instance();
                
    $CI->posted_month $CI->input->post($name) ? $CI->input->post($name) : "";
              
    $m 0;
              
    $CI->dropdown="<select name=\\"".$name."\">";
                while(
    $m<12)
                                {
                
    $m++;
                
    $CI->dropdown.="<option value=\"".$m."\"";     if($CI->posted_m
                 
    {
                  
    $CI->dropdown.="selected=\"selected\"";
                 }
                
    $CI->dropdown.= ">\n".date("F"mktime(000$m))."</option>";
                                }
                
    $CI->dropdown.="</select>"
                return 
    $CI->dropdown;
             }
     } 
    And in view
    PHP Code:
    <?php echo $months;?>
    Please advice. Thanks
  2. #2
  3. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,317
    Rep Power
    7170
    Don't cater specifically to designers. Either they're smart enough to understand PHP, or they're smart enough to understand what to touch and what not to touch, or they're not smart enough to be mucking around in your application's views.

    Your views will be a bit clearer if you use the alternative PHP block syntax:
    PHP Code:
    ?>
    <select name="post_month">
        <option selected="selected"></option>
        <?php for($m 1$m <= 12$m++): ?>
            <option value="<?php echo $m;?>"
                <?php if(??): ?> selected="selected" <?php endif; ?>>
                <?php echo date("F"mktime(000$m));?>
            </option>
        <?php endfor; ?>
    </select>
    Alternatively, create a new view with that snippet of code and include it in your main view using load->view. Then tell your designers not to touch the partial view snippets.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  4. #3
  5. Code Monkey V. 0.9
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2005
    Location
    A Land Down Under
    Posts
    2,094
    Rep Power
    1990
    Another thing to remember when you're doing things like this:

    Don't think about "clean" (as far as templating goes), think about "re-use". Your example is actually a very good one because there's a high chance that you'll want to have a months select elements in more then one place, so that's when you should start looking at it to extract that out into it's own helper or function, so I think that you've got the right idea. Depending on how you've got the rest of your system set out you can incorporate E-Oreo's suggestion of using that with a separate snippet for the HTML layout pretty easily.

IMN logo majestic logo threadwatch logo seochat tools logo