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
#2

[eluser]Harold Villacorte[/eluser]
You only need to reference the super object to access the native classes. Other than that write like you normally write a php class. Set the the property then access it within the method using $this->property or self::$property.
#3

[eluser]behnampmdg3[/eluser]
Harold, please explain more. Thanks
#4

[eluser]Harold Villacorte[/eluser]
This is a Codeigniter specific forum so I cannot post tutorials on PHP fundamentals. But I can tell you this much, you do not need to devote an entire library to one function. Just put it in a model. CI allows you to load any model from any controller.
#5

[eluser]PhilTem[/eluser]
There's a helper called "form_helper" that creates dropdowns from an array passed as an argument. Look at the user's guide to see, how it works.

The only thing you would need to do: Create the array of months in another helper (or extend the form_helper with a MY_form_helper and add a month_dropdown() function that takes care of all this Wink )
#6

[eluser]InsiteFX[/eluser]
Add to your_helper ./application/helpers
Code:
// --------------------------------------------------------------------

/**
* build_dropdown()
*
* Builds a select/options dropdown.
*
* @access public
* @param string - the select css class id
* @param string - the select css class vlaues
* @param object - the database object
* @param string - the selected option value
* @param string - the onchanged method
* @return string - the output string
*/
if ( ! function_exists('build_dropdown'))
{
//                       id         class    name    array        selected         onchange
function build_dropdown($id = '', $classes, $name, $dropdown, $selected_value, $onchange_method = '')
{
  $output  = "<select id='$id' class='$classes' name='$name' $onchange_method>\n";

        foreach ($dropdown as $key => $text)
        {
         $output .= "<option value=\"" . $key . "\"";

         if ($key == $selected_value)
   {
             $output .= " selected";
         }

         $output .= ">" . $text . "</option>\n";
        }

        $output .= "</select>\n";

        return ($output);
    }
}

// --------------------------------------------------------------------

/**
* sel_month()
*
* Description:
*
* @access public
* @param string
* @return void
*/
if ( ! function_exists('sel_month'))
{
    function sel_month()
    {
  $data = array(
   ''   => 'Month',
   '1'  => 'January',
   '2'  => 'Febuary',
   '3'  => 'March',
   '4'  => 'April',
   '5'  => 'May',
   '6'  => 'June',
   '7'  => 'July',
   '8'  => 'August',
   '9'  => 'September',
   '10' => 'October',
   '11' => 'November',
   '12' => 'December',
  );

  return $data;
}
}
#7

[eluser]CroNiX[/eluser]
CI already has helpers to build form elements like form_dropdown(). Have you tried that before making your own?
#8

[eluser]behnampmdg3[/eluser]
[quote author="Harold Villacorte" date="1359356266"]But I can tell you this much, you do not need to devote an entire library to one function. Just put it in a model. CI allows you to load any model from any controller.[/quote]But isn't model suppose to look after database actions?
#9

[eluser]behnampmdg3[/eluser]
Thanks for all the replies above. Form helper is a great solution BUT, I have become lazy using all these helpers lol

I feel tough to handcode a few things Smile
#10

[eluser]Harold Villacorte[/eluser]
Well yeah. I think we already both got our answer that stuff like that in basic Codeigniter belongs in a helper. I tend to follow a slightly different pattern because I write everything as an HMVC module with no libraries and no helpers, just modules. And modular CI is not addressed in the documentation.

The point I was making was that you were writing a class called Months_drop_down as a library and method called index() to generate a select form. So to call this function you would have to something like:

$this->load->library('months_drop_down');
$this->months_drip_down->index();

Wouldn't it be much cooler to call it like this:

$this->load->library('my_awesome_library');
$this->my_awesome_library->months_dropdown();

But we already got an answer on where to put helper functions.




Theme © iAndrew 2016 - Forum software by © MyBB