Welcome Guest, Not a member yet? Register   Sign In
Page load speed ( Forms & making them in the controller)
#1

[eluser]RS71[/eluser]
Hello,

I am currently making a form and I was wondering if I should really make arrays of the inputs in the controller and use the form helpers. I can't effectively test the page load speed changes (since moving to the form helpers) on this machine but I'd like some advice on what to do. I'd really like to have the beauty and organization of passing from the controller but I'm afraid that when my hundreds of thousands of daily visitors load these pages everything will slow down.
#2

[eluser]Teks[/eluser]
If I understand correctly, you are actually building HTML code for your forms inside your controller, and passing that code to the view. Is that correct?

If that is so, I would say, that even though you certainly *can* work this way, it is not the intended way of working with CodeIgniter, and will probably make things harder for you to maintain in the future. Any display code (= (X)HTML, CSS, Javascript, etc.), including any code that builds your form elements, belongs directly in your VIEWS - not in controllers or models. Views are php scripts, just like your controllers and models, and therefore you can use the Form Helper directly from inside the views. In this instance, the job of the controller is merely to pass any necessary raw data to the view - ie., data that the controller has retrieved from the models or the database - but the controller usually has no idea how the view is going to display it.

You may wish to have a look at the File Uploading Class, as it has a nice step-by-step tutorial on how to setup a form view that works with a controller in the back-end.

I hope this helps.
#3

[eluser]RS71[/eluser]
Hello Teks,
Thank you for your reply.

They way I currently have it set up is:

I set the form field data in the controller (id, name, options for dropdowns, params) as an array and pass that data to the view.

I then use that data passed to the view in the form helpers.

So my code would look something like this:

controller
Code:
//
        $data['fullname_field'] = array(
                                  'name'        => 'full_name',
                                  'id'          => 'full_name',
                                  'class'            =>'inputtext full_name',
                                  'maxlength'   => '180',
                                  'size'        => '25'
                                );
        
        $data['birthday_day_field'] = 'birthday_day';
        $data['birthday_day_name'] = $this->lang->line('signup_birthday_day');
        $data['birthday_day_params'] = 'id="'.$data['birthday_day_field'].'"';
        $data['birthday_day_options'] = array(
                                    ''    => 'Dia:'
                                );
                                for ($i = 1; $i <= 31; $i++) {
                                    $data['birthday_day_options'][$i] = $i;
                                }
                                
        $data['birthday_month_field'] = 'birthday_month';
        $data['birthday_month_name'] = $this->lang->line('signup_birthday_month');
        $data['birthday_month_params'] = 'id="'.$data['birthday_month_field'].'"';
        $data['birthday_month_options'] = array(
                                    '' => 'Mes:',
                                    '1' => 'Jan',
                                    '2' => 'Fev',
                                    '3' => 'Mar',
                                    '4' => 'Abr',
                                    '5' => 'Mai',
                                    '6' => 'Jun',
                                    '7' => 'Jul',
                                    '8' => 'Ago',
                                    '9' => 'Set',
                                    '10' => 'Out',
                                    '11' => 'Nov',
                                    '12' => 'Dez'
                                );
                                
        $data['birthday_year_field'] = 'birthday_year';
        $data['birthday_year_name'] = $this->lang->line('signup_birthday_year');
        $data['birthday_year_params'] = 'id="'.$data['birthday_year_field'].'"';
        $data['birthday_year_options'] = array(
                                    ''    => 'Ano:'
                                );
                                for ($i = $current_year; $i >= $max_year; $i--) {
                                    $data['birthday_year_options'][$i] = $i;
                                }
                                

        $data['sex_field'] = 'sex';
        $data['sex_name'] = $this->lang->line('signup_sex');
        $data['sex_params'] = 'id="'.$data['sex_field'].'" class="select"';
        $data['sex_options'] = array(
                                    ''    => 'Seu Sexo:',
                                    '1' => 'Feminino',
                                    '2' => 'Masculino'
                                );

view
Code:
//
                    <tr class="select sex">
                        <td class="label">Sexo:</td>
                        <td>
                            &lt;?php echo form_dropdown($sex_field, $sex_options, set_value($sex_field, (isset($databasevalue->fetch)) ? $databasevalue->fetch : ''), $sex_params); ?&gt;
                                &lt;?php echo form_error($sex_field); ?&gt;
                        </td>
                    </tr>
                    <tr class="birthday tallrow">
                        <td class="label">Data de Nascimento:</td>
                        <td>
                            &lt;?php echo form_dropdown($birthday_day_field, $birthday_day_options, set_value($birthday_day_field, (isset($databasevalue->fetch)) ? $databasevalue->fetch : ''), $birthday_day_params); ?&gt;
                            
                            &lt;?php echo form_dropdown($birthday_month_field, $birthday_month_options, set_value($birthday_month_field, (isset($databasevalue->fetch)) ? $databasevalue->fetch : ''), $birthday_month_params); ?&gt;

                            &lt;?php echo form_dropdown($birthday_year_field, $birthday_year_options, set_value($birthday_year_field, (isset($databasevalue->fetch)) ? $databasevalue->fetch : ''), $birthday_year_params); ?&gt;
                            
                        </td>
                    </tr>

It seems to me that when I have my app up (which I estimate some 200,000+ daily visitors), the system will be overworking making these forms. Then again it could just be me worrying too much.

Thanks in advance.
#4

[eluser]obiron2[/eluser]
There should not be an overhead problem with the forms being generated dynamically in CI so keep going along these lines.

A couple of things I would point out though.

It is against the current received wisdom to build forms in tables. you should stick to using divs and spans and some good CSS - it takes longer initally but gives you better control in the long run.

I would move the preparation of the form array out of the controller and into a model that contains all your forms (see the CI 1.7.1 form validation module for an indication of how this works). this means that if you need to present the same form from different controllers you only have to modify it in one place.

Build a generic form renderer and pass the relevant form data array to it (you may need additional information on top of the basic form helper data - e.g. class names, js event calls, whether to display an information icon next to the field etc..)

I am 75% to building mine. when I finish it, I will post it to the board or the wiki.

obiron




Theme © iAndrew 2016 - Forum software by © MyBB