这是我在模型下的功能..
public function get_categories_select() {
// output
$output = "";
// query
$this->db->select("*");
$this->db->from("categories");
$query = $this->db->get();
// zero result(s)
if ($query->num_rows() == 0) {
return $output = "No Results Found";
}
// result(s)
$output .= "<select name=\"category\">";
foreach ($query->result_array() as $row) {
$output .= "<option value=\"{$row['name']}\">{$row['name']}</option>\n";
}
$output .= "</select>";
// return
return $output;
}
这是我的控制器..
public function add_cellphone() {
// loading
$this->load->model("category_model");
$this->load->model("brand_model");
// controlling
$data['categories'] = $this->category_model->get_categories_select();
$data['brands'] = $this->brand_model->get_brands_select("cell phones");
if ($this->validate_cellphone() === FALSE) {
// on failure
$to_view = "add_cellphone";
} else {
// on success
$data['category'] = set_value('category');
$data['brand'] = set_value('brand');
$data['model'] = set_value("model");
$data['price'] = set_value("price");
$this->load->model("product_model");
$this->product_model->add_product($data);
$to_view = "add_cellphone_success";
}
// viewing
$this->load->view("admin/templates/header");
$this->load->view("admin/pages/{$to_view}", $data);
$this->load->view("admin/templates/footer");
}
public function validate_cellphone() {
// laoding
$this->load->helper("form");
$this->load->library("form_validation");
// validation rules
$config = array(
array(
"field" => "category",
"label" => "Category",
"rules" => "required",
),
array(
"field" => "brand",
"label" => "Brand",
"rules" => "required",
),
array(
"field" => "model",
"label" => "Model",
"rules" => "required",
),
array(
"field" => "price",
"label" => "Price",
"rules" => "required",
),
array(
"field" => "released",
"label" => "Released",
"rules" => "required",
),
array(
"field" => "status",
"label" => "Status",
"rules" => "required",
)
);
// controlling
$this->form_validation->set_rules($config);
$this->form_validation->set_error_delimiters('<span class="error">', '</span>');
if ($this->form_validation->run() === FALSE) {
return FALSE;
}
else {
return TRUE;
}
// no viewing
}
这里$data['cartegory'] and $data['brand']
包含带有子选项元素的完整选择元素,而不仅仅是从表单视图中选择的选项元素。我做错了什么?