索引值也与数据库值一起显示


Index value is also shown with database value

我有带数据库值的下拉列表。但是,下拉列表还显示数据库值的索引,我想删除索引。我在谷歌和其他论坛上搜索过,但没有得到预期的解决方案。

function products_edit($product_id) {
    $this->load->helper('form');  
    $this->load->helper('html');
    $this->load->library('form_validation');
    $this->load->model('products_model');
    $data=$this->products_model->general();
    $category['categories']=$this->products_model->get_category();
    $product = $this->products_model->get_product($product_id);
    $this->data['title'] = 'Edit Product';
    //validate form input
    $this->form_validation->set_rules('name', 'Product name', 'required|xss_clean');
    $this->form_validation->set_rules('description', 'Product Description', 'required|xss_clean');
    $this->form_validation->set_rules('category', 'Category', 'required|xss_clean');
    //$this->form_validation->set_rules('extras', 'Extras', 'required|xss_clean');
    $this->form_validation->set_rules('price', 'Price', 'required|xss_clean');
    $this->form_validation->set_rules('is_featured', 'Is Featured', 'required|xss_clean');
    $this->form_validation->set_rules('prorder', 'prorder', 'required|xss_clean');
    if (isset($_POST) && !empty($_POST)) {      
        $data = array(
            'product_name'=> $this->input->post('name'),
            'product_desc'=> $this->input->post('description'),
            'product_category' => $this->input->post('category'),
            'extras' => $this->input->post('extras'),
            'price' => $this->input->post('price'),
            'is_featured' => $this->input->post('is_featured'),
            'prorder' => $this->input->post('prorder'),
        );
        if ($this->form_validation->run() === true) {
            $this->products_model->updateproducts($product_id, $data);
            $this->session->set_flashdata('message', "<p>Product updated successfully.</p>");
            redirect('products_controller/products_edit/'.$product_id);
        }           
    }
    $this->data['message'] = (validation_errors() ? validation_errors() : $this->session->flashdata('message'));
    $this->data['product'] = $product;
    //display the edit product form
    $this->data['name'] = array(
        'name'      => 'name',
        'id'        => 'name',
        'type'      => 'text',
        'style'     => 'width:300px;',
        'value'     => $this->form_validation->set_value('name', $product['product_name']),
    );
    $this->data['description'] = array(
        'name'      => 'description',
        'id'        => 'description',
        'type'      => 'text',
        'cols'      =>  60,
        'rows'      =>  5,
        'value'     => $this->form_validation->set_value('description', $product['product_desc']),
    );
    $cat=array();
    $test = array();
    for($i=0;$i<=3;$i++) {
        $test[$i] = array($category['categories'][$i] => $category['categories'][$i]);
    } 
    $this->data['category'] = $test;
    $this->data['extras'] = array(
        'name'  => 'extras',
        'id'    => 'extras',
        'type'  => 'text',
        'style' => 'width:250px;',
        'value' => $this->form_validation->set_value('extras', $product['extras']),
        );
    $this->data['price'] = array(
        'name'  => 'price',
        'id'    => 'picture',
        'type'  => 'text',
        'style' => 'width:250px;',
        'value' => $this->form_validation->set_value('price', $product['price']),
    );
    $this->data['is_featured'] = array(
        'name'  => 'is_featured',
        'id'    => 'is_featured',
        'type'  => 'text',
        'style' => 'width:250px;',
        'value' => $this->form_validation->set_value('is_featured', $product['is_featured']),
    );
    $this->data['prorder'] = array(
        'name'  => 'prorder',
        'id'    => 'prorder',
        'type'  => 'text',
        'style' => 'width:250px;',
        'value' => $this->form_validation->set_value('prorder', $product['prorder']),
    );
    $this->load->view('products_edit', $this->data);
}

错误发生在这一行。

for($i=0;$i<=3;$i++) {
    $test[$i] = array($category['categories'][$i] => $category['categories'][$i]);
} 

该错误是由测试阵列中的$i引起的。如果我删除它,会导致错误。我没有解决这个错误的办法。

屏幕截图http://i.share.pho.to/f4a24cc3_o.png

使用for循环构建类别下拉列表有什么特殊原因吗?

$test阵列需要看起来像这样:

$test = array(
    1 => 'Pizza',
    2 => 'Sandwich',
    3 => 'Dessert',
    4 => 'Salad'
);

其中键是类别的关联id,值是类别名称。目前,您正在使用整个类别(基于其索引)加载数组的键和值。

如果你想把所有类别都拉到下拉框中,我建议你做一些类似于下面的事情,因为这将允许你在未来添加其他类别,它们会出现在下拉框中:

foreach($category['categories'] as $category) {
    $test[$category['id']] = $category['name'];
}
$this->data['category'] = $test;

这将要求类别(我认为是从数据库表中提取的?)具有idname字段。

希望能有所帮助。。。