簡體   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