协助 PDO 的日期选择器和 HTML table 过滤器 (PHP, mySQL)

Assistance with datepicker and HTML table filter for PDO (PHP, mySQL)

我已经 watching/reading 教程、coding/recoding 等好几天了,但仍然无法使我的功能正常工作。我有一个由 HTML table 组成的网页,其中使用 PHP 从 mySQL 数据库填充数据。我已经添加了一个日期选择器、脚本​​和一个带有用户输入的表单,但是没有成功地让 HTML table 根据那些 from日期来自日期选择器。我的难题在于 PDO 语法:我就是找不到任何使用 PDO 来执行此操作的教程。有人可以帮忙吗?我试过 AJAX 和 jquery 数据 tables,但它们最终破坏了我的 table,我仍然无法让日期选择器获得 return 结果。下面显示了我的页面的图像。

screenshot of page with styling

<!-- This is my form -->
<form name="frmSearch" method="post" action="">
 <p class="search_input">
<input type="text" placeholder="From Date" id="from_date" name="from_date" class="input-control" />
<input type="text" placeholder="To Date" id="to_date" name="to_date" style="margin-left:10px"  class="input-control"  />             
<input type="submit" name="go" value="Search" >
</p>
</form>

<!-- This is my table including <thead>, <tbody> markup and php for populating the table from the database -->
<table class="user-table"> 
<thead>
     <th><a href="admin_view_general_ledger.php?sort=id">ID</a></th> 
     <th><a href="admin_view_general_ledger.php?sort=date">Date</th> 
     <th>Receipt #</th> 
     <th><a href="admin_view_general_ledger.php?sort=mem_no">Mbr #</th> 
     <th>First Name</th> 
     <th>Last Name</th> 
     <th>Description</th> 
     <th>Transaction Type</th> 
     <th>(-/+) Amount</th> 
     <th>Expense Type</th> 
     <th>Income type</th> 
     <th>Balance</th> 
     <th>Added By</th> 
     <th>Action</th>
</thead>
<tbody>

<?php
include 'database.php';
$pdo = Database::connect();
$sql = 'SELECT * FROM general_ledger a
LEFT JOIN balance b ON a.gen_led_id=b.bal_gen_led_id
LEFT JOIN receipts c ON a.gen_led_id=c.rec_gen_led_id
LEFT JOIN ref_gen_led_expense_type d ON 
a.gen_led_expense_type=d.ref_gen_led_expense_typ
LEFT JOIN ref_gen_led_transaction_type e ON 
a.gen_led_transaction_type=e.ref_gen_led_transaction_typ
LEFT JOIN ref_gen_led_income_type f ON 
a.gen_led_income_type=f.ref_gen_led_income_typ
LEFT JOIN member g ON a.gen_led_users_mem_no=g.mem_no
LEFT JOIN balance h ON a.gen_led_id=h.bal_gen_led_id
LEFT JOIN users i ON a.gen_led_add_by=i.user_mem_no';

<!-- This is some code I have to sort the table by column name with a page refresh -->
if(isset($_GET['sort'])){
if ($_GET['sort'] == 'id')
{
    $sql .= " ORDER BY gen_led_id DESC";
}
elseif ($_GET['sort'] == 'date')
{
    $sql .= " ORDER BY gen_led_trans_date";
}
elseif ($_GET['sort'] == 'mem_no')
{
    $sql .= " ORDER BY mem_no";
}
}

foreach ($pdo->query($sql) as $row) {
    echo '<tr>';
    echo '<td>'. $row['gen_led_id'] . '</td>';
    echo '<td>'. $row['gen_led_trans_date'] . '</td>';
    echo '<td>'. $row['rec_receipt_no'] . '</td>';
    echo '<td>'. $row['mem_no'] . '</td>';
    echo '<td>'. $row['mem_fname'] . '</td>';
    echo '<td>'. $row['mem_lname'] . '</td>';
    echo '<td>'. $row['gen_led_description'] . '</td>';
    echo '<td>'. $row['ref_gen_led_transaction_desc'] . '</td>';
    echo '<td>'. $row['gen_led_amount'] . '</td>';
    echo '<td>'. $row['ref_gen_led_expense_desc'] . '</td>';
    echo '<td>'. $row['ref_gen_led_income_desc'] . '</td>';
    echo '<td>'. $row['bal_acct_balance'] . '</td>';
    echo '<td>'. $row['gen_led_add_by'] . '</td>';
    echo '<td><a class="btn" href="admin_user_receipt.php?rec_receipt_no='.$row['rec_receipt_no'].'">View Receipt</a></td>';
    echo ' ';
    echo '</tr>';
 }

