Welcome Guest, Not a member yet? Register   Sign In
quick question arrays and helpers and such
#1

[eluser]dsims[/eluser]
I am new to CI so bear with me.
Since most of the helpers are better utilized with arrays, I was wondering if writing helpers differently would be useful.
example on the form helper
typical input form helper
Code:
$data = array(
              'name'        => 'username',
              'id'          => 'username',
              'value'       => 'johndoe',
              'maxlength'   => '100',
              'size'        => '50',
              'style'       => 'width:50%',
            );

echo form_input($data);

// Would produce:

<input type="text" name="username" id="username" value="johndoe" maxlength="100" size="50" style="width:50%" />
Form helper function for form_input()
Code:
if ( ! function_exists('form_input'))
{
    function form_input($data = '', $value = '', $extra = '')
    {
        $defaults = array('type' => 'text', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);

        return "<input ".parse_form_attributes($data, $defaults).$extra." />\n";
    }
}
I was thinking of a function to parse arrays
Code:
function iterate_array($data){
if(is_array($data)){
return $data;
}
else{
$newdata = explode(" ",$data);
$i=0;
foreach($newdata as $key => $value){
    if (strstr($newdata[$i], ".")){
    $class = str_replace(".", "",$newdata[$i] );
    $formclass .= $class." ";
    }
    elseif(strstr($newdata[$i], "#")){
    $id = str_replace( "#", "",$newdata[$i]);
    $formid .= $id." ";
    }
    elseif(strstr($newdata[$i], 'style=')){
    $formstyle = str_replace( 'style=', "",$newdata[$i]);
    $forminstyle = $formstyle;
    }
    elseif(strstr($newdata[$i], '=>')){
    $formray = $newdata[$i];
    $formra  .= $formray." ";
    }
$i++;
}
if(!empty($formclass)){
$formed['class'] = trim($formclass);
}else{
$formclass =array();
}
if(!empty($formid)){
$formed['id']    = trim($formid);
}else{
$formclass =array();
}
if(!empty($forminstyle)){
$formed['style'] = trim($forminstyle);
}else{
$formclass =array();
}
if(!empty($formra)){
    $prepped = trim($formra);
        if (strstr($prepped, " ")){
        $prep = explode(" ",$prepped);
            foreach($prep as $k => $v){
            $v = explode("=>",$v);
                foreach($v as $v1){
                $t = $v[0];
                $p =$v[1];
                $formed[$t] =$p;
                }
            $i++;
            }
        }else{
        $prep  = explode("=>",$prepped);
                foreach($prep as $k => $v){
                $t = $prep[0];
                $p =$prep[1];
                $formed[$t] =$p;
                }
        }
     }else{
$formra =array();
}
$form = array_merge($formclass, $formid, $formstyle, $formra);
return $form;  
}
to use it the helper I would use
Code:
if ( ! function_exists('form_input'))
{
if( ! function_exists('iterate_array'){
    function form_input($data = '', $value = '', $extra = '')
    {
                

        $defaults = array('type' => 'text', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);

        return "<input ".parse_form_attributes($data, $defaults).$extra." />\n";
    }else{
        function form_input($data = '', $extra = '')
    {
              $defaults = array('type' => 'text');
                $inline   = iterate_array($data);
                $attribbed= array_merge($defaults, $inline);
        return "<input ".parse_form_attributes($data, $attribbed).$extra." />\n";
    
}
I really don't know if the above would work and it is not short tagged.
I have currently gotten a myfom_helper.php that utilizes this functioning
Code:
echo functionname('input name=>fclass .class #rad .class2 maxlength=>50 style=color:red;height:15px');
which outputs
<input type="text" name="fclass" value="" class="class class2" id="rad" style="color:red;height:15px" maxlength="50"  />


Im actually working on modified code of a form helper so I could just use functionname('input TYPEtext MXL100 NAMEusername .class .class2 #id #id2 $$js'); Gotta love spaghetti.




Theme © iAndrew 2016 - Forum software by © MyBB