Welcome Guest, Not a member yet? Register   Sign In
Creating my own helpers? Is this the good way to clean the view?
#1

[eluser]behnampmdg3[/eluser]
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>
    &lt;?php
        $m = 0;
        while($m<12)
            {
                ++$m;
                ?&gt;<option value="&lt;?php echo $m;?&gt;"                 &lt;?php if($post_m ?&gt; selected="selected" &lt;?php } ?&gt;
                >&lt;?php echo date("F", mktime(0, 0, 0, $m));?&gt;</option>&lt;?php
            }
    ?&gt;
</select>
Now in order to keep my views clean for stupid desginers, would this be the right thing to do?

Controller:
Code:
$this->load->library('months_drop_down','','drop_down');
$data['months'] = $this->drop_down->index('month');
And in libraries folder I ll have:
Code:
&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

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="&lt;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(0, 0, 0, $m))."</option>";
                    }
    $CI->dropdown.="</select>";
    return $CI->dropdown;
}
}
And in view
Code:
&lt;?php echo $months;?&gt;
Please advice. Thanks


Messages In This Thread
Creating my own helpers? Is this the good way to clean the view? - by El Forum - 01-27-2013, 10:02 PM



Theme © iAndrew 2016 - Forum software by © MyBB