1
單選項元素這是我的模式下功能..笨發送完整的選擇,而不是從形式
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']
包含了完整的選擇元素與子選項元素,而不是隻有從窗體視圖中選擇的選項元素。我做錯了什麼?
表單是否通過驗證? – Jeemusu 2013-04-10 04:10:05
'set_value'只能在你的視圖中使用,設置''字段的'value =「」'屬性。正如fembb在他的回答中所說的,您需要使用'$ this-> input-> post('category');'從控制器的表單中獲取發佈數據。 – Jeemusu 2013-04-11 05:12:24