Welcome Guest, Not a member yet? Register   Sign In
How can I write this query with Active Records?
#4

[eluser]behnampmdg3[/eluser]
[quote author="TheFuzzy0ne" date="1369386788"]When you say you want to change the WHERE condition, do you simply mean the value, or the entire WHERE condition. In either case, query bindings will probably help: http://ellislab.com/codeigniter/user-gui...eries.html (bottom of the page).[/quote]The whole WHERE condition is dynamic. This means I will have to put ligic in the model. ot have multiple queries. Dont really like either. This is where I build the $data.
Code:
public function validate_search()
{
  $CI =& get_instance();
  $CI->price_type=$CI->input->post('price_type');
  
  
  
  $CI->load->library('form_validation');
  
  $this->class = $CI->input->post('class');
  if($this->class!='0')
   {
    $data['class'] = $CI->input->post('class') ? $CI->input->post('class') : '';
    $CI->form_validation->set_rules('class', '<strong class="yellow">Product Class</strong>', 'exact_length[1]|numeric|xss_clean|valid_product_class');
   }
  
  $this->category = $CI->input->post('category');
  if($this->category!='0')
   {
    $data['category'] = $CI->input->post('category') ? $CI->input->post('category') : '';
    $CI->form_validation->set_rules('category', '<strong class="yellow">Product Category</strong>', 'exact_length[1]|required|alpha|xss_clean');
   }
  
  #Price
  $price = $CI->input->post('price_type');
  if($price=='exact_price')
   {
    $data['price'] = $CI->input->post('price') ? $CI->input->post('price') : '';
    $data['price_type'] = 'exact_price';
    $data['price_to'] = NULL;
    $data['price_from'] = NULL;
    $CI->form_validation->set_rules('price', '<strong class="yellow">Product Price</strong>', 'required|numeric|xss_clean|numeric|greater_than[0]');
   }
  if($price=='between_price')
   {
    $data['price_from'] = $CI->input->post('price_from') ? $CI->input->post('price_from') : '';
    $data['price_to'] = $CI->input->post('price_to') ? $CI->input->post('price_to') : '';
    $data['price_type'] = 'between_price';    
    $data['price'] = NULL;
    $CI->form_validation->set_rules('price_from', 'Product <strong class="yellow">Price from</strong>', 'required|numeric|xss_clean|numeric|greater_than[0]');
    $CI->form_validation->set_rules('price_to', 'Product <strong class="yellow">Price to</strong>', 'required|numeric|xss_clean|numeric|greater_than[0]');
   }
  if($price!='exact_price' && $price!='between_price')
   {
    $data['price'] = NULL;
    $data['price_from'] = NULL;
    $data['price_to'] = NULL;    
   }
  # END price
  
  
  if(strlen($CI->input->post('production_date'))>1)
   {
    $CI->form_validation->set_rules('production_date', 'Product <strong class="yellow">date</strong>', 'valid_production_date|xss_clean');
    $data['production_date'] = $CI->input->post('production_date') ? $CI->input->post('production_date') : '';
   }
  else
   {
    $data['production_date']=NULL;
   }
  
  
  
  $this->status = $CI->input->post('status');
  if($this->status!='0')
   {
    $data['status'] = $CI->input->post('status') ? $CI->input->post('status') : '';
    $CI->form_validation->set_rules('status', '<strong class="yellow">Product Status</strong>', 'alpha|xss_clean');
   }
  $CI->form_validation->set_rules('status', 'Product <strong class="yellow">status</strong>', 'exact_length[1]|required|numeric|xss_clean');
  
  if($CI->form_validation->run())
   {
    $data['success']=TRUE;
    return $data;
   }
  else
   {
    $data['success']=FALSE;
    return $data;
   }
  
  }


Messages In This Thread
How can I write this query with Active Records? - by El Forum - 05-23-2013, 08:35 PM
How can I write this query with Active Records? - by El Forum - 05-24-2013, 02:13 AM
How can I write this query with Active Records? - by El Forum - 05-24-2013, 05:21 AM
How can I write this query with Active Records? - by El Forum - 05-24-2013, 02:33 PM
How can I write this query with Active Records? - by El Forum - 05-24-2013, 02:37 PM
How can I write this query with Active Records? - by El Forum - 05-29-2013, 01:12 AM
How can I write this query with Active Records? - by El Forum - 05-29-2013, 04:20 AM
How can I write this query with Active Records? - by El Forum - 05-29-2013, 04:41 AM



Theme © iAndrew 2016 - Forum software by © MyBB