0

The following code works well so far for searching on multiple fields. How can I include a date rage criteria in it to search with (startdate, enddate)?

The date field is named "IncidentDate" in the table "t_persons"

$criteria = array('FamilyName', 'FirstName', 'OtherNames', 'NRCNo', 'PassportNo', 'Gender', 'IncidenceCountryID', 'Status', 'OffenceKeyword', 'AgencyID', 'CountryID', 'IncidenceCountryID' );
    $likes = "";
    $url_criteria = '';
    foreach ( $criteria AS $criterion ) {
            if ( ! empty($_POST[$criterion]) ) {
                    $value = ($_POST[$criterion]);
                    $likes .= " AND `$criterion` LIKE '%$value%'";
                    $url_criteria .= '&'.$criterion.'='.htmlentities($_POST[$criterion]);
            } elseif ( ! empty($_GET[$criterion]) ) {
                    $value = mysql_real_escape_string($_GET[$criterion]);
                    $likes .= " AND `$criterion` LIKE '%$value%'";
                    $url_criteria .= '&'.$criterion.'='.htmlentities($_GET[$criterion]);
            } //var_dump($likes);
    }
    $sql = "SELECT * FROM t_persons WHERE PersonID>0" . $likes . " ORDER BY PersonID DESC";
Barmar
  • 741,623
  • 53
  • 500
  • 612
user1868306
  • 75
  • 1
  • 14
  • 1
    FYI, if you want to allow both `GET` and `POST`, you can use `$_REQUEST` -- it contains parameters from both. – Barmar Sep 01 '14 at 09:05

1 Answers1

1
if (!empty($_REQUEST['start_date']) && !empty($_REQUEST['end_date'])) {
    $start = mysqli_real_escape_string($con, $_REQUEST['start_date']);
    $end = mysqli_real_escape_string($con, $_REQUEST['end_date']);
    $likes .= " AND IncidentDate BETWEEN '$start' AND '$end'";
    $url_criteria .= '&start_date='.htmlentities($_REQUEST['start_date']).'&end_date='.htmlentities($_REQUEST['end_date']);
}
Barmar
  • 741,623
  • 53
  • 500
  • 612