0

i want to multidimensional array to my view and then use this array to build form

This is what i've in my controller

function signin(){
    $attributes = array(
        'name'  =>  
        array(
            'name'        =>    'name',
            'type'        =>    'text',
            'placeholder'     =>     '' ,
            'value'       =>    'value'
            ),
        'password'                =>    
            array(
            'name'        =>    'name',
            'type'        =>    'password',
            'placeholder' =>      '',

            ),
        'gender' => 
            array(
            'name'        =>    'name',
            'type'        =>    'select',
                        'value'=>       
                            array(
                 'male','female'
            ),
        ),

        'usertpye'=>array(
            'type'                =>      'radio',
            'seller'          =>    'seller',
            'buyer'       =>    'buyer'
        ),
        'upload'=>array(
            'type'            =>      'file',
            'name'        =>    'file'
        ),

        'submit'=>array(
            'type'        =>    'submit',
            'name'        =>    'submit',
            'value'       =>    'submit'

        )

    );
        $this->load->view('login',$attributes);

}

in my view login i can access these items like $name or $password but i want to fetch in a loop.really have no idea how can i do it please help.

0

1 Answer 1

2

The load function receives an array, which keys then parses as variables in the view. Thus, you get variables like $name, $password etc. Just add another layer before calling the load function like:

$data['attributes'] = $attributes;

And then, when loading the view do

$this->load->view('login',$data); 

Here is the array a bit adjusted:

$attributes = array(
    'name'  =>  
    array(
        'name'        =>    'name',
        'type'        =>    'text',
        'placeholder' =>     '' ,
        'value'       =>    'value'
        ),
    'password'                =>    
        array(
        'name'        =>    'name',
        'type'        =>    'password',
        'placeholder' =>    '',

        ),
    'gender' => 
        array(
        'name'    =>    'name',
        'type'    =>    'select',
        'options' =>     array(
             'male'   => 'Male',
             'female' => 'Female'
        ),
    ),

    'usertpye'=>array(
        'type'   =>     'radio',
        'values' => array(
            'seller' =>    'seller',
            'buyer'  =>    'buyer'
        )
    ),
    'upload'=>array(
        'type' =>      'file',
        'name' =>    'file'
    ),

    'submit'=>array(
        'type'        =>    'submit',
        'name'        =>    'submit',
        'value'       =>    'submit'

    )
);

Here is how it would look like with the form helper of CI (this would go in the view, remember to first load the helper in the controller):

echo form_open('email/send');
foreach($attributes as $key=>$attribute) {
    echo form_label($key).'<br/>';
    if($attribute['type'] == 'select') {
        echo form_dropdown($attribute['name'],$attribute['options']).'<br/>';
    } elseif($attribute['type'] == 'radio') {
        foreach ($attribute['values'] as $value) {
            echo form_label($value);
            echo form_radio(array('name' => $key, 'value' => $value)).'<br/>';
        }
    } else {
        echo form_input($attribute).'<br/>';
    }
}

Note I did some adjustments to your initial attributes array to make it work, but you'd still need to improve its structure, add unique names for all items etc.

Sign up to request clarification or add additional context in comments.

4 Comments

that seems good but problem is passing this data to form i want to generate form out of it and unable to find the way to do it
Are you using plain html or CI's form helper?
i am using form helper
for this i think i will have to use core php to parse md array fine ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.