View Full Version : paginate search results
ianhaney
02-05-2016, 03:11 PM
Hi
I am trying to paginate search results but have the following error
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, array given in /home/sites/it-doneright.co.uk/public_html/admin/software-sales/search-data.php on line 148
It is dividing up the results as 5 per page so that bit is working but unsure of what I am missing
below is my script
<?php
ini_set('display_startup_errors',1);
ini_set('display_errors',1);
error_reporting(-1);
include('connect.php');
$per_page=5;
if (isset($_GET["page"])) {
$page = $_GET["page"];
}
else {
$page=1;
}
// Page will start from 0 and Multiple by Per Page
$start_from = ($page-1) * $per_page;
$data = [];
if (isset($_GET['d1']) && isset($_GET['d2'])) {
$d1 = strtotime($_GET['d1']);
$d2 = strtotime($_GET['d2']);
if ($d1 && $d2) {
$start_date = date('Y-m-d', $d1);
$end_date = date('Y-m-d', $d2);
$result = $db->prepare("SELECT * FROM purchased_software WHERE sales_month BETWEEN :a AND :b ORDER BY id LIMIT $start_from, $per_page");
$result->bindParam(':a', $start_date);
$result->bindParam(':b', $end_date);
$exec = $result->execute();
if ($exec) {
$data = $result->fetchAll();
}
}
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Search Data Between Two Dates</title>
<link rel="stylesheet" type="text/css" media="screen" href="css/styles.css" />
<link rel="stylesheet" type="text/css" href="css/tcal.css" />
<script type="text/javascript" src="js/tcal.js"></script>
</head>
<body>
<div id="logo">
<img src="images/logo/it-done-right.jpg" alt="" title="">
</div>
<?php if ($user): ?>
<div class='home-content'>
<center>
<h2>Hello, <?= $user['username']; ?></h2>
<a href='../logout.php'>Log Out</a>
<br><br>
<a href='../index.php'>Home</a>
</center>
</div>
<br>
<?php endif; ?>
<?php include("nav-menu.php"); ?>
<br>
<form action="search-data.php" method="get">
From : <input type="text" name="d1" class="tcal" value="" />
To: <input type="text" name="d2" class="tcal" value="" />
<input type="submit" value="Search">
</form>
<table class="view-repairs">
<thead>
<tr>
<th>Software ID</th>
<th>Customer PayPal Email</th>
<th>Sales Date</th>
<th>Software Title</th>
<th>Quantity</th>
<th>Total Sale</th>
<th>Ebay Fees</th>
<th>PayPal Fees</th>
<th>Cost Price</th>
<th>Profit</th>
<th>Notes</th>
<th>Status</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php foreach ($data as $row): ?>
<tr>
<td><a href="view-specific-software-sale.php?id=<?= $row['id']; ?>"><?php echo $row['id']; ?></a></td>
<td><?php echo $row['customer_pp_email']; ?></td>
<td><?php echo date("d/m/Y", strtotime($row['sales_month'])); ?></td>
<td><?php echo $row['software_title']; ?></td>
<td><?php echo $row['quantity']; ?></td>
<td><?php echo $row['total_sale']; ?></td>
<td><?php echo $row['ebay_fees']; ?></td>
<td><?php echo $row['paypal_fees']; ?></td>
<td><?php echo '£' . $row['software_cost']; ?></td>
<td><?php echo '£' . $row['profit']; ?></td>
<td><?php echo substr($row['notes'], 0, 25); ?></td>
<td><?php echo $row['status']; ?></td>
<td><a href="add-update-software-sales.php?id=<?= $row['id']; ?>">Edit</a></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php
// Count the total records
$total_records = mysqli_num_rows($data);
//Using ceil function to divide the total records on per page
$total_pages = ceil($total_records / $per_page);
//Going to first page
echo "<center><a href='search-data.php?page=1'>".'First Page'."</a>";
for ($i=1; $i<=$total_pages; $i++) {
echo "<a href='search-data.php?page=".$i."'>".$i."</a>";
};
// Going to last page
echo "<a href='search-data.php?page=$total_pages'>".'Last Page'."</a></center>";
?>
<div class="box-right" style="height: 90px !important">
<div class="box-middle-content">
<?php
include('connect.php');
$data = [];
if (isset($_GET['d1']) && isset($_GET['d2'])) {
$d1 = strtotime($_GET['d1']);
$d2 = strtotime($_GET['d2']);
if ($d1 && $d2) {
$start_date = date('Y-m-d', $d1);
$end_date = date('Y-m-d', $d2);
$result = $db->prepare("SELECT SUM(profit) FROM purchased_software WHERE sales_month BETWEEN :a AND :b");
$result->bindParam(':a', $start_date);
$result->bindParam(':b', $end_date);
$exec = $result->execute();
if ($exec) {
$data = $result->fetchAll();
}
}
}
foreach ($data as $row):
echo 'Total Software Profit for ' . $start_date = date('d-m-Y', $d1) . ' AND ' . $end_date = date('d-m-Y', $d2) . '<br>' . '£' . $row['SUM(profit)'];
endforeach;
?>
</div>
</div>
<a href="view-software-sales.php">BACK TO VIEW ALL SOFTWARE SALES</a>
</body>
</html>
Thank you in advance
Ian
bojangles
02-06-2016, 01:18 PM
Is this the 'search-data.php' file stated in the error message? I guess I don't see very clearly what line 148 is to know how to identify the problem.
ianhaney
02-06-2016, 01:59 PM
Yeah, sorry the error message I got on search-data.php is Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, array given in /home/sites/it-doneright.co.uk/public_html/admin/software-sales/search-data.php on line 155
On that line 155 is
$total_records = mysqli_num_rows($data);
styxlawyer
02-06-2016, 05:18 PM
You appear to have included "connect.php" twice. The first is on line 7 and again on line 143. That could be the reason that you cannot connect to the database. Try removing the second instantiation and changing the first to:
require_once('connect.php');
ianhaney
02-06-2016, 08:38 PM
You appear to have included "connect.php" twice. The first is on line 7 and again on line 143. That could be the reason that you cannot connect to the database. Try removing the second instantiation and changing the first to:
require_once('connect.php');
Hi
I have updated the script now to what you said as quoted here but got the following error
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, array given in /home/sites/it-doneright.co.uk/public_html/admin/software-sales/search-data.php on line 149
styxlawyer
02-06-2016, 09:09 PM
It was only a possible error. The page you have quoted returns a 404 error, so nobody can see your modified code.
ianhaney
02-06-2016, 09:19 PM
It was only a possible error. The page you have quoted returns a 404 error, so nobody can see your modified code.
Oh right ok, I have added below the modified code
<?php
ini_set('display_startup_errors',1);
ini_set('display_errors',1);
error_reporting(-1);
session_start();
if ($_SESSION['user']=='') {
header("Location:../index.php");
} else {
include("../config.php");
$sql = $dbh->prepare("SELECT * FROM users WHERE id=?");
$exec = $sql->execute(array($_SESSION['user']));
$user = $exec ? $sql->fetch() : null;
}
require_once('connect.php');
$per_page=5;
if (isset($_GET["page"])) {
$page = $_GET["page"];
}
else {
$page=1;
}
// Page will start from 0 and Multiple by Per Page
$start_from = ($page-1) * $per_page;
$data = [];
if (isset($_GET['d1']) && isset($_GET['d2'])) {
$d1 = strtotime($_GET['d1']);
$d2 = strtotime($_GET['d2']);
if ($d1 && $d2) {
$start_date = date('Y-m-d', $d1);
$end_date = date('Y-m-d', $d2);
$result = $db->prepare("SELECT * FROM purchased_software WHERE sales_month BETWEEN :a AND :b ORDER BY id LIMIT $start_from, $per_page");
$result->bindParam(':a', $start_date);
$result->bindParam(':b', $end_date);
$exec = $result->execute();
if ($exec) {
$data = $result->fetchAll();
}
}
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Search Data Between Two Dates</title>
<link rel="stylesheet" type="text/css" media="screen" href="css/styles.css" />
<link rel="stylesheet" type="text/css" href="css/tcal.css" />
<script type="text/javascript" src="js/tcal.js"></script>
</head>
<body>
<div id="logo">
<img src="images/logo/it-done-right.jpg" alt="" title="">
</div>
<script>
var t;
window.onload=resetTimer;
document.onkeypress=resetTimer;
function logout()
{
alert("You are now logged out.");
location.href='../logout.php';
}
function resetTimer()
{
clearTimeout(t);
t=setTimeout(logout,1800000) //logs out in 30 minutes
}
</script>
<?php if ($user): ?>
<div class='home-content'>
<center>
<h2>Hello, <?= $user['username']; ?></h2>
<a href='../logout.php'>Log Out</a>
<br><br>
<a href='../index.php'>Home</a>
</center>
</div>
<br>
<?php endif; ?>
<?php include("nav-menu.php"); ?>
<br>
<form action="search-data.php" method="get">
From : <input type="text" name="d1" class="tcal" value="" />
To: <input type="text" name="d2" class="tcal" value="" />
<input type="submit" value="Search">
</form>
<table class="view-repairs">
<thead>
<tr>
<th>Software ID</th>
<th>Customer PayPal Email</th>
<th>Sales Date</th>
<th>Software Title</th>
<th>Quantity</th>
<th>Total Sale</th>
<th>Ebay Fees</th>
<th>PayPal Fees</th>
<th>Cost Price</th>
<th>Profit</th>
<th>Notes</th>
<th>Status</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php foreach ($data as $row): ?>
<tr>
<td><a href="view-specific-software-sale.php?id=<?= $row['id']; ?>"><?php echo $row['id']; ?></a></td>
<td><?php echo $row['customer_pp_email']; ?></td>
<td><?php echo date("d/m/Y", strtotime($row['sales_month'])); ?></td>
<td><?php echo $row['software_title']; ?></td>
<td><?php echo $row['quantity']; ?></td>
<td><?php echo $row['total_sale']; ?></td>
<td><?php echo $row['ebay_fees']; ?></td>
<td><?php echo $row['paypal_fees']; ?></td>
<td><?php echo '£' . $row['software_cost']; ?></td>
<td><?php echo '£' . $row['profit']; ?></td>
<td><?php echo substr($row['notes'], 0, 25); ?></td>
<td><?php echo $row['status']; ?></td>
<td><a href="add-update-software-sales.php?id=<?= $row['id']; ?>">Edit</a></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php
// Count the total records
$total_records = mysqli_num_rows($data);
//Using ceil function to divide the total records on per page
$total_pages = ceil($total_records / $per_page);
//Going to first page
echo "<center><a href='search-data.php?page=1'>".'First Page'."</a>";
for ($i=1; $i<=$total_pages; $i++) {
echo "<a href='search-data.php?page=".$i."'>".$i."</a>";
};
// Going to last page
echo "<a href='search-data.php?page=$total_pages'>".'Last Page'."</a></center>";
?>
<div class="box-right" style="height: 90px !important">
<div class="box-middle-content">
<?php
$data = [];
if (isset($_GET['d1']) && isset($_GET['d2'])) {
$d1 = strtotime($_GET['d1']);
$d2 = strtotime($_GET['d2']);
if ($d1 && $d2) {
$start_date = date('Y-m-d', $d1);
$end_date = date('Y-m-d', $d2);
$result = $db->prepare("SELECT SUM(profit) FROM purchased_software WHERE sales_month BETWEEN :a AND :b");
$result->bindParam(':a', $start_date);
$result->bindParam(':b', $end_date);
$exec = $result->execute();
if ($exec) {
$data = $result->fetchAll();
}
}
}
foreach ($data as $row):
echo 'Total Software Profit for ' . $start_date = date('d-m-Y', $d1) . ' AND ' . $end_date = date('d-m-Y', $d2) . '<br>' . '£' . $row['SUM(profit)'];
endforeach;
?>
</div>
</div>
<a href="view-software-sales.php">BACK TO VIEW ALL SOFTWARE SALES</a>
</body>
</html>
styxlawyer
02-06-2016, 11:23 PM
Two things spring to mind. Firstly, mysqli_num_rows() requires a query result as a parameter (see here (http://www.w3schools.com/php/func_mysqli_num_rows.asp)) and $data is not a mysqli result but an array. Secondly, it would be much more sensible to evaluate the size of the array immediately after the data is fetched.
Try this:
if ($exec) {
$data = $result->fetchAll();
$total_records = mysqli_num_rows($result);
}
If that fails I'll have a closer look tomorrow.
Later: Another alternative is to simply get the number of rows in the array '$data' using the sizeof() function.
if ($exec) {
$data = $result->fetchAll();
$total_records = sizeof($data, 0); // Second parameter is optional but if set to 1 will return the total number of elements in the array.
}
ianhaney
02-06-2016, 11:36 PM
Two things spring to mind. Firstly, mysqli_num_rows() requires a query result as a parameter (see here (http://www.w3schools.com/php/func_mysqli_num_rows.asp)) and $data is not a mysqli result but an array. Secondly, it would be much more sensible to evaluate the size of the array immediately after the data is fetched.
Try this:
if ($exec) {
$data = $result->fetchAll();
$total_records = mysqli_num_rows($result);
}
If that fails I'll have a closer look tomorrow.
Thank you really appreciate it, will try that first thing in the morning and post a update if it works or not, thank you again
ianhaney
02-07-2016, 11:35 AM
Hi
Can you please check the modified coding, I used the alternative method you suggested and is paginating the search results to 5 per page but there is no second page where as on the second page should be 4 for the dates I am searching, below is the modified coding
<?php
ini_set('display_startup_errors',1);
ini_set('display_errors',1);
error_reporting(-1);
session_start();
if ($_SESSION['user']=='') {
header("Location:../index.php");
} else {
include("../config.php");
$sql = $dbh->prepare("SELECT * FROM users WHERE id=?");
$exec = $sql->execute(array($_SESSION['user']));
$user = $exec ? $sql->fetch() : null;
}
require_once('connect.php');
$per_page=5;
if (isset($_GET["page"])) {
$page = $_GET["page"];
}
else {
$page=1;
}
// Page will start from 0 and Multiple by Per Page
$start_from = ($page-1) * $per_page;
$data = [];
if (isset($_GET['d1']) && isset($_GET['d2'])) {
$d1 = strtotime($_GET['d1']);
$d2 = strtotime($_GET['d2']);
if ($d1 && $d2) {
$start_date = date('Y-m-d', $d1);
$end_date = date('Y-m-d', $d2);
$result = $db->prepare("SELECT * FROM purchased_software WHERE sales_month BETWEEN :a AND :b ORDER BY id LIMIT $start_from, $per_page");
$result->bindParam(':a', $start_date);
$result->bindParam(':b', $end_date);
$exec = $result->execute();
if ($exec) {
$data = $result->fetchAll();
}
}
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Search Data Between Two Dates</title>
<link rel="stylesheet" type="text/css" media="screen" href="css/styles.css" />
<link rel="stylesheet" type="text/css" href="css/tcal.css" />
<script type="text/javascript" src="js/tcal.js"></script>
</head>
<body>
<div id="logo">
<img src="images/logo/it-done-right.jpg" alt="" title="">
</div>
<script>
var t;
window.onload=resetTimer;
document.onkeypress=resetTimer;
function logout()
{
alert("You are now logged out.");
location.href='../logout.php';
}
function resetTimer()
{
clearTimeout(t);
t=setTimeout(logout,1800000) //logs out in 30 minutes
}
</script>
<?php if ($user): ?>
<div class='home-content'>
<center>
<h2>Hello, <?= $user['username']; ?></h2>
<a href='../logout.php'>Log Out</a>
<br><br>
<a href='../index.php'>Home</a>
</center>
</div>
<br>
<?php endif; ?>
<?php include("nav-menu.php"); ?>
<br>
<form action="search-data.php" method="get">
From : <input type="text" name="d1" class="tcal" value="" />
To: <input type="text" name="d2" class="tcal" value="" />
<input type="submit" value="Search">
</form>
<table class="view-repairs">
<thead>
<tr>
<th>Software ID</th>
<th>Customer PayPal Email</th>
<th>Sales Date</th>
<th>Software Title</th>
<th>Quantity</th>
<th>Total Sale</th>
<th>Ebay Fees</th>
<th>PayPal Fees</th>
<th>Cost Price</th>
<th>Profit</th>
<th>Notes</th>
<th>Status</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php foreach ($data as $row): ?>
<tr>
<td><a href="view-specific-software-sale.php?id=<?= $row['id']; ?>"><?php echo $row['id']; ?></a></td>
<td><?php echo $row['customer_pp_email']; ?></td>
<td><?php echo date("d/m/Y", strtotime($row['sales_month'])); ?></td>
<td><?php echo $row['software_title']; ?></td>
<td><?php echo $row['quantity']; ?></td>
<td><?php echo $row['total_sale']; ?></td>
<td><?php echo $row['ebay_fees']; ?></td>
<td><?php echo $row['paypal_fees']; ?></td>
<td><?php echo '£' . $row['software_cost']; ?></td>
<td><?php echo '£' . $row['profit']; ?></td>
<td><?php echo substr($row['notes'], 0, 25); ?></td>
<td><?php echo $row['status']; ?></td>
<td><a href="add-update-software-sales.php?id=<?= $row['id']; ?>">Edit</a></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php
// Count the total records
$total_records = sizeof($data, 0);
//Using ceil function to divide the total records on per page
$total_pages = ceil($total_records / $per_page);
//Going to first page
echo "<center><a href='search-data.php?page=1'>".'First Page'."</a>";
for ($i=1; $i<=$total_pages; $i++) {
echo "<a href='search-data.php?page=".$i."'>".$i."</a>";
};
// Going to last page
echo "<a href='search-data.php?page=$total_pages'>".'Last Page'."</a></center>";
?>
<div class="box-right" style="height: 90px !important">
<div class="box-middle-content">
<?php
$data = [];
if (isset($_GET['d1']) && isset($_GET['d2'])) {
$d1 = strtotime($_GET['d1']);
$d2 = strtotime($_GET['d2']);
if ($d1 && $d2) {
$start_date = date('Y-m-d', $d1);
$end_date = date('Y-m-d', $d2);
$result = $db->prepare("SELECT SUM(profit) FROM purchased_software WHERE sales_month BETWEEN :a AND :b");
$result->bindParam(':a', $start_date);
$result->bindParam(':b', $end_date);
$exec = $result->execute();
if ($exec) {
$data = $result->fetchAll();
}
}
}
foreach ($data as $row):
echo 'Total Software Profit for ' . $start_date = date('d-m-Y', $d1) . ' AND ' . $end_date = date('d-m-Y', $d2) . '<br>' . '£' . $row['SUM(profit)'];
endforeach;
?>
</div>
</div>
<a href="view-software-sales.php">BACK TO VIEW ALL SOFTWARE SALES</a>
</body>
</html>
styxlawyer
02-07-2016, 12:24 PM
You have set a limit in your search query on line 42:
$result = $db->prepare("SELECT * FROM purchased_software WHERE sales_month BETWEEN :a AND :b ORDER BY id LIMIT $start_from, $per_page");
The value of '$per_page' is set to 5 in line 19 so that query only returns five results! If you want to return all the matching results starting from '$start_from' then you must change LIMIT to OFFSET. Your query should look like this:
$result = $db->prepare("SELECT * FROM purchased_software WHERE sales_month BETWEEN :a AND :b ORDER BY id OFFSET $start_from");
See here (http://www.w3schools.com/php/php_mysql_select_limit.asp) for the usage of LIMIT and OFFSET in MYSQL.
ianhaney
02-07-2016, 02:19 PM
I just modified the query but gave the error below when I clicked search
Fatal error: Uncaught exception 'PDOException' with message '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 'OFFSET 0' at line 1' in /home/sites/it-doneright.co.uk/public_html/admin/software-sales/search-data.php:45 Stack trace: #0 /home/sites/it-doneright.co.uk/public_html/admin/software-sales/search-data.php(45): PDOStatement->execute() #1 {main} thrown in /home/sites/it-doneright.co.uk/public_html/admin/software-sales/search-data.php on line 45
line 45 is
$exec = $result->execute();
shall I paste the whole code again?
styxlawyer
02-07-2016, 02:24 PM
Hmmm, it doesn't seem to like an OFFSET of 0. Try this in your query:
OFFSET $start_from+1
ianhaney
02-07-2016, 02:51 PM
Sorry I modified the query and now got this error
Fatal error: Uncaught exception 'PDOException' with message '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 'OFFSET 0+0' at line 2' in /home/sites/it-doneright.co.uk/public_html/admin/software-sales/search-data.php:46 Stack trace: #0 /home/sites/it-doneright.co.uk/public_html/admin/software-sales/search-data.php(46): PDOStatement->execute() #1 {main} thrown in /home/sites/it-doneright.co.uk/public_html/admin/software-sales/search-data.php on line 46
On line 46 is
$exec = $result->execute();
the query looks like
$result = $db->prepare("SELECT * FROM purchased_software WHERE sales_month BETWEEN :a AND :b ORDER BY id
OFFSET $start_from+0");
styxlawyer
02-07-2016, 03:09 PM
First try:
$result = $db->prepare("SELECT * FROM purchased_software WHERE sales_month BETWEEN :a AND :b ORDER BY id OFFSET $start_from+1");
and if that doesn't work try:
$startxx = $start_from+1;
$result = $db->prepare("SELECT * FROM purchased_software WHERE sales_month BETWEEN :a AND :b ORDER BY id OFFSET $startxx");
ianhaney
02-07-2016, 03:16 PM
I tried the first way and got the following error
Fatal error: Uncaught exception 'PDOException' with message '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 'OFFSET 0+1' at line 1' in /home/sites/it-doneright.co.uk/public_html/admin/software-sales/search-data.php:45 Stack trace: #0 /home/sites/it-doneright.co.uk/public_html/admin/software-sales/search-data.php(45): PDOStatement->execute() #1 {main} thrown in /home/sites/it-doneright.co.uk/public_html/admin/software-sales/search-data.php on line 45
So then tried the second way and got the following error
Fatal error: Uncaught exception 'PDOException' with message '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 'OFFSET 1' at line 1' in /home/sites/it-doneright.co.uk/public_html/admin/software-sales/search-data.php:46 Stack trace: #0 /home/sites/it-doneright.co.uk/public_html/admin/software-sales/search-data.php(46): PDOStatement->execute() #1 {main} thrown in /home/sites/it-doneright.co.uk/public_html/admin/software-sales/search-data.php on line 46
styxlawyer
02-07-2016, 03:46 PM
I've had a scour through Google and it looks as though OFFSET on its own is invalid. The only way to get all the records from a particular start point will be to set an impossibly large value for number of records retrieved. Like this:
$result = $db->prepare("SELECT * FROM purchased_software WHERE sales_month BETWEEN :a AND :b ORDER BY id LIMIT $start_from, 1000000");
ianhaney
02-07-2016, 04:19 PM
I've had a scour through Google and it looks as though OFFSET on its own is invalid. The only way to get all the records from a particular start point will be to set an impossibly large value for number of records retrieved. Like this:
$result = $db->prepare("SELECT * FROM purchased_software WHERE sales_month BETWEEN :a AND :b ORDER BY id LIMIT $start_from, 1000000");
This worked but lost all the results, it does not paginate them to 5 per page for example?
styxlawyer
02-07-2016, 04:32 PM
Without access to your live page, I can only guess what's going on.
ianhaney
02-07-2016, 04:41 PM
Sorry I did not mean lost all the records, I meant it displays all the records on one page insteading of paginate them to 5 per page, sorry I can't give access to the live page as has customer data on and our own business info, sorry
styxlawyer
02-07-2016, 07:50 PM
OK, so you are getting data back from the the query. It's only the start point that seems to be the problem.
You seem to have two separate tabulated queries on that page. Which is the one that isn't working correctly?
ianhaney
02-07-2016, 10:49 PM
OK, so you are getting data back from the the query. It's only the start point that seems to be the problem.
You seem to have two separate tabulated queries on that page. Which is the one that isn't working correctly?
Hi
Sorry for my late reply, yeah am getting data back from the query
I think it is this one that is not working correctly
$result = $db->prepare("SELECT * FROM purchased_software WHERE sales_month BETWEEN :a AND :b ORDER BY id LIMIT $start_from, 1000000");
the data is not being paginated, I have the following
$per_page=5;
if (isset($_GET["page"])) {
$page = $_GET["page"];
}
else {
$page=1;
}
but the $per_page is not being in the query, so do I need to do LIMIT $per_page or something in the query above?
styxlawyer
02-07-2016, 10:54 PM
Putting the $per_page variable back into the query should limit it to 5 entries being returned for each query:
$result = $db->prepare("SELECT * FROM purchased_software WHERE sales_month BETWEEN :a AND :b ORDER BY id LIMIT $start_from, $per_page");
... but I thought you said earlier that wasn't working. I have a dummy database I created for your SMS problem, so I'll add a few more lines and try a few things.
ianhaney
02-07-2016, 10:56 PM
I can't remember now, sorry, I'll try it now and see what happens, oh right ok cool, the SMS system is working now
ianhaney
02-07-2016, 11:02 PM
right I tried it with the $per_page in the query and you was right, it did not work, it did make the records limited to 5 per page but it did not display page two etc link
below is the code for the query I now have the paginated links
$result = $db->prepare("SELECT * FROM purchased_software WHERE sales_month BETWEEN :a AND :b ORDER BY id LIMIT $start_from, $per_page");
//Using ceil function to divide the total records on per page
$total_pages = ceil($total_records / $per_page);
//Going to first page
echo "<center><a href='search-data.php?page=1'>".'First Page'."</a>";
for ($i=1; $i<=$total_pages; $i++) {
echo "<a href='search-data.php?page=".$i."'>".$i."</a>";
};
// Going to last page
echo "<a href='search-data.php?page=$total_pages'>".'Last Page'."</a></center>";
ianhaney
02-08-2016, 03:53 PM
Hi
UPDATE
Just seeing if anyone has a update or script change to help me, with the $per_page in the query, the records are paginated to 5 on the first page, but all I get down the bottom is First Page1LastPage, I have around 25 records so should have 1 2 3 4 5 down the bottom for the pagination links, the code looks like the following
Hope someone can help me
<?php
ini_set('display_startup_errors',1);
ini_set('display_errors',1);
error_reporting(-1);
session_start();
if ($_SESSION['user']=='') {
header("Location:../index.php");
} else {
include("../config.php");
$sql = $dbh->prepare("SELECT * FROM users WHERE id=?");
$exec = $sql->execute(array($_SESSION['user']));
$user = $exec ? $sql->fetch() : null;
}
require_once('connect.php');
$per_page=5;
if (isset($_GET["page"])) {
$page = $_GET["page"];
}
else {
$page=1;
}
// Page will start from 0 and Multiply by Per Page
$start_from = ($page-1) * $per_page;
$data = [];
if (isset($_GET['d1']) && isset($_GET['d2'])) {
$d1 = strtotime($_GET['d1']);
$d2 = strtotime($_GET['d2']);
if ($d1 && $d2) {
$start_date = date('Y-m-d', $d1);
$end_date = date('Y-m-d', $d2);
$result = $db->prepare("SELECT * FROM purchased_software WHERE sales_month BETWEEN :a AND :b ORDER BY id LIMIT $start_from, $per_page");
$result->bindParam(':a', $start_date);
$result->bindParam(':b', $end_date);
$exec = $result->execute();
if ($exec) {
$data = $result->fetchAll();
}
}
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Search Data Between Two Dates</title>
<link rel="stylesheet" type="text/css" media="screen" href="css/styles.css" />
<link rel="stylesheet" type="text/css" href="css/tcal.css" />
<script type="text/javascript" src="js/tcal.js"></script>
</head>
<body>
<div id="logo">
<img src="images/logo/it-done-right.jpg" alt="" title="">
</div>
<script>
var t;
window.onload=resetTimer;
document.onkeypress=resetTimer;
function logout()
{
alert("You are now logged out.");
location.href='../logout.php';
}
function resetTimer()
{
clearTimeout(t);
t=setTimeout(logout,1800000) //logs out in 30 minutes
}
</script>
<?php if ($user): ?>
<div class='home-content'>
<center>
<h2>Hello, <?= $user['username']; ?></h2>
<a href='../logout.php'>Log Out</a>
<br><br>
<a href='../index.php'>Home</a>
</center>
</div>
<br>
<?php endif; ?>
<?php include("nav-menu.php"); ?>
<br>
<form action="search-data.php" method="get">
From : <input type="text" name="d1" class="tcal" value="" />
To: <input type="text" name="d2" class="tcal" value="" />
<input type="submit" value="Search">
</form>
<table class="view-repairs">
<thead>
<tr>
<th>Software ID</th>
<th>Customer PayPal Email</th>
<th>Sales Date</th>
<th>Software Title</th>
<th>Quantity</th>
<th>Total Sale</th>
<th>Ebay Fees</th>
<th>PayPal Fees</th>
<th>Cost Price</th>
<th>Profit</th>
<th>Notes</th>
<th>Status</th>
<th>Sold By</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php foreach ($data as $row): ?>
<tr>
<td><a href="view-specific-software-sale.php?id=<?= $row['id']; ?>"><?php echo $row['id']; ?></a></td>
<td><?php echo $row['customer_pp_email']; ?></td>
<td><?php echo date("d/m/Y", strtotime($row['sales_month'])); ?></td>
<td><?php echo $row['software_title']; ?></td>
<td><?php echo $row['quantity']; ?></td>
<td><?php echo $row['total_sale']; ?></td>
<td><?php echo $row['ebay_fees']; ?></td>
<td><?php echo $row['paypal_fees']; ?></td>
<td><?php echo '£' . $row['software_cost']; ?></td>
<td><?php echo '£' . $row['profit']; ?></td>
<td><?php echo substr($row['notes'], 0, 25); ?></td>
<td><?php echo $row['status']; ?></td>
<td><?php echo $row['sold_by']; ?></td>
<td><a href="add-update-software-sales.php?id=<?= $row['id']; ?>">Edit</a></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php
// Count the total records
$total_records = sizeof($data, 0);
//Using ceil function to divide the total records on per page
$total_pages = ceil($total_records / $per_page);
//Going to first page
echo "<center><a href='search-data.php?page=1'>".'First Page'."</a>";
for ($i=1; $i<=$total_pages; $i++) {
echo "<a href='search-data.php?page=".$i."'>".$i."</a>";
};
// Going to last page
echo "<a href='search-data.php?page=$total_pages'>".'Last Page'."</a></center>";
?>
<div class="box-right" style="height: 90px !important">
<div class="box-middle-content">
<?php
$data = [];
if (isset($_GET['d1']) && isset($_GET['d2'])) {
$d1 = strtotime($_GET['d1']);
$d2 = strtotime($_GET['d2']);
if ($d1 && $d2) {
$start_date = date('Y-m-d', $d1);
$end_date = date('Y-m-d', $d2);
$result = $db->prepare("SELECT SUM(profit) FROM purchased_software WHERE sales_month BETWEEN :a AND :b");
$result->bindParam(':a', $start_date);
$result->bindParam(':b', $end_date);
$exec = $result->execute();
if ($exec) {
$data = $result->fetchAll();
}
}
}
foreach ($data as $row):
echo 'Total Software Profit for ' . $start_date = date('d-m-Y', $d1) . ' AND ' . $end_date = date('d-m-Y', $d2) . '<br>' . '£' . $row['SUM(profit)'];
endforeach;
?>
</div>
</div>
<a href="view-software-sales.php">BACK TO VIEW ALL SOFTWARE SALES</a>
</body>
</html>
styxlawyer
02-09-2016, 12:00 AM
I did say I would look at this for you and I have done so. Unfortunately, I do also have other things to do in my life, so your problem isn't top priority. However, I now know what your problem is. Here's a breakdown of why it doesn't work:
In line 47 you fetch some rows of data to a maximum of the value stored in '$per_page'.
In line 151 you evaluate how many rows of data you received and put it in '$total_records'. So the value stored in '$total_records' will not be any larger than the value stored in '$per_page'.
In line 154 you divide the value in '$total_records' by the value in '$per_page' and store the result in '$total_pages'. Thus '$total_pages' will either be 0 or 1 and never higher.
That's why you only see one link under your table and cannot retrieve any more records. At no point do you evaluate the total number of records in the database.
Hope that helps.
ianhaney
02-09-2016, 11:13 AM
Hi
Thank you for replying, appreciate it, ahh yeah I know I understand mine isn't top priority
I guess then them lines 47, 151 and 154 need amending, is that right? or do I need to evaluate the total number of the records in the database as you say, if so how do I do that?
ianhaney
02-09-2016, 11:16 AM
Would the total number of records in the db table look something like the following
$result = mysqli_query("SELECT count(*) FROM purchased_software");
I ma not sure though how to do it in PDO rather than mysqli?
styxlawyer
02-09-2016, 11:16 AM
You are effectively using recursion in this page and, as a C/C++ programmer of some 30+ years experience, I can tell you that is one of the most difficult techniques to master. For recursion to work correctly each call to the function (or PHP page in your case) needs to know where it has come from as well as where it is going. The recursive calls to your page (clicked links) only contain one parameter which is where you wish to go. There is no information carried in the URL to tell the receiving page where the call has originated. You need to add a second parameter to your links to tell the PHP page at which set of results the browser is currently looking.
There are a number of other things which are going to catch you out. Firstly, you are extracting only some of the results from the database according to an arbitrary date range. Each set of results will probably not be starting at the first entry in the database, so you are going to have difficulty using '$start_from' in your query on line 42. The OFFSET attached to the LIMIT in the query relates to the first record in the database and not to the first record you have recovered. You need to evaluate '$start_from' such that it points to the database offset of the first recovered record and not the first record. Also, this query will probably fail if the recovered records aren't consecutive in the database as the value calculated in '$start_from' may point to a record which isn't one you require to display.
I suggest you rethink your page and do not use recursion. Make one query to the database, return all the results to the browser and then use JavaScript on the browser to paginate the results. This will be fine unless you are going to return tens of thousands of results in which case the resulting page will be huge but, since you are running this page on an internal server, that may not be a problem.
ianhaney
02-09-2016, 11:19 AM
I did update the query line to the following
$result = $db->prepare("SELECT COUNT(*) FROM purchased_software WHERE sales_month BETWEEN :a AND :b ORDER BY id LIMIT 5 offset 0");
but when I ran the search, I get undefined index errors like the following
Notice: Undefined index: id in /home/sites/it-doneright.co.uk/public_html/admin/software-sales/search-data.php on line 129
Notice: Undefined index: customer_pp_email in /home/sites/it-doneright.co.uk/public_html/admin/software-sales/search-data.php on line 130
Notice: Undefined index: sales_month in /home/sites/it-doneright.co.uk/public_html/admin/software-sales/search-data.php on line 131 01/01/1970
Notice: Undefined index: software_title in /home/sites/it-doneright.co.uk/public_html/admin/software-sales/search-data.php on line 132
Notice: Undefined index: quantity in /home/sites/it-doneright.co.uk/public_html/admin/software-sales/search-data.php on line 133
Notice: Undefined index: total_sale in /home/sites/it-doneright.co.uk/public_html/admin/software-sales/search-data.php on line 134
Notice: Undefined index: ebay_fees in /home/sites/it-doneright.co.uk/public_html/admin/software-sales/search-data.php on line 135
Notice: Undefined index: paypal_fees in /home/sites/it-doneright.co.uk/public_html/admin/software-sales/search-data.php on line 136
Notice: Undefined index: software_cost in /home/sites/it-doneright.co.uk/public_html/admin/software-sales/search-data.php on line 137 £
Notice: Undefined index: profit in /home/sites/it-doneright.co.uk/public_html/admin/software-sales/search-data.php on line 138 £
Notice: Undefined index: notes in /home/sites/it-doneright.co.uk/public_html/admin/software-sales/search-data.php on line 139
Notice: Undefined index: status in /home/sites/it-doneright.co.uk/public_html/admin/software-sales/search-data.php on line 140
Notice: Undefined index: sold_by in /home/sites/it-doneright.co.uk/public_html/admin/software-sales/search-data.php on line 141
styxlawyer
02-09-2016, 01:21 PM
Yes, that's exactly I would expect from a query which is returning a COUNT. There will be no fields in the returned data just a number of records.
ianhaney
02-09-2016, 01:23 PM
ok, is there a way I can then do the COUNT and the fields to return the data in the same query or would it need to be multiple queries?
styxlawyer
02-09-2016, 02:47 PM
You will have to use two discrete queries, one to determine how many data rows will be returned and the second to actually get that data.
ianhaney
02-09-2016, 02:55 PM
You will have to use two discrete queries, one to determine how many data rows will be returned and the second to actually get that data.
I did think that, how would the script look for that in PDO?
ianhaney
02-09-2016, 03:02 PM
I gave it a go as thought it might be like the following but didn't work
$result = $db->prepare("SELECT COUNT(*) FROM purchased_software");
$result = $db->prepare("SELECT * FROM purchased_software WHERE sales_month BETWEEN :a AND :b ORDER BY id LIMIT 5 offset 0");
styxlawyer
02-09-2016, 03:16 PM
The query will be identical. PDO is just an object-oriented interface to mysqli. I think you need to read this page (https://www.developphp.com/video/PHP/Pagination-MySQLi-Google-Style-Paged-Results-Tutorial) before you try to do any more.
ianhaney
02-09-2016, 04:05 PM
I have looked at the link you provided and got the script from it and just got one little issue, it comes up with the error message below
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in /home/sites/it-doneright.co.uk/public_html/admin/software-sales/search-data.php on line 104
on 104 is the following line
while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
ianhaney
02-09-2016, 04:35 PM
This is my whole code
<?php
ini_set('display_startup_errors',1);
ini_set('display_errors',1);
error_reporting(-1);
session_start();
if ($_SESSION['user']=='') {
header("Location:../index.php");
} else {
include("../config.php");
$sql = $dbh->prepare("SELECT * FROM users WHERE id=?");
$exec = $sql->execute(array($_SESSION['user']));
$user = $exec ? $sql->fetch() : null;
}
include_once("mysqli_connection.php");
// This first query is just to get the total count of rows
$sql = "SELECT COUNT(id) FROM purchased_software";
$query = mysqli_query($con, $sql);
$row = mysqli_fetch_row($query);
// Here we have the total row count
$rows = $row[0];
// This is the number of results we want displayed per page
$page_rows = 5;
// This tells us the page number of our last page
$last = ceil($rows/$page_rows);
// This makes sure $last cannot be less than 1
if($last < 1){
$last = 1;
}
// Establish the $pagenum variable
$pagenum = 1;
// Get pagenum from URL vars if it is present, else it is = 1
if(isset($_GET['pn'])){
$pagenum = preg_replace('#[^0-9]#', '', $_GET['pn']);
}
// This makes sure the page number isn't below 1, or more than our $last page
if ($pagenum < 1) {
$pagenum = 1;
} else if ($pagenum > $last) {
$pagenum = $last;
}
// This sets the range of rows to query for the chosen $pagenum
$limit = 'LIMIT ' .($pagenum - 1) * $page_rows .',' .$page_rows;
$data = [];
if (isset($_GET['d1']) && isset($_GET['d2'])) {
$d1 = strtotime($_GET['d1']);
$d2 = strtotime($_GET['d2']);
if ($d1 && $d2) {
$start_date = date('Y-m-d', $d1);
$end_date = date('Y-m-d', $d2);
$result = $db->prepare("SELECT * FROM purchased_software WHERE sales_month BETWEEN :a AND :b ORDER BY id");
$result->bindParam(':a', $start_date);
$result->bindParam(':b', $end_date);
$exec = $result->execute();
if ($exec) {
$data = $result->fetchAll();
}
}
}
// This is your query again, it is for grabbing just one page worth of rows by applying $limit
$sql = "SELECT id, customer_pp_email, sales_month, software_title, quantity, total_sale, ebay_fees, paypal_fees, cost_price, profit, notes, status, sold_by FROM purchased_software sales_month BETWEEN :a AND :b ORDER BY id ORDER BY id DESC $limit";
$query = mysqli_query($con, $sql);
// This shows the user what page they are on, and the total number of pages
$textline1 = "Sales (<b>$rows</b>)";
$textline2 = "Page <b>$pagenum</b> of <b>$last</b>";
// Establish the $paginationCtrls variable
$paginationCtrls = '';
// If there is more than 1 page worth of results
if($last != 1){
/* First we check if we are on page one. If we are then we don't need a link to
the previous page or the first page so we do nothing. If we aren't then we
generate links to the first page, and to the previous page. */
if ($pagenum > 1) {
$previous = $pagenum - 1;
$paginationCtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$previous.'">Previous</a> ';
// Render clickable number links that should appear on the left of the target page number
for($i = $pagenum-4; $i < $pagenum; $i++){
if($i > 0){
$paginationCtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$i.'">'.$i.'</a> ';
}
}
}
// Render the target page number, but without it being a link
$paginationCtrls .= ''.$pagenum.' ';
// Render clickable number links that should appear on the right of the target page number
for($i = $pagenum+1; $i <= $last; $i++){
$paginationCtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$i.'">'.$i.'</a> ';
if($i >= $pagenum+4){
break;
}
}
// This does the same as above, only checking if we are on the last page, and then generating the "Next"
if ($pagenum != $last) {
$next = $pagenum + 1;
$paginationCtrls .= ' <a href="'.$_SERVER['PHP_SELF'].'?pn='.$next.'">Next</a> ';
}
}
$list = '';
while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
$id = $row["id"];
$firstname = $row["customer_pp_email"];
$lastname = $row["sales_month"];
$datemade = $row["software_title"];
$datemade = strftime("%b %d, %Y", strtotime($datemade));
$list .= '<p><a href="testimonial.php?id='.$id.'">'.$firstname.' '.$lastname.' Testimonial</a> - Click the link to view this testimonial<br>Written '.$datemade.'</p>';
}
// Close your database connection
mysqli_close($con);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Search Data Between Two Dates</title>
<link rel="stylesheet" type="text/css" media="screen" href="css/styles.css" />
<link rel="stylesheet" type="text/css" href="css/tcal.css" />
<script type="text/javascript" src="js/tcal.js"></script>
</head>
<body>
<div id="logo">
<img src="images/logo/it-done-right.jpg" alt="" title="">
</div>
<script>
var t;
window.onload=resetTimer;
document.onkeypress=resetTimer;
function logout()
{
alert("You are now logged out.");
location.href='../logout.php';
}
function resetTimer()
{
clearTimeout(t);
t=setTimeout(logout,1800000) //logs out in 30 minutes
}
</script>
<?php if ($user): ?>
<div class='home-content'>
<center>
<h2>Hello, <?= $user['username']; ?></h2>
<a href='../logout.php'>Log Out</a>
<br><br>
<a href='../index.php'>Home</a>
</center>
</div>
<br>
<?php endif; ?>
<?php include("nav-menu.php"); ?>
<br>
<form action="search-data.php" method="get">
From : <input type="text" name="d1" class="tcal" value="" />
To: <input type="text" name="d2" class="tcal" value="" />
<input type="submit" value="Search">
</form>
<div>
<h2><?php echo $textline1; ?> Paged</h2>
<p><?php echo $textline2; ?></p>
<p><?php echo $list; ?></p>
<div id="pagination_controls"><?php echo $paginationCtrls; ?></div>
</div>
</body>
</html>
styxlawyer
02-09-2016, 05:09 PM
There's far too much extraneous stuff in that page to make any sense of it at all.
I suggest you start off with only the code from the page I linked to and modify the queries to match your database. Once you have that working then you can go on and add the additional code required for the other queries. You are trying to jump too far ahead before you have the basic page working.
KISS.
ianhaney
02-09-2016, 06:11 PM
I took everything out of it apart from the pagination coding, logo and menu and still got the same error, below is the code
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in /home/sites/it-doneright.co.uk/public_html/admin/software-sales/search-data.php on line 82
<?php
ini_set('display_startup_errors',1);
ini_set('display_errors',1);
error_reporting(-1);
include_once("mysqli_connection.php");
// This first query is just to get the total count of rows
$sql = "SELECT COUNT(id) FROM purchased_software";
$r = mysqli_query($con,$sql)
or die("Error: ".mysqli_error($con));
$query = mysqli_query($con, $sql);
$row = mysqli_fetch_row($query);
// Here we have the total row count
$rows = $row[0];
// This is the number of results we want displayed per page
$page_rows = 5;
// This tells us the page number of our last page
$last = ceil($rows/$page_rows);
// This makes sure $last cannot be less than 1
if($last < 1){
$last = 1;
}
// Establish the $pagenum variable
$pagenum = 1;
// Get pagenum from URL vars if it is present, else it is = 1
if(isset($_GET['pn'])){
$pagenum = preg_replace('#[^0-9]#', '', $_GET['pn']);
}
// This makes sure the page number isn't below 1, or more than our $last page
if ($pagenum < 1) {
$pagenum = 1;
} else if ($pagenum > $last) {
$pagenum = $last;
}
// This sets the range of rows to query for the chosen $pagenum
$limit = 'LIMIT ' .($pagenum - 1) * $page_rows .',' .$page_rows;
// This is your query again, it is for grabbing just one page worth of rows by applying $limit
$sql = "SELECT id, customer_pp_email, sales_month, software_title, quantity, total_sale, ebay_fees, paypal_fees, cost_price, profit, notes, status, sold_by FROM purchased_software ORDER BY id DESC $limit";
$query = mysqli_query($con, $sql);
// This shows the user what page they are on, and the total number of pages
$textline1 = "Sales (<b>$rows</b>)";
$textline2 = "Page <b>$pagenum</b> of <b>$last</b>";
// Establish the $paginationCtrls variable
$paginationCtrls = '';
// If there is more than 1 page worth of results
if($last != 1){
/* First we check if we are on page one. If we are then we don't need a link to
the previous page or the first page so we do nothing. If we aren't then we
generate links to the first page, and to the previous page. */
if ($pagenum > 1) {
$previous = $pagenum - 1;
$paginationCtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$previous.'">Previous</a> ';
// Render clickable number links that should appear on the left of the target page number
for($i = $pagenum-4; $i < $pagenum; $i++){
if($i > 0){
$paginationCtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$i.'">'.$i.'</a> ';
}
}
}
// Render the target page number, but without it being a link
$paginationCtrls .= ''.$pagenum.' ';
// Render clickable number links that should appear on the right of the target page number
for($i = $pagenum+1; $i <= $last; $i++){
$paginationCtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$i.'">'.$i.'</a> ';
if($i >= $pagenum+4){
break;
}
}
// This does the same as above, only checking if we are on the last page, and then generating the "Next"
if ($pagenum != $last) {
$next = $pagenum + 1;
$paginationCtrls .= ' <a href="'.$_SERVER['PHP_SELF'].'?pn='.$next.'">Next</a> ';
}
}
$list = '';
while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
$id = $row["id"];
$customer_pp_email = $row["customer_pp_email"];
$sales_month = $row["sales_month"];
$software_title = $row["software_title"];
$datemade = strftime("%b %d, %Y", strtotime($datemade));
$list .= '<p>' . $customer_pp_email . $sales_month . $software_title . $datemade.'</p>';
}
// Close your database connection
mysqli_close($con);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Search Data Between Two Dates</title>
<link rel="stylesheet" type="text/css" media="screen" href="css/styles.css" />
</head>
<body>
<div id="logo">
<img src="images/logo/it-done-right.jpg" alt="" title="">
</div>
<br><br>
<?php include("nav-menu.php"); ?>
<br><br>
<div>
<h2><?php echo $textline1; ?> Paged</h2>
<p><?php echo $textline2; ?></p>
<p><?php echo $list; ?></p>
<div id="pagination_controls"><?php echo $paginationCtrls; ?></div>
</div>
</body>
</html>
ianhaney
02-09-2016, 06:24 PM
Right am slowly winning
I have got the pagination all working and paginating the results, just need to put the search form back in to be able to search for data between the two dates, I will try that now and post a update for that section of code
ianhaney
02-09-2016, 06:30 PM
It def does not like the search form and the dates, is it because that is in PDO and the rest is mysqli
I get the error message again like before
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in /home/sites/it-doneright.co.uk/public_html/admin/software-sales/search-data.php on line 100
below is what I have so far
<?php
ini_set('display_startup_errors',1);
ini_set('display_errors',1);
error_reporting(-1);
include_once("mysqli_connection.php");
// This first query is just to get the total count of rows
$sql = "SELECT COUNT(id) FROM purchased_software";
$r = mysqli_query($con,$sql)
or die("Error: ".mysqli_error($con));
$query = mysqli_query($con, $sql);
$row = mysqli_fetch_row($query);
// Here we have the total row count
$rows = $row[0];
// This is the number of results we want displayed per page
$page_rows = 5;
// This tells us the page number of our last page
$last = ceil($rows/$page_rows);
// This makes sure $last cannot be less than 1
if($last < 1){
$last = 1;
}
// Establish the $pagenum variable
$pagenum = 1;
// Get pagenum from URL vars if it is present, else it is = 1
if(isset($_GET['pn'])){
$pagenum = preg_replace('#[^0-9]#', '', $_GET['pn']);
}
// This makes sure the page number isn't below 1, or more than our $last page
if ($pagenum < 1) {
$pagenum = 1;
} else if ($pagenum > $last) {
$pagenum = $last;
}
// This sets the range of rows to query for the chosen $pagenum
$limit = 'LIMIT ' .($pagenum - 1) * $page_rows .',' .$page_rows;
$data = [];
if (isset($_GET['d1']) && isset($_GET['d2'])) {
$d1 = strtotime($_GET['d1']);
$d2 = strtotime($_GET['d2']);
if ($d1 && $d2) {
$start_date = date('Y-m-d', $d1);
$end_date = date('Y-m-d', $d2);
$result = $con->prepare("SELECT * FROM purchased_software WHERE sales_month BETWEEN :a AND :b ORDER BY id");
$result->bindParam(':a', $start_date);
$result->bindParam(':b', $end_date);
$exec = $result->execute();
if ($exec) {
$data = $result->fetchAll();
}
}
}
// This is your query again, it is for grabbing just one page worth of rows by applying $limit
$sql = "SELECT id, customer_pp_email, sales_month, software_title, quantity, total_sale, ebay_fees, paypal_fees, software_cost, profit, notes, status, sold_by FROM purchased_software WHERE sales_month BETWEEN :a AND :b ORDER BY id DESC $limit";
$query = mysqli_query($con, $sql);
// This shows the user what page they are on, and the total number of pages
$textline1 = "Sales (<b>$rows</b>)";
$textline2 = "Page <b>$pagenum</b> of <b>$last</b>";
// Establish the $paginationCtrls variable
$paginationCtrls = '';
// If there is more than 1 page worth of results
if($last != 1){
/* First we check if we are on page one. If we are then we don't need a link to
the previous page or the first page so we do nothing. If we aren't then we
generate links to the first page, and to the previous page. */
if ($pagenum > 1) {
$previous = $pagenum - 1;
$paginationCtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$previous.'">Previous</a> ';
// Render clickable number links that should appear on the left of the target page number
for($i = $pagenum-4; $i < $pagenum; $i++){
if($i > 0){
$paginationCtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$i.'">'.$i.'</a> ';
}
}
}
// Render the target page number, but without it being a link
$paginationCtrls .= ''.$pagenum.' ';
// Render clickable number links that should appear on the right of the target page number
for($i = $pagenum+1; $i <= $last; $i++){
$paginationCtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$i.'">'.$i.'</a> ';
if($i >= $pagenum+4){
break;
}
}
// This does the same as above, only checking if we are on the last page, and then generating the "Next"
if ($pagenum != $last) {
$next = $pagenum + 1;
$paginationCtrls .= ' <a href="'.$_SERVER['PHP_SELF'].'?pn='.$next.'">Next</a> ';
}
}
$list = '';
while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
$id = $row["id"];
$customer_pp_email = $row["customer_pp_email"];
$sales_month = strftime("%b %d, %Y", strtotime($sales_month));
$software_title = $row["software_title"];
$quantity = $row["quantity"];
$total_sale = $row["total_sale"];
$ebay_fees = $row["ebay_fees"];
$paypal_fees = $row["paypal_fees"];
$software_cost = $row["software_cost"];
$profit = $row["profit"];
$notes = $row["notes"];
$status = $row["status"];
$sold_by = $row["sold_by"];
$list .= '<p>' . $customer_pp_email . $sales_month . $software_title . $quantity . $total_sale . $ebay_fees . $paypal_fees . $software_cost . $profit . $notes . $status . $sold_by.'</p>';
}
// Close your database connection
mysqli_close($con);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Search Data Between Two Dates</title>
<link rel="stylesheet" type="text/css" media="screen" href="css/styles.css" />
<link rel="stylesheet" type="text/css" href="css/tcal.css" />
<script type="text/javascript" src="js/tcal.js"></script>
</head>
<body>
<div id="logo">
<img src="images/logo/it-done-right.jpg" alt="" title="">
</div>
<br><br>
<?php include("nav-menu.php"); ?>
<br><br>
<form action="search-data.php" method="get">
From : <input type="text" name="d1" class="tcal" value="" />
To: <input type="text" name="d2" class="tcal" value="" />
<input type="submit" value="Search">
</form>
<div>
<h2><?php echo $textline1; ?> Paged</h2>
<p><?php echo $textline2; ?></p>
<p><?php echo $list; ?></p>
<div id="pagination_controls"><?php echo $paginationCtrls; ?></div>
</div>
</body>
</html>
ianhaney
02-09-2016, 06:41 PM
Is there a way to use mysqli rather than PDO for the search form dates as looking on Google but can't seem to find anything yet
ianhaney
02-09-2016, 09:41 PM
I have made these amendments but know I have got it wrong cause of the error messages but just seeing if am close or how to resolve the error messages, once working I will sort out the sql injection
I added in the following above the sql query
$a=$_POST['from'];
$b=$_POST['to'];
I added the following to the query
WHERE sales_month BETWEEN '$a' AND '$b'
I added the form in as well which is below
<form action="search-data.php" method="post">
From : <input type="text" name="from" class="tcal" />
To: <input type="text" name="to" class="tcal" />
<input name="" type="submit" value="Search">
</form>
With them amendments, I get the followign error messages
Notice: Undefined index: from in /home/sites/it-doneright.co.uk/public_html/admin/software-sales/search-data.php on line 42
Notice: Undefined index: to in /home/sites/it-doneright.co.uk/public_html/admin/software-sales/search-data.php on line 43
ianhaney
02-09-2016, 10:16 PM
I solved the undefined index errors but amending the two lines to the following
$a = (isset($_POST['from']) ? $_POST['from'] : null);
$b = (isset($_POST['to']) ? $_POST['to'] : null);
but that does not return any results when I search for two different dates using the form?
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.