Database::disconnect();
?>
</tbody>
</table>
<!-- This is my script which does display the datepicker when the input boxes are clicked -->
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<script>
$.datepicker.setDefaults({
showOn: "button",
buttonImage: "datepicker.png",
buttonText: "Date Picker",
buttonImageOnly: true,
dateFormat: 'yy-mm-dd'  
});
$(function() {
$("#from_date").datepicker();
$("#to_date").datepicker();
});
</script>

我在您的查询中没有看到您告诉查询查找介于开始日期和结束日期之间的日期的任何地方。

为您的 php 块输入此代码:

include 'database.php';
$pdo = Database::connect();
$sql = 'SELECT * FROM general_ledger a
LEFT JOIN balance b ON a.gen_led_id=b.bal_gen_led_id
LEFT JOIN receipts c ON a.gen_led_id=c.rec_gen_led_id
LEFT JOIN ref_gen_led_expense_type d ON 
a.gen_led_expense_type=d.ref_gen_led_expense_typ
LEFT JOIN ref_gen_led_transaction_type e ON 
a.gen_led_transaction_type=e.ref_gen_led_transaction_typ
LEFT JOIN ref_gen_led_income_type f ON 
a.gen_led_income_type=f.ref_gen_led_income_typ
LEFT JOIN member g ON a.gen_led_users_mem_no=g.mem_no
LEFT JOIN balance h ON a.gen_led_id=h.bal_gen_led_id
LEFT JOIN users i ON a.gen_led_add_by=i.user_mem_no

WHERE a.gen_led_trans_date >= :from_date && 
a.gen_led_trans_date <= :to_date';

// This is some code I have to sort the table by column name with a page     refresh -->
if(isset($_GET['sort'])){
if ($_GET['sort'] == 'id')
{
    $sql .= " ORDER BY gen_led_id DESC";
}
elseif ($_GET['sort'] == 'date')
 {
    $sql .= " ORDER BY gen_led_trans_date";
}
elseif ($_GET['sort'] == 'mem_no')
{
    $sql .= " ORDER BY mem_no";
}
}

$stmt = $pdo->prepare($sql);

$stmt->bindParam(":from_date", date('Y-m-d 00:00:00', strtotime($_POST['from_date'])));
$stmt->bindParam(":to_date",     date('Y-m-d 23:59:59', strtotime($_POST['to_date'])));

$stmt->execute();

$stmt->setFetchMode(PDO::FETCH_ASSOC);

while ($row = $stmt->fetch()) {

  echo '<tr>';
  echo '<td>'. $row['gen_led_id'] . '</td>';
  echo '<td>'. $row['gen_led_trans_date'] . '</td>';
  echo '<td>'. $row['rec_receipt_no'] . '</td>';
  echo '<td>'. $row['mem_no'] . '</td>';
  echo '<td>'. $row['mem_fname'] . '</td>';
  echo '<td>'. $row['mem_lname'] . '</td>';
  echo '<td>'. $row['gen_led_description'] . '</td>';
  echo '<td>'. $row['ref_gen_led_transaction_desc'] . '</td>';
  echo '<td>'. $row['gen_led_amount'] . '</td>';
  echo '<td>'. $row['ref_gen_led_expense_desc'] . '</td>';
  echo '<td>'. $row['ref_gen_led_income_desc'] . '</td>';
  echo '<td>'. $row['bal_acct_balance'] . '</td>';
  echo '<td>'. $row['gen_led_add_by'] . '</td>';
  echo '<td><a class="btn"     href="admin_user_receipt.php?rec_receipt_no='.$row['rec_receipt_no'].'">View    Receipt</a></td>';
  echo ' ';
  echo '</tr>';

 }

 Database::disconnect();

供您继续阅读的一些内容: PDO 查询和准备好的语句。 php date() 函数及其格式。 php strtotime()函数及使用

您的日期选择器将为您提供一个日期 yy-mm-dd 格式。您需要确保此日期格式与您的数据库时间格式匹配。那应该会为您指明正确的方向。

您需要了解 table 上的日期格式。我猜是 Y-m-d H:i:s。所以我从yy-mm-dd转换而来。这些没有什么不同。一种是 javascript 表示法,另一种是 PHP 表示法。

我建议您再次检查数据表,这对您的代码来说真的很难。使用数据表,您可以使用 rang date filter

轻松添加它