1. A Change of Season
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Mar 2004
    Next Door
    Rep Power

    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:
    <select name="post_month">
        <option selected="selected"></option>
            $m = 0;
                    ?><option value="<?php echo $m;?>"                 <?php if($post_m ?> selected="selected" <?php } ?>
                    ><?php echo date("F", mktime(0, 0, 0, $m));?></option><?php 
    Now in order to keep my views clean for genious desginers, would this be the right thing to do?

    PHP Code:
    $data['months'] = $this->drop_down->index('month'); 
    And in libraries folder I ll have:
    PHP Code:
    class Months_drop_down
    $posted_month "";
          public function 
    $CI =& get_instance();
    $CI->posted_month $CI->input->post($name) ? $CI->input->post($name) : "";
    $m 0;
    $CI->dropdown="<select name=\\"".$name."\">";
    $CI->dropdown.="<option value=\"".$m."\"";     if($CI->posted_m
    $CI->dropdown.= ">\n".date("F"mktime(000$m))."</option>";
    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
    Rep Power
    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));?>
        <?php endfor; ?>
    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.

    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
    A Land Down Under
    Rep Power
    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