Mysql and PHP Dynamic Form - Codeinigter

Asked

Viewed 728 times

2

I need to create a form to create evaluations, with a number of variable questions. For this I created a form that adds questions, as requested;

The problem is saving banco de dados this variety of issues. To be clearer, follow the Code;

<div class="box">
<div class="box-header">




    <ul class="nav nav-tabs nav-tabs-left">
        <li class="active">
            <a href="#add" data-toggle="tab"><i class="icon-plus"></i> 
                <?php echo ('adicionar avaliação');?>
            </a>
         </li>
    </ul>
</div> 


<div class="box-content padded">
<?php echo form_open('admin/avalia/create' , array('class' => 'form-horizontal validatable','target'=>'_top'));?>
    <div class="tab-content">
        <div class="action-nav-normal">
                    <div class=" action-nav-button" style="width:300px;">
                      <a href="#" title="Users">
                        <img src="<?php echo base_url();?>template/images/icons/exam.png" />
                        <span>Total de <?php echo count($avalias);?> Avaliações</span>
                      </a>
                    </div>
                </div>



        <div class="tab-pane  active" id="add">

       <form action="" method="post">  
            <div class="form-actions">           
            <label style="display: block">
             <input class="btn btn-gray" type="button" name="add" value="Adicionar Pergergunta" />
             <button type="submit" class="btn btn-gray"><?php echo ('Salvar Avaliação');?></button>
            </label> 

            </div>
            </br>
            <div class="control-group">
                            <label><?php echo ('Avaliação:');?>
                            <input type="text" class="" name="titulo"/>
                            </label>
                        </div> 
            <label style="display: block">Pergunta: <input type="text" name="pergunta"></label>  
                <fieldset id="inputs_adicionais" style="border: Pergunta" name="pergunta2">  
                </fieldset>  
       </form> 

                    </div>
        </div>        
     </div>
</div>

<-- GERANDO FORMULÁRIOS ADICIONAIS -->
<script type="text/javascript">  
$(document).ready(function(){  

    var input = '<label style="display: block">Pergunta: <input type="text" name="pergunta" /> <a href="#" class="remove">X</a></label>';  
    $("input[name='add']").click(function( e ){  
        $('#inputs_adicionais').append( input );  
    });  

    $('#inputs_adicionais').delegate('a','click',function( e ){  
        e.preventDefault();  
        $( this ).parent('label').remove();  
    });  

});  
</script>  
Segue o código do controller:

function avalia($param1 = '', $param2 = '')
    {
        if ($this->session->userdata('admin_login') != 1)
            redirect(base_url(), 'refresh');
        if ($param1 == 'create') {
            $data['avalia_id']    = $this->input->post('avalia_id');
            $data['titulo']    = $this->input->post('titulo');
            $data['pergunta'] = $this->input->post('pergunta');
            $this->db->insert('avalia', $data);
            redirect(base_url() . 'index.php?admin/avalia/', 'refresh');
        }
        if ($param1 == 'edit' && $param2 == 'do_update') {
            $data['avalia_id']    = $this->input->post('avalia_id');
            $data['titulo']    = $this->input->post('titulo');
            $data['pergunta'] = $this->input->post('pergunta');

            $this->db->where('avalia_id', $param3);
            $this->db->update('avalia', $data);
            redirect(base_url() . 'index.php?admin/avalia/', 'refresh');
        } else if ($param1 == 'edit') {
            $page_data['edit_data'] = $this->db->get_where('avalia', array(
                'avalia_id' => $param2
            ))->result_array();
        }
        if ($param1 == 'delete') {
            $this->db->where('avalia_id', $param2);
            $this->db->delete('avalia');
            redirect(base_url() . 'index.php?admin/avalia/', 'refresh');
        }
        $page_data['avalia']      = $this->db->get('avalia')->result_array();
        $page_data['page_name']  = 'avalia';
        $page_data['page_title'] = ('Avaliação');
        $this->load->view('index', $page_data);
    }

I wonder how I could save the additional fields generated on banco de dados.

1 answer

0

You need to have at least two tables. In the first you put the id and the title of the quiz. In the second quiz you put the questions and the id of the quiz you just created in table 1.

Or you can have 3 tables: First with the question id, Second with the question id and the third related question_id with questions_id.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.