在codeigniter上将默认值设置为表单

时间:2014-02-04 23:17:05

标签: sql forms codeigniter

对不起老兄,这是我的第一个问题,我一直在你的网站上住了一段时间并且学到了很多东西,但现在我有一个问题,我找不到答案,问题是我有一个填写两个不同视图的表单,一个用于选择将在第二个视图上编辑的课程,第二个应该已经填充了在数据库上等于它们的所有字段,我知道应该完成设置默认值使用set_value函数定义第二个参数,但它不起作用,其他一切似乎都很好......

这是我的第一个观点

<?php echo link_tag('assets/css/nuevocurso.css');?>

<div id="infoMessage"><?php echo $message ?></div>

<section class="container">
    <div class="register">
        <h1>Ingrese el código del curso que desea modificar.</h1>
        <?php echo form_open("curso/modificarcurso");?>

        <p> <i>Código</i> <br /> <?php echo form_input($codigo);?> </p>

        <p class="submit"><?php echo form_submit('commit', "Registrarse");?></p> 

        <?php echo form_close();?>

    </div>
  </section>
  <br/>

这是我的第二个观点

<?php echo link_tag('assets/css/nuevocurso.css');?>

<div id="infoMessage"><?php echo $message ?></div>

<section class="container">
    <div class="register">
        <h1>Modifique los atributos de curso que desee.</h1>
        <?php echo form_open("curso/gestionarcurso");?>

        <p> <i>Nombre</i> <br /> <?php echo form_input($nombre);?> </p>

        <p> <i>Descripcion</i> <br /> <?php echo form_textarea($descripcion);?> </p>

        <p> <i>Codigo</i> <br /> <?php echo form_input($codigo);?> </p>

        <p> <i>Alias</i> <br /> <?php echo form_input($alias);?> </p>

        <p> <i>Programa</i> <br /> <?php echo form_textarea($programa);?> </p>




        <p class="submit"><?php echo form_submit('commit', "Registrarse");?></p> 

        <?php echo form_close();?>

    </div>
  </section>
  <br/>

和我的控制者

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Modificarcurso extends CI_Controller 
{

    public function index()
    {
    $this->load->database();
    $this->load->library('ion_auth');
    $this->load->model('nuevocurso_model');
    $this->load->helper('form');
    $this->load->library('form_validation');        
    $this->form_validation->set_rules('codigo', 'Código', 'required|xss_clean');
    $user = $this->ion_auth->user()->row();
    if ($this->form_validation->run() == true)
        {
            $codigo = $this->input->post('codigo');
            echo $codigo;
            $flag = $this->db->query("SELECT codigo FROM curso WHERE codigo = '".$codigo."'");
            $flag = $flag->result_array();
            var_dump($flag);
            if ($user->es_admin == 1 && !empty($flag))
                    {
                    echo "chupala maraco";
                    $this->data['codigo']= $codigo;
                    $this->data['tipo'] = "admin";
                    $this->manage($this->data);
                    }
            $flag = $this->db->query("SELECT codigo FROM dicta_curso_profesor WHERE rut = '".$user->rut."' AND codigo = '".$codigo."'");
            $flag = $flag->result_array();      
            if ((!empty($flag))&&($user->es_admin!=1)&&($user->es_profesor==1))
                {
                    $this->data['codigo'] = $codigo;
                    $this->data['tipo'] = "profesor";
                    $this->manage($this->data);
                }
            if ($this->db->affected_rows()>0) 
                {
                    $this->data['message'] = 'EEEEEXITO';
                }
            else
                {
                    $this->data['message'] = 'HAHAHAHAHA';
                }
            }


            $this->data['codigo'] = array(
                'name'  => 'codigo',
                'id'    => 'codigo',
                'type'  => 'text',
                'value' => $this->form_validation->set_value('codigo'));
            echo $user->es_admin;
            echo $user->es_profesor;
            $this->load->view('home/header_view');
            $this->load->view('curso/gestionarcursoredirect',$this->data);
            $this->load->view('home/footer_view');

        }


    public function manage($datos)
    {
        $this->load->database();
        $this->load->helper('html');
        $this->load->helper('form');
        $this->load->library('form_validation');
        $user = $this->ion_auth->user();
        $this->form_validation->set_rules('nombre','Nombre','required|xss_clean');
        $this->form_validation->set_rules('alias','Alias','required|xss_clean');
        $this->form_validation->set_rules('codigo','Codigo','required|xss_clean');
        $this->form_validation->set_rules('descripcion','Codigo','required|xss_clean');

        if ($this->form_validation->run() == true)
        {
            $nombre = $this->input->post('nombre');
            $alias = $this->input->post('alias');
            $descripcion = $this->input->post('descripcion');
            $codigo = $this->input->post('codigo');
            $rutprofesor = $this->input->post('profesor');
            $flag = $this->db->query("SELECT nombre, codigo FROM curso WHERE curso.nombre = '".$nombre."' OR curso.codigo = '".$codigo."'");
            $flag = $flag->result_array();
            if (empty($flag))
                    {
                        $query = $this->db->query("INSERT INTO curso (nombre, alias, codigo, descripcion) VALUES ('".$nombre."','".$alias."','".$codigo."','".$descripcion."')");
                        $query = $this->db->query("INSERT INTO dicta_curso_profesor (rut, codigo) VALUES ('".$rutprofesor."','".$codigo."')");
                    }
            if ($this->db->affected_rows()>0) 
                                {
                                    $this->data['message'] = 'EEEEEXITO';
                                }
            else
                                {
                                    $this->data['message'] = 'HAHAHAHAHA';
                                }
        }


        $codigo = $this->input->post('codigo');
        $curso = $this->db->query("SELECT * FROM curso WHERE curso.codigo = '".$codigo."'");
        $curso = $curso->result_array();
        var_dump($curso);
        echo $curso[0]['nombre'];
        $this->data['nombre'] = array(
                'name'  => 'nombre',
                'id'    => 'nombre',
                'type'  => 'text',
                'value' => $this->form_validation->set_value('nombre', $curso[0]["nombre"]),
        );

        $this->data['descripcion'] = array(
                'name'  => 'descripcion',
                'id'    => 'descripcion',
                'type'  => 'text',
                'value' => $this->form_validation->set_value('descripcion', $curso[0]['descripcion']),
                'maxlength' => '300',
                'rows' => '5',
                'cols' => '35',
        );
        $this->data['codigo'] = array(
                'name' => 'codigo',
                'id'   => 'codigo',
                'type' => 'text',
                'value' => $this->form_validation->set_value('codigo', $curso[0]['codigo']),
                'maxlength' => '8',
        );
        $this->data['programa'] = array(
                'name' => 'programa',
                'id' => 'programa',
                'type' => 'text',
                'value' => $this->form_validation->set_value('programa', $curso[0]["programa"]),
        );
        $this->data['alias'] = array(
                'name' => 'alias',
                'id'   => 'alias',
                'type' => 'text',
                'value'=> $this->form_validation->set_value('alias', $curso[0]["alias"]),
                'maxlength' => '14',
        );
        $this->data['profesores'] = $this->nuevocurso_model->get_profesores();
        $this->load->view('home/header_view');
        $this->load->view('curso/gestionarcurso',$this->data);
        $this->load->view('home/footer_view');
    }
}

set_value位于控制器上名为manage的最后一个函数中,那些不起作用,不用担心模型,我还没有使用存储在其中的唯一函数。

谢谢大家,stackoverflow太神奇了!

2 个答案:

答案 0 :(得分:0)

如果要设置值,CodeIgniter表单助手需要两个参数,例如:

echo form_input('nombre', $nombre);

正如手册所说:“你可以在第一个和第二个参数中最低限度地传递字段名称和值”

http://ellislab.com/codeigniter/user-guide/helpers/form_helper.html

答案 1 :(得分:0)

它会在您的视图中如下工作:

echo form_input(array('name' => 'codigo', 'id' => 'codigo'), set_value('codigo', 'YOUR_VALUE'));