Okay, i will try to make it short.
I want to search data by date using PHP ONLY, can anyone paste their related source code here please?
PHP Search Data By Date
Maybe a bit too short? What kind of data are you sorting? (If it's a database, the SQL you use can do that easily if the relevant table is properly defined. If it's a CSV or JSON file, that's a whole different story.)
- Edited
Yes i'm fetching the data from database. I have a form created for user to input a date range for searching data, so my query is like this;
if(isset($_POST['btnDateSearch'])){
$dateValue=$_POST['dateValue'];
$query="SELECT * FROM tb_expenses where date='$dateValue'";
$dateConnect=mysqli_query($connect,$query);
I DON'T KNOW WHAT TO DO NEXT FROM HERE!!
(Added [code]...[/code]
tags ~ MOD)
I'd start by making sure you get a usable date from the form, while avoiding SQL injection attacks:
if(isset($_POST['dateValue'])) {
$date = strtotime($_POST['dateValue']);
if(!$date) {
// handle error of invalid date here
}
else {
$dateValue = date('Y-m-d', $date);
// now do your query
}
}
As opposed to what's next, that depends on what you want to do with what gets retrieved from the DB. mysqli_query()
will return false if it fails to run, so you should check for that first and deal with any errors. Otherwise it should return a mysqli_result
object (see https://www.php.net/manual/en/class.mysqli-result.php), which you could then use in a foreach()
loop on the results of a mysqli_fetch_all()
, or a while()
loop on mysqli_fetch_assoc()
, or whatever you prefer to do, outputting the results as desired (an HTML table row?).
Okay i already have a code to fetch all data as history data:
<tr>
<?php
$qury = "SELECT * FROM expenses;";
$ret = mysqli_query($connect, $qury);
while($row = mysqli_fetch_assoc($ret)){?>
<th scope="row"><?php echo $row['id']; ?></th>
<td> <?php echo $row['item']; ?> </td>
<td> <?php echo $row['date'];?> </td>
<td> <?php echo $row['paymentType']; ?> </td>
<td><?php echo $row['amount']; ?> </td>
<td>
How do i do the date search without interrupting the history table? should i have another html table or it's still going to be in the same table tag??
What is the difference between the expenses
and tb_expenses
table? Is there a field in one or the other that relates entries to the other table? If so, is it a one-to-one, many-to-one, or many-to-many relationship? Depending on those relationships and what is it functionally that you are ultimately trying to do, there may be a way to do a single query against both tables, using some sort of SQL join
between the two tables.
I made a mistake at the first post, its the same table "expenses" not "tb_expenses"
That would be the general pattern. I might first do a mysqli_num_rows()
check to handle any case where you get zero rows returned. Assuming it's >= 1, then the basic idea would be to output your table
tag and probably a <tr>...</tr>
line with <th>...</th>
elements for the table column headers, then do your while()
loop, including <tr>
and </tr>
tags wrapping up the rows, e.g.:
while($row = mysqli_fetch_assoc($ret)) {
echo "<tr>";
foreach(array('col_1', 'col_2', 'etc') as $key) { // replace actual column names in array
echo "<td>".htmlspecialchars($row[$key])."</td>";
}
echo "</tr>\n";
}
Then stick your closing </table>
tag after the end of the loop.