我有数据库值的下拉列表。但是,下拉列表还显示数据库值的索引,我想删除index.I已在谷歌和其他论坛中搜索,但未获得预期的解决方案。
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
引起的。如果我删除它,导致错误。我没有这个错误的解决方案。
答案 0 :(得分:1)
您是否有使用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;
这会要求类别(我假设从数据库表中取出?)有id
和name
字段。
希望有帮助...