繁体   English   中英

为PDO(PHP,mySQL)提供日期选择器和HTML表格过滤器的协助

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

我已经看/读了教程,编码/编码等了好几天了,但仍然无法正常使用我的功能。 我有一个网页,其中包含一个HTML表,该表中填充了使用PHP的mySQL数据库中的数据。 我添加了一个日期选择器,脚本,并与用户输入的形式,但都没有成功地让HTML表格过滤器的基础上,从那些日期从日期选择器。 我的挣扎与PDO语法有关:我只是找不到使用PDO进行此操作的任何教程。 有人可以帮忙吗? 我已经尝试过AJAX和jquery数据表,但是它们最终破坏了我的表,但我仍然无法获取datepicker返回结果。 图片显示在我页面的下面。

带有样式的页面的屏幕截图

<!-- 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格式的日期。 您将需要确保此日期格式与您的数据库时间格式匹配。 那应该为您指明正确的方向。

您需要知道表格上的日期格式。 我猜是Ymd H:i:s。 所以我从yy-mm-dd转换而来。 这些并没有什么不同。 一个是javascript表示法,另一个是PHP表示法。

我建议您再次检查数据表,这对您的代码来说真的很难。 使用数据表,您可以使用范围日期过滤器轻松添加它

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM