Codeigniter form_dropdown错误

时间:2014-04-08 18:21:30

标签: php html codeigniter drop-down-menu compiler-errors

我正在建立一个网站,我有一个注册客户端的功能,我需要选择从下拉列表中的数据库中选择,然后将其发送到用户表,我得到2个错误atm和我无法找到解决方案。

这是我的模特:

class Home extends CI_Model{

//SQL

    function get_prodTYPE(){
        $this->db->select()->from('producttype')->where('active',1);
        $query=$this->db->get();
        return $query->result_array();
    }

    function get_COUNTRY(){
        $this->db->select()->from('pais');
        $this->db->order_by("id_pais", "asc");
        $result = $this->db->get('pais');
        $resultado = array();
        if($result->num_rows() > 0){
            $resultado[''] = 'please select';
            foreach($result->result_array() as $row){
                $resultado[$row['id_pais']] = $row['Pais'];
            }
        }
    return $resultado;
    }

然后我有一个大文件,这是注册的主要部分 这是我的控制器:

function register(){

    if(!$this->session->userdata('userID')){

        $data['prodTYPE']=$this->home->get_prodTYPE();
        $data['info']=$this->home->get_info();
        $data['country']=$this->home->get_COUNTRY();


        //validacao
            $config=array(

                array(
                    'field'=>'username',
                    'label'=>'Username',
                    'rules'=>'trim|required|is_unique[users.username]|callback_min_lenght_3'                
                ),

                array(
                    'field'=>'password',
                    'label'=>'Password',
                    'rules'=>'trim|required|callback_min_lenght_6'
                ),

                array(
                    'field'=>'password2',
                    'label'=>'Password de Confirmação',
                    'rules'=>'trim|required|matches[password]'
                ),

                array(
                    'field'=>'email',
                    'label'=>'Email',
                    'rules'=>'trim|required|is_unique[users.email]|valid_email'
                ),

                array(
                    'field'=>'name',
                    'label'=>'Nome',
                    'rules'=>'trim|required|callback_username_check|callback_min_lenght_3'
                ),

                array(
                    'field'=>'contact',
                    'label'=>'Contacto',
                    'rules'=>'trim|required|numeric|callback_min_lenght_9'
                ),

                array(
                    'field'=>'address',
                    'label'=>'Morada',
                    'rules'=>'trim|required|callback_adress_check|callback_min_lenght_9'
                ),

                array(
                    'field'=>'postalCODE',
                    'label'=>'Codigo Postal',
                    'rules'=>'trim|required|numeric]|callback_min_lenght_4'
                ),

                array(
                    'field'=>'postalCODE2',
                    'label'=>'Codigo Postal',
                    'rules'=>'trim|required|numeric|callback_min_lenght_3'
                ),

                array(
                    'field'=>'city',
                    'label'=>'Localidade',
                    'rules'=>'trim|required|callback_min_lenght_3|callback_city_check'
                ),

                array(
                    'field'=>'taxpayerNUMBER',
                    'label'=>'N.º Contribuinte',
                    'rules'=>'trim|required|numeric|callback_min_lenght_9'
                ),

            );

            $this->form_validation->set_rules($config);

            if($this->form_validation->run() == FALSE){//erro
                $data['errors']=validation_errors();

            } else {//registo
                $data=array(
                    'username'=>$_POST['username'],
                    'userTYPE'=>'user',
                    'password'=>sha1($_POST['password']),
                    'email'=>$_POST['email'],
                    'active'=>$_POST['newsletter']==null?0:1,
                    'name'=>$_POST['name'],
                    'contact'=>$_POST['contact'],
                    'country'=>$_POST['country'],
                    'address'=>$_POST['address'],
                    'postalCODE'=>$_POST['postalCODE'],
                    'postalCODE2'=>$_POST['postalCODE2'],
                    'city'=>$_POST['city'],
                    'taxpayerNUMBER'=>$_POST['taxpayerNUMBER']                      
                );
                $userid = $this->user->create_user($data);

                $data_init=array(
                    'userID'=>$userid['userID'],
                    'situation'=>0                  
                    );

                $this->user->init_shop($data_init);//comecar encomendas

                $this->session->set_userdata('userID',$userid['userID']);
                $this->session->set_userdata('userTYPE',$userid['userTYPE']);//userTYPE
                $this->session->set_userdata('userNAME',$userid['username']);
                $this->session->set_userdata('newsletter',$_POST['newsletter']==null?0:1);


                redirect(base_url().'/index.php/emails/email_to/'.$userid['userID']);
            }
            $data['page']="#fragment-3";
            $this->load->view('header',$data);
            $this->load->view('login',$data);
            $this->load->view('footer');
        } else {
            redirect(base_url().'/index.php/');
    }
}

Finnaly我有视图:

        <div id="fragment-3">

    <?php echo form_open(base_url().'index.php/users/register'); ?>
    <div id="user_r"><?=form_label('Username','username')?><?php
    $data_form=array(
            'id'=>'username',
            'name'=>'username',
            'size'=>50,
            'maxlength'=>'20',
            'placeholder'=>'username',
        );
    ?></div><div id="user_r_b"><?php echo form_input($data_form)?></div> 


    <div id="mail_r"><?=form_label('Email','email')?><?php
        $data_form=array(
                'id'=>'email',
                'name'=>'email',
                'size'=>50,
                'maxlength'=>'40',
                'placeholder'=>'email válido',
        );
        ?></div><div id="mail_r_b"><?php echo form_input($data_form)?></div> 


        <div id="news_u"><?=form_label('Newsletter','newsletter')?><?php
        $data_form=array(
                'name'=>'newsletter',
                'id'=>'newsletter',
                'checked'=>TRUE,
                'value'=>1,
                'style'=>'margin:10px',    
                );
        ?></div>
        <div id="news_u_b"><?php echo form_checkbox($data_form)?></div> 


        <div id="pass_1"><?=form_label('Password','password')?><?php
        $data_form=array(
                'name'=>'password',
                'id'=>'password',
                'size'=>50,
                'maxlength'=>'20',
                'placeholder'=>'minimo 6 caracteres',
            );
        ?></div>
        <div id="pass_1_b"><?php echo form_password($data_form)?></div> 


        <div id="pass_2"><?=form_label('Confirmar Pass','password2')?><?php
        $data_form=array(
                'name'=>'password2',
                'id'=>'password2',
                'size'=>50,
                'maxlength'=>'20',
                'placeholder'=>'minimo 6 caracteres',
            );
        ?></div>
        <div id="pass_2_b"><?php echo form_password($data_form)?></div>


        <div id="name_r"><?=form_label('Nome','name')?><?php
        $data_form=array(
                'id'=>'name',
                'name'=>'name',
                'size'=>50,
                'maxlength'=>'50',
                'placeholder'=>'nome facturação',
            );
        ?></div>
        <div id="name_r_b"><?php echo form_input($data_form)?></div> 


        <div id="contact1"><?=form_label('Contacto','contact')?><?php
        $data_form=array(
                'id'=>'contact',
                'name'=>'contact',
                'size'=>24,
                'maxlength'=>'9',
                'placeholder'=>'n.º movel ou fixo',
                'style'=>"padding-bottom : 0px;font-size:13px;"
        );
        ?>
        </div>
        <div id="contact_b"><?php echo form_input($data_form)?></div>



        <div id="pais"><?=form_label('Pais','country');?></div>
        <div id="pais_b"><?php echo form_dropdown('country', $country);  ?></div> 


        <div id="adres"><?=form_label('Morada','address')?><?php
        $data_form=array(
                'id'=>'address',
                'name'=>'address',
                'size'=>50,
                'maxlength'=>'50',
                'placeholder'=>'morada de facturação',
        );
        ?></div><div id="adres_b"><?php echo form_input($data_form)?></div> 


        <div id="code"><?=form_label('Codigo Postal','postalCODE')?><?php
        $data_form=array(
                'id'=>'postalCODE',
                'name'=>'postalCODE',
                'size'=>6,
                'maxlength'=>'6',
                'placeholder'=>'1111',
        );
        ?></div>
        <div id="code1"><?php echo form_input($data_form)?> /</div>  

        <div id="code22"><?=form_label('','postalCODE2')?><?php
        $data_form=array(
                'id'=>'postalCODE2',
                'name'=>'postalCODE2',
                'size'=>5,
                'maxlength'=>'5',
                'placeholder'=>'111',
        );
        ?></div><div id="code2"><?php echo form_input($data_form)?></div>  



        <div id="city2"><?=form_label('Localidade','city')?> <?php
        $data_form=array(
                'id'=>'city',
                'name'=>'city',
                'size'=>24,
                'maxlength'=>'30',
                'style'=>"padding-bottom : 0px;font-size:13px;",
                'placeholder'=>'localidade facturação',
        );
        ?></div><div id="city_b"><?php echo form_input($data_form)?></div> 


        <div id="number"><?=form_label('N.º Contribuinte','taxpayerNUMBER')?><?php
        $data_form=array(
                'id'=>'taxpayerNUMBER',
                'name'=>'taxpayerNUMBER',
                'size'=>24,
                'maxlength'=>'9',
                'placeholder'=>'numero contribuinte valido',
        );
        ?></div><div id="number_b"><?php echo form_input($data_form)?></div> 

        <div id="botao"><?php echo form_submit('','      Registar      '); ?></div>
    <?php echo form_close(); ?>

</div>

我这里有错误的form_helper部分:

if ( ! function_exists('form_dropdown'))
    {
    function form_dropdown($name = '', $options = array(), $selected = array(), $extra = '')
    {
        if ( ! is_array($selected))
        {
            $selected = array($selected);
        }

    // If no selected state was submitted we will attempt to set it automatically
    if (count($selected) === 0)
    {
        // If the form name appears in the $_POST array we have a winner!
        if (isset($_POST[$name]))
        {
            $selected = array($_POST[$name]);
        }
    }

    if ($extra != '') $extra = ' '.$extra;

    $multiple = (count($selected) > 1 && strpos($extra, 'multiple') === FALSE) ? ' multiple="multiple"' : '';

    $form = '<select name="'.$name.'"'.$extra.$multiple.">\n";

    foreach ($options as $key => $val)
    {
        $key = (string) $key;

        if (is_array($val) && ! empty($val))
        {
            $form .= '<optgroup label="'.$key.'">'."\n";

            foreach ($val as $optgroup_key => $optgroup_val)
            {
                $sel = (in_array($optgroup_key, $selected)) ? ' selected="selected"' : '';

                $form .= '<option value="'.$optgroup_key.'"'.$sel.'>'.(string) $optgroup_val."</option>\n";
            }

            $form .= '</optgroup>'."\n";
        }
        else
        {
            $sel = (in_array($key, $selected)) ? ' selected="selected"' : '';

            $form .= '<option value="'.$key.'"'.$sel.'>'.(string) $val."</option>\n";
        }
    }

    $form .= '</select>';

    return $form;
}
}

这是2个错误

A PHP Error was encountered
Severity: Notice
Message: Undefined variable: country
Filename: views/login.php
Line Number: 143

A PHP Error was encountered
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: helpers/form_helper.php
Line Number: 331

希望有人可以帮助我 谢谢:)

1 个答案:

答案 0 :(得分:0)

你的问题很简单,在这部分代码中:

        } else {//registo
            $data=array(
                'username'=>$_POST['username'],

您正在将$data重置为新阵列。对于一个简单的解决方案,请使用array_merge():

        } else {//registo
            $additional_data=array(
                'username'=>$_POST['username'],
                'userTYPE'=>'user',
                'password'=>sha1($_POST['password']),
                'email'=>$_POST['email'],
                'active'=>$_POST['newsletter']==null?0:1,
                'name'=>$_POST['name'],
                'contact'=>$_POST['contact'],
                'country'=>$_POST['country'],
                'address'=>$_POST['address'],
                'postalCODE'=>$_POST['postalCODE'],
                'postalCODE2'=>$_POST['postalCODE2'],
                'city'=>$_POST['city'],
                'taxpayerNUMBER'=>$_POST['taxpayerNUMBER']                      
            );
            array_merge($data, $additional_data);