MySQL で準備された PHP Web アプリケーションがあり、ページネーションに問題がある PDO を渡すことにしました。
これは私のコードです
<html>
<head>
<link rel="stylesheet" media="all" type="text/css" href="css/jquery-ui.css" />
<link rel="stylesheet" media="all" type="text/css" href="css/jquery-ui-timepicker-addon.css" />
<script src="js/jquery-1.7.2.min.js"></script>
<!-- jQuery UI -->
<script src="js/jquery-ui-1.8.21.custom.min.js"></script>
<script type="text/javascript" src="js/timepicker.js"></script>
</head>
<script type="text/javascript" src="js/jquery-ui-sliderAccess.js"></script>
<script type="text/javascript">
$(function(){
$('#time').datetimepicker({
dateFormat: 'yy-mm-dd',
timeFormat: "HH:mm:ss "
});
minDate: getFormattedDate(new Date())
function getFormattedDate(date) {
var day = date.getDate();
var month = date.getMonth() + 1;
var year = date.getFullYear().toString().slice(2);
return day + '-' + month + '-' + year;
}
});
</script>
<script type="text/javascript" src="js/jquery-ui-sliderAccess.js"></script>
<script type="text/javascript">
$(function(){
$('#time1').datetimepicker({
dateFormat: 'yy-mm-dd',
timeFormat: "HH:mm:ss "
});
minDate: getFormattedDate(new Date())
function getFormattedDate(date) {
var day = date.getDate();
var month = date.getMonth() + 1;
var year = date.getFullYear().toString().slice(2);
return day + '-' + month + '-' + year;
}
});
</script>
<body>
<form action="" method="post">
<input class="input-medium" type="text" name="time" id="time" value="start_date"/>
<input class="input-medium" type="text" name="time1" id="time1" value="end_date"/>
<button style="margin-bottom:10px;" type="submit" class="btn btn-primary">Getir</button>
</form>
</body>
</html>
<?php
error_reporting(E_ALL ^ E_NOTICE);
error_reporting(0);
$time = $_POST['time'];
$time1 = $_POST['time1'];
try {
require_once('conn.php');
$cepbankSUM = $conn->prepare("SELECT SUM(tutar) FROM cepbank WHERE tarih BETWEEN :time AND :time1");
$cepbankSUM->bindParam(':time', $time, PDO::PARAM_STR, 20);
$cepbankSUM->bindParam(':time1', $time1, PDO::PARAM_STR, 20);
$cepbankSUM->execute();
$row_cepbankSUM = $cepbankSUM->fetch();
// Find out how many items are in the table
$cepbank = $conn->prepare('SELECT COUNT(*) FROM cepbank WHERE tarih BETWEEN :time AND :time1');
$cepbank->bindParam(':time', $time, PDO::PARAM_STR, 20);
$cepbank->bindParam(':time1', $time1, PDO::PARAM_STR, 20);
$cepbank->execute();
$total = $cepbank->fetchColumn();
// How many items to list per page
$limit = 20;
// How many pages will there be
$pages = ceil($total / $limit);
// What page are we currently on?
$page = min($pages, filter_input(INPUT_GET, 'page', FILTER_VALIDATE_INT, array(
'options' => array(
'default' => 1,
'min_range' => 1,
),
)));
// Calculate the offset for the query
$offset = max($page - 1, 0) * $limit;
// Some information to display to the user
$start = $offset + 1;
$end = min(($offset + $limit), $total);
// The "back" link
$prevlink = ($page > 1) ? '<a href="?page=1" title="First page">«</a> <a href="?page=' . ($page - 1) . '" title="Previous page">‹</a>' : '<span class="disabled">«</span> <span class="disabled">‹</span>';
// The "forward" link
$nextlink = ($page < $pages) ? '<a href="?page=' . ($page + 1) . '" title="Next page">›</a> <a href="?page=' . $pages . '" title="Last page">»</a>' : '<span class="disabled">›</span> <span class="disabled">»</span>';
// Display the paging information
echo '<div id="paging"><p>', $prevlink, ' Page ', $page, ' of ', $pages, ' pages, displaying ', $start, '-', $end, ' of ', $total, ' results ', $nextlink, ' </p></div>';
// Prepare the paged query
$stmt = $conn->prepare('SELECT * FROM cepbank WHERE tarih BETWEEN :time AND :time1 ORDER BY tarih DESC LIMIT :limit OFFSET :offset');
// Bind the query params
$stmt->bindParam(':time', $time, PDO:: PARAM_STR, 20);
$stmt->bindParam(':time1', $time1, PDO:: PARAM_STR, 20);
$stmt->bindParam(':limit', $limit, PDO:: PARAM_INT);
$stmt->bindParam(':offset', $offset, PDO:: PARAM_INT);
$stmt->execute();
// Do we have any results?
if ($stmt->rowCount() > 0) {
// Define how we want to fetch the results
$stmt->setFetchMode(PDO::FETCH_ASSOC);
$iterator = new IteratorIterator($stmt);
// Display the results
foreach ($iterator as $row_cepbank) {
?>
<table
<tbody>
<tr>
<td class="center"><?php echo $row_cepbank['username']?></td>
<td class="center"><?php echo $row_cepbank['trans_id']?></td>
<td class="center"><?php echo $row_cepbank['bank']?></td>
<td class="center"><?php echo $row_cepbank['tutar']?></td>
<td class="center"><?php echo $row_cepbank['operator']?></td>
<td class="center"><?php echo $row_cepbank['tarih']?></td>
</tr>
</tbody>
</table>
<?php }
} else {
echo '<p>No results could be displayed.</p>';
}
} catch (Exception $e) {
echo '<p>', $e->getMessage(), '</p>';
}
?>
これは、最初の結果セットを使用した最初の実行で問題なく機能します。次のページをクリックすると、このエラーが発生します
« ‹ Page 0 of 0 pages, displaying -19-0 of 0 results › »
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-20' at line 1
しかし、時間変数マヌエルのように書くと
$time = '2013-08-01 00:00:00';
$time1 = '2013-09-01 00:00:00';
すべてが完璧に機能する問題はありませんが、タイムピッカーで POST メソッドのような時間を取得しようとすると
$time = $_POST['time'];
$time1 = $_POST['time1'];
失敗します。
あなたの偉大な心を待っています。
オフセット変数を次のように編集しました
$offset = max($page - 1, 0) * $limit;
いや結果は
最初の実行では、次のページのリンクをクリックすると正常に動作します
« ‹ Page 0 of 0 pages, displaying 1-0 of 0 results › »
No results could be displayed.