0

I am passing array data to my view like this

public function login(){
       $array= array(
           array(
                'type'           =>     'text',
                'usrname'        =>     'username',
                'class'          =>     'form-control',
                'placeholder'    =>     'Username',
             ),
           array(
                'type'           =>     'password',
                'class'          =>     'form-control',
                'placeholder'    =>     'Password',
            ),
           array(
                'type'          =>'heading',
                'heading'       =>'Not a Memer YET ?',
            ),

       );

    $output['data']=$array;
    $this->load->view('authentication',$output);

in my view this is what i'm doing

<?php foreach ($data as $key=> $value):?>
        <?php 
            switch ($value){
                case  $value['type']=='heading':
                    echo $value['heading'];
                    break;
                case  $value['type']=='text':
                    echo 'textfield';
                    break;
             }

        ?>
    <?php endforeach;?>

this is working properly but i just want to ask is it standard practice the way i am doing it or there can be any better way to traverse this array in switch .

Need your help please

This question is not about if i should use switch or if else , i want to know the way I've used switch in terms of coding is it correct or not ?or i can improve it .

Sikander
  • 2,799
  • 12
  • 48
  • 100

1 Answers1

1

Instead of comparing in switch case, you can modify your switch code something like this.

<?php foreach ($data as $key=> $value):?>
<?php 
    switch ($value['type']){
        case 'heading':
                        echo "heading";
                        break;
        case 'text':
                        echo 'textfield';
                        break;
        default : //default code here
     }

?>
<?php endforeach;?>
Param Bhat
  • 470
  • 1
  • 6
  • 17