[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>
<?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 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:
<?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="<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:
<?php echo $months;?>
Please advice. Thanks