Reputation: 157
I'm using the CodeIgniter framework. I'm creating a search function for my companies CMS, the search has different inputs coming from a form. Is there a way to run query groups based on the $_POST
input? I can run queries separate for example:
class Search_model extends CI_Model {
public function get_results($search_term='default')
{
$cat = $_POST["col"];
// Column Select:
$col = 'id, dob, gender, app_date, tests_ordered, payment_method, cost, report_results, country';
// Build the query.
$database = $this->load->database('bookings_dev', TRUE)
->select($col)
->from('results')
->where($cat . ' ' . 'BETWEEN "'. date('Y-m-d', strtotime($_POST['sdate'])). '" and "'. date('Y-m-d', strtotime($_POST['edate'])).'"')
->order_by($_POST["col"]);
// Execute the query.
$query = $database->get();
// Return the results.
return $query->result_array();
}
Works fine ad returns the correct records (for example all records within the selected date range, my issue is when I try and expand the query to take other options into account I either get all records from the db or none. I've tried changing the order of the LIKE and WHERE clauses, I've tried OR_LIKE, OR_WHERE etc. Any pointers in the right direction will be greatly appreciated. I've read CIs Query Builder Class Docs but m officially stuck. here is my model:
<?php
class Search_model extends CI_Model {
public function get_results($search_term='default')
{
$cat = $_POST["col"];
// Column Select:
$col = 'id, dob, gender, app_date, tests_ordered, payment_method, cost, report_results, country';
// Build the query.
$database = $this->load->database('bookings_dev', TRUE)
->select($col)
->from('results')
->group_start()
->where($cat . ' ' . 'BETWEEN "'. date('Y-m-d', strtotime($_POST['sdate'])). '" and "'. date('Y-m-d', strtotime($_POST['edate'])).'"')
->group_end()
->or_group_start()
->or_like($_POST["col"], $_POST["terms"])
->group_end()
->order_by($_POST["col"]);
// Execute the query.
$query = $database->get();
// Return the results.
return $query->result_array();
}
}
What id like to achive is: SELECT $cols FROM results WHERE condtion 1 has input OR condition 2 input OR condition 3 etc etc Is this possible?
Upvotes: 0
Views: 82
Reputation: 493
Yes, you can run query groups based on the $_POST
input, but input is sent to controller, you should write the method with some paramaters and set it in controller. You cannot transfer data straight to model.
This is a reference.
https://www.sitepoint.com/the-mvc-pattern-and-php-1/
Upvotes: 0