php - passing to model through controller in codeigniter? -
i want select option of product categories dropdown menu , show products have specific category.
here form part my view:
<?php $attributes = array('method'=>"post", "class" => "myc", "id" => "myc", "name" => "dropdwn"); echo form_open_multipart('frontend/home/display', $attributes); ?> <div class="form-group"> <div class="col-sm-9"> <select class="form-control" name="category" onchange="this.form.submit();" /> <option value="" <?php echo set_select('category', 'zero', true); ?>>categories...</option> <option value="phone" >phones</option> <option value="laptops">laptops</option> <option value="accessories" >accessories</option> </select> </div> </div> <?php echo form_close(); ?>
as can see option dropdown through onchange="this.form.submit(); (if it's not idea please suggest other way do, didn't want use ajax, i'm not @ yet, anyhow suggest seems better).
then in controller option , convert array, use in model.
controller part:
public function display($sort_by='product_id', $sort_order='asc', $offset = 0) { $this->load->model('model_product'); $selected = implode(" ", $this->input->post()); //var_dump($selected); $results = $this->model_product->fetch($selected, $limit, $offset, $sort_by, $sort_order); $data['products'] = $results['rows']; $data['num_results'] = $results['num_rows']; //and goes pagination part, guess no meaning posting it, works fine. }
my model:
function fetch($selected, $limit, $offset, $sort_by, $sort_order) { $sort_order = ($sort_order == 'desc') ? 'desc' : 'asc'; $sort_columns = array('product_id', 'name', 'description', 'category', 'country', 'price'); $sort_by = (in_array($sort_by, $sort_columns)) ? $sort_by : 'product_id'; //actual results query $q = $this->db->select('product_id, name, description, category, country, price, img_name, thumb_name') ->from('products') ->where('category', $selected) ->limit($limit, $offset) ->order_by($sort_by, $sort_order); $ret['rows'] = $q->get()->result(); var_dump($ret['rows']); die; //count query $q = $this->db->select('count(*) count', false) ->from('products'); $tmp = $q->get()->result(); $ret['num_rows'] = $tmp[0]->count; return $ret; }
here no results, probobly because clause returns no result. if change clause such ->where('category', 'phones') shows phones. so how can pass selected value query correctly?
i wil you. view file
<div class="box-header"> <h3 class="box-title">enter category details</h3> </div> <?php echo validation_errors(); $attributes = array('id' => 'formcategory','name'=>'formcategory'); ?> <?php echo form_open_multipart(base_url().'moderator/b2bcategory/addcategory'); ?> <div class="box-body"> <div class="row"> <div class="col-xs-6"> <div class="form-group"> <label for="txtcatname">title of category :</label> <input type="text" name="txtcatname" class="form-control" id="txtcatname" placeholder="category name " required="required"> </div> </div> <div class="col-xs-6"> <div class="form-group"> <label for="categorysection">section of category :</label> <select class="form-control" name="categorysection" required> <option value="" >----select------</option> <option value="1">laptop</option> <option value="2">phone</option> <option value="3">accessories</option> </select> <input type="submit" name="selsub" class="btn btn-primary"> </div> </div> </div>
controller
public function addcategory() { $this->load->helper(array('form', 'url')); $this->load->view('moderator/templates/header'); $this->load->view('moderator/templates/sidebar'); if ($this->input->post('selsub')) { $data = array('ctg_name' => $this->input->post('categorysection')); $this->b2bcategory_model->form_insert($data); }
in model
public function form_insert($data){ $this->db->insert('jil_category', $data); }
to display output table,just use
public function viewall() { $this->db -> select('*'); $this -> db -> from('jil_category'); $query = $this -> db -> get(); return $query->result(); }
Comments
Post a Comment