I have a table associated with a database mysql, All I want is to add a search box by date.
Although I have tried many methods, but unfortunately all of them did not work for me as I should This is my code:
<form action="" method="GET"">
<input type="date" name="search" class="form-control" placeholder="Start" >
<button type="submit" class="btn btn-primary">Search</button>
</form>
function getRecords($params)
{
$rp = isset($params['rowCount']) ? $params['rowCount'] : 10;
if (isset($params['current'])) {
$page = $params['current'];
} else {
$page = 1;
}
$start_from = ($page - 1) * $rp;
$sql = $sqlRec = $sqlTot = $where = '';
if (!empty($params['searchPhrase'])) {
$where .= " WHERE ";
$where .= " ( fullname LIKE '" . $params['searchPhrase'] . "%' ";
$where .= " OR email LIKE '" . $params['searchPhrase'] . "%' ";
$where .= " OR phone LIKE '" . $params['searchPhrase'] . "%' )";
}
if (!empty($params['sort'])) {
$where .= " ORDER By " . key($params['sort']) . ' ' . current($params['sort']) . " ";
}
// getting total number records without any search
$sql = "SELECT * FROM `api` where DATE_FORMAT(created_at, '%Y-%m-%d') = DATE_SUB(CURRENT_DATE(),INTERVAL 1 DAY)";
$sqlTot .= $sql;
$sqlRec .= $sql;
//concatenate search sql if value exist
if (isset($where) && $where != '') {
$sqlTot .= $where;
$sqlRec .= $where;
}
if ($rp != -1)
$sqlRec .= " LIMIT " . $start_from . "," . $rp;
}
My Answer:
To add a search box by date to your table using MySQL and PHP, you can modify your existing code as follows:
1. Update your HTML form to include a date input field for searching by date:
html
2. Modify your PHP code to handle the search by date:
php
function getRecords($params)
{
$rp = isset($params['rowCount']) ? $params['rowCount'] : 10;
if (isset($params['current'])) {
$page = $params['current'];
} else {
$page = 1;
}
$start_from = ($page - 1) * $rp;
$sql = $sqlRec = $sqlTot = $where = '';
if (!empty($params['search_date'])) {
$search_date = $params['search_date'];
$where .= " WHERE DATE(created_at) = '$search_date'";
}
if (!empty($params['sort'])) {
$where .= " ORDER By " . key($params['sort']) . ' ' . current($params['sort']);
}
// getting total number records without any search
$sql = "SELECT * FROM `api`";
$sqlTot .= $sql;
$sqlRec .= $sql;
//concatenate search sql if value exist
if (isset($where) && $where != '') {
$sqlTot .= $where;
$sqlRec .= $where;
}
if ($rp != -1) {
$sqlRec .= " LIMIT " . $start_from . "," . $rp;
}
// Execute your SQL queries and return the results
}
With these modifications, you can now search your table by date using the date input field in the form. Make sure to adjust the SQL query and database column names according to your specific table structure.
Rate this post
3 of 5 based on 5855 votesComments