0

如果选择“是”,我有以下控制器可以正常工作,但如果选择“否”,则会给我带来一些麻烦。

我怎样才能让它在数据库中插入“否”?**

** 我添加了 elseif 试图让它拒绝!

控制器:

public function add()
{

    $this->form_validation->set_rules('add_fields_name', 'Name', 'trim|required|xss_clean');
    $this->form_validation->set_rules('add_fields_value', 'Value', 'trim|xss_clean');
    $this->form_validation->set_rules('add_fields_class', 'Class', 'trim|xss_clean');
    $this->form_validation->set_rules('add_fields_placeholder', 'Placeholder', 'trim|required|xss_clean|callback_dropdown_check');
    $this->form_validation->set_rules('add_fields_type', 'Type', 'trim|required|xss_clean|callback_dropdown_check');



    if($this->form_validation->run() == FALSE)
    {
        $data['validation_errors'] = validation_errors('<div class="alert alert-error">', '</div>');
        $data['contentMangement'] = $this->options_model->systemOptions();
        $data['pageTitle'] = 'Add Fields';
        $this->load->view('_assets/dashHeader', $data);
        $this->load->view('dashboard/add_fields', $data);
        $this->load->view('_assets/footer');    


    }else{

        $fieldName = $this->input->post('add_fields_name', TRUE);
        $fieldClass = $this->input->post('add_fields_class', TRUE);
        $fieldValue = $this->input->post('add_fields_defaultValue', TRUE);

        //Dropdown Placeholder
        $dropdown = $this->input->post('add_fields_placeholder', TRUE);

        if($dropdown =='yes')
        {
          $dropdown_value = $this->input->post('add_fields_placeholderValue', TRUE);
        }
        else($dropdown == 'no')
        {

        }

        //Removes Caps & spaces
        $fieldNameStripped = strtolower(str_replace(" ", "", $fieldName));
        $fieldValueStripped = strtolower(str_replace(" ", "", $fieldValue));
        $fieldClassStripped = strtolower(str_replace(" ", "", $fieldClass));
        //Default Form Details

        $fieldLabel = '<label for="'. $fieldNameStripped . '">'. $fieldName. '</label>'."\n";

        $formData = array(
                            'name' => ''.$fieldNameStripped.'',
                            'id' => ''.$fieldNameStripped.'',
                            'value' => ''.$fieldValueStripped.'',
                            'class' =>''.$fieldClassStripped.'',
                            'placeholder' => ''.$dropdown_value.''
                        );


        if (isset($_REQUEST['add_fields_type']))
        {
            if($_REQUEST['add_fields_type'] == 'input')
            {
                $fieldField = form_input($formData, TRUE);
            }

            if($_REQUEST['add_fields_type'] == 'textarea')
            {
                $fieldField = form_textarea($formData, TRUE);
            }

            if($_REQUEST['add_fields_type'] == 'radiobutton')
            {
                $fieldField = form_radio($formData, TRUE);
            }

            if($_REQUEST['add_fields_type'] == 'checkbox')
            {
                $fieldField = form_checkbox($formData, TRUE);
            }

            $fieldTypeInsert = array(
                                    'name'  =>  ''.$fieldName.'',
                                    'class' =>  ''.$fieldClass.'',
                                    'value' => ''.$fieldValue.'',
                                    'placeholder' => ''.$dropdown.'',
                                    'placeholder_value' => ''.$dropdown_value.'', 
                                    'type'  =>  ''.$_REQUEST['add_fields_type'].'',
                                    'label' =>  ''.$fieldLabel.'',
                                    'field' =>  ''.$fieldField.''   
                                    );

            $this->field_model->insertField($fieldTypeInsert);

            $data['validation_errors'] = '<div class="alert alert-success"><strong>Thank You</strong> Your Field Has Been Added</div>';
            $data['contentMangement'] = $this->options_model->systemOptions();
            $data['pageTitle'] = 'Add Fields';
            $data['fieldType'] = $this->input->get('add_fields_type');
            $data['placeholderType'] = $this->input->get('add_fields_placeholder');
            $this->load->view('_assets/dashHeader', $data);
            $this->load->view('dashboard/add_fields', $data);
            $this->load->view('_assets/footer');    

        }
    }
}       

看法:

<label for="add_fields_placeholder">Placeholder: </label>
    <select name="add_fields_placeholder" id="add_fields_placeholder">
        <option value="">Please Select</option>
        <option value="yes" <?php echo set_select('add_fields_placeholder','yes', ( !empty($placeholderType) && $placeholderType == "yes" ? TRUE : FALSE ));?>>Yes</option>
        <option value="no" <?php echo set_select('add_fields_placeholder','no', ( !empty($placeholderType) && $placeholderType == "no" ? TRUE : FALSE ));?>>No</option>
    </select>
4

2 回答 2

3

好的,首先摆脱

$dropdown = '';

因为这会重置您的价值,所以它不会持有是或否

其次,两者

if($dropdown = 'yes')
else($dropdown = 'no')

需要使用==而不是==用于为变量赋值,==用于比较值。

改完之后还能用吗?

编辑:您不能使用其他,然后添加另一个条件。你要么只需要使用

else { 

或使用其他

if($dropdown == 'no') {

编辑:为了清晰和信息,你在你的 if 中用单个 = 做的不是询问它是否等于是或否,你让它相等,并询问你是否能够

于 2012-08-14T05:20:40.053 回答
0

您将 $dropdown 设置为空字符串,然后将其比较“是”和“否”。实际上你没有比较,但检查你是否分配了$dropdown = 'yes'. 请检查比较运算符: http: //php.net/manual/en/language.operators.comparison.php

$dropdown = '';
if($dropdown = 'yes')
于 2012-08-14T05:20:54.233 回答