问题描述:

This is my function under the model..

 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;

}

This is my controller..

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

}

Here $data['cartegory'] and $data['brand'] contains the complete select element with children option element instead of only the option element which is selected from form view. What I'm doing wrong?

网友答案:

what are you trying to do? i'm sorry i don't get it. If your add_cellphone function is trying to get data after validation, we should use $this->input->post("name") to get the data, isn't it? what is set_value use for?

相关阅读:
Top