I'm trying to create a pagination for our status table but the records being displayed is not on par with the records that I want to display. Here's my code:
<?php
$query = mysqli_query($connect,"SELECT * FROM inventory_item ") or die ("Error: Could not fetch rows!");
$count = 0;
if (isset($_GET['page_no']) && $_GET['page_no']!="") {
$page_no = $_GET['page_no'];
} else {
$page_no = 1;
}
$total_records_per_page = 3;
$offset = ($page_no-1) * $total_records_per_page;
$previous_page = $page_no - 1;
$next_page = $page_no + 1;
$adjacents = "2";
$query_count = mysqli_query($connect,"SELECT COUNT(*) As total_records FROM `inventory_item`");
$total_records = mysqli_fetch_array($query_count);
$total_records = $total_records['total_records'];
$total_no_of_pages = ceil($total_records / $total_records_per_page);
$second_last = $total_no_of_pages - 1;
$result = mysqli_query($connect,"SELECT * FROM `inventory_item` LIMIT $offset, $total_records_per_page");
while ($row = mysqli_fetch_array($query))
{
$item_name = $row ['item_name'];
echo '<tr><td>'.$row['item_name']. '</td><td>'.$row['description']. '</td><td>'.$row['unit'].'</td>
<td>'.$row['quantity']. '</td><td>'.$row['price']. '</td>
<td>'.$row['price'] * $row['quantity'].'</td>
<td>'.$row['status'].'</td>
<td><a href="inventory_statusEditRecordsFinal1.php?CO='.$item_name.'" title="Click to edit records" class="buttonn"/> Edit </a></td>
<td><a href="inventory_deletestatusFinal.php?item_name='.$item_name.'" title="Click to delete records" class="delbutton" onclick="return ConfirmDelete()"/> Delete </a></td>
</tr>';
}
mysqli_close($connect);
?>
Here's the result, I wanted to display 3 records per page but it displays all the data.
Related
if (isset($_GET['page_no']) && $_GET['page_no']!="") {
$page_no = $_GET['page_no'];
} else {
$page_no = 1;
}
$total_records_per_page = 9;
$offset = ($page_no-1) * $total_records_per_page;
$previous_page = $page_no - 1;
$next_page = $page_no + 1;
$adjacents = "2";
$result_count = mysqli_query($con,"SELECT COUNT(*) As total_records FROM `products`");
$total_records = mysqli_fetch_array($result_count);
$total_records = $total_records['total_records'];
$total_no_of_pages = ceil($total_records / $total_records_per_page);
$second_last = $total_no_of_pages - 1; // total page minus 1
$result = mysqli_query($con,"SELECT * FROM `products` LIMIT $offset, $total_records_per_page");
while($row = mysqli_fetch_array($result)){
echo "<tr>
<td>".$row['productCode']."</td>
<td>".$row['productName']."</td>
<td>".$row['MSRP']."</td>
<td><button type='submit' class='buy'>Buy Now</button></td>
</tr>";
}
mysqli_close($con);
?>
I need to change this code to PDO format. And I am not really sure what is the same function of mysqli_fetch_array in PDO.
This is how you would do it:
// Execute query and fetch a single cell from the result
$total_records = $PDO->query('SELECT COUNT(*) FROM `products`')->fetch(PDO::FETCH_COLUMN);
$total_no_of_pages = ceil($total_records / $total_records_per_page);
$second_last = $total_no_of_pages - 1; // total page minus 1
// prepare a statement with 2 parameters and execute it
$stmt = $PDO->prepare('SELECT * FROM `products` LIMIT ?,?');
$stmt->execute([$offset, $total_records_per_page]);
// PDO results are easily traversable
foreach ($stmt->fetchAll() as $row) {
echo "<tr>
<td>".$row['productCode']."</td>
<td>".$row['productName']."</td>
<td>".$row['MSRP']."</td>
<td><button type='submit' class='buy'>Buy Now</button></td>
</tr>";
}
I replaced your concatenated query with a prepared statement, which you should always do!
About fetching: You can traverse the records one by one or fetch all of them like I did into an array and foreach on them. There is many different ways to do it. Always remember about many PDO fetch options available: https://phpdelusions.net/pdo#fetchall
I need serial number next to next page, first page from 1 to 10, next from 11 to 20...... but my code prints 1 to 10 for all pages.
<?php
if (isset($_GET['pageno'])) {
$pageno = $_GET['pageno'];
} else {
$pageno = 1;
}
$number = 1;
$no_of_records_per_page = 10;
$offset = ($pageno-1) * $no_of_records_per_page;
$total_pages_sql = "SELECT COUNT(*) FROM regs";
$result = mysqli_query($conn,$total_pages_sql);
$total_rows = mysqli_fetch_array($result)[0];
$total_pages = ceil($total_rows / $no_of_records_per_page);
//print_r($total_pages);
$result = mysqli_query($conn, "SELECT * FROM regs LIMIT $offset, $no_of_records_per_page");
while($res = mysqli_fetch_array($result)) {
// How many elements per page
echo "<tr>";
echo "<td>".$res['FirstName']."</td>";
echo "<td>".$res['Email']."</td>";
echo "<td>".$res['Gender']."</td>";
echo "<td>View | Edit | Delete</td>";
$pageNumber = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$currentNumber = ($pageNumber - 1) * $no_of_records_per_page + $number;
echo "<td> ". $currentNumber++ ." </td>";
$number++;
}
?>
your code should be like that
if (isset($_GET['pageno'])) {
$pageno = $_GET['pageno'];
} else {
$pageno = 1;
}
$number = 1;
$no_of_records_per_page = 10;
$offset = ($pageno-1) * $no_of_records_per_page;
$total_pages_sql = "SELECT COUNT(*) FROM regs";
$result = mysqli_query($conn,$total_pages_sql);
$total_rows = mysqli_fetch_array($result)[0];
$total_pages = ceil($total_rows / $no_of_records_per_page);
//print_r($total_pages);
$result = mysqli_query($conn, "SELECT * FROM regs LIMIT $offset, $no_of_records_per_page");
while($res = mysqli_fetch_array($result)) {
// How many elements per page
echo "<tr>";
echo "<td>".$res['FirstName']."</td>";
echo "<td>".$res['Email']."</td>";
echo "<td>".$res['Gender']."</td>";
echo "<td>View | Edit | Delete</td>";
$currentNumber = ($pageno - 1) * $no_of_records_per_page + $number;
echo "<td> ". $currentNumber++ ." </td>";
$number++;
}
I have a little script that have a Previous & Next button.
My problem is I want to detect & place the ID in those buttons
this is the code from the pagination
<body>
<?php include_once 'data.php'; ?>
<center>
<ul class="pagination">
<?php
if($page_counter == 0){
echo "<li><a href=?start='0' class='active'>0</a></li>";
for($j=1; $j < $paginations; $j++) {
echo "";
}
}else{
echo "<a href=?start=$previous><button>Previous</button></a>";
for($j=0; $j < $paginations; $j++) {
if($j == $page_counter) {
echo " ";
}else{
echo " ";
}
}if($j != $page_counter+1)
echo "<a href=?start=$next><button>Next</button></a>";
}
?>
</ul>
</center>
In this part I have the ID but the problem is I can`t get it with this example to place it into the pagination.
<?php
foreach($result as $row) {
echo '
<div class="card"><button1>
'. $row['notice_id'] .'
<div class="time-left">
<div class="dropdown1">
' ;
}
}
else {
echo '';
}
$conn->close();
?>
</div></div>
This is the code from data.php I think I need to place some code into for detection from the ID
<?php
//include configuration file
require 'configuration.php';
$start = 0; $per_page = 1;
$page_counter = 0;
$next = $page_counter + 1;
$previous = $page_counter - 1;
if(isset($_GET['start'])){
$start = $_GET['start'];
$page_counter = $_GET['start'];
$start = $start * $per_page;
$next = $page_counter + 1;
$previous = $page_counter - 1;
}
// query to get messages from messages table
$q = "SELECT * FROM group_notice LIMIT $start, $per_page";
$query = $db->prepare($q);
$query->execute();
if($query->rowCount() > 0){
$result = $query->fetchAll(PDO::FETCH_ASSOC);
}
// count total number of rows in students table
$count_query = "SELECT * FROM group_notice";
$query = $db->prepare($count_query);
$query->execute();
$count = $query->rowCount();
// calculate the pagination number by dividing total number of rows with per page.
$paginations = ceil($count / $per_page);
?>
You can use some formulas for calculate limit and offset of results.
For example you have 100 records, and you want to paginate it into 10 records per page, and the formula is like below.
// Get Page from Query String
$page = 1;
if(!empty($_GET["page"])){
$page = $_GET["page"];
}
// Get record count
$count_query = "SELECT * FROM group_notice";
$query = $db->prepare($count_query);
$query->execute();
$count = $query->rowCount();
$per_page = 10; // records per page
$pages = ceil($count/$per_page); // get total page
$start = $page * $per_page - $per_page; // get offset
// Do something with your records
$query = db->prepare("SELECT * FROM group_notice LIMIT ?, ?");
$query->bind_param("ii", $start, $per_page); // you should use bind param if you use prepared statement
$query->execute();
if($query->rowCount() > 0){
$result = $query->fetchAll(PDO::FETCH_ASSOC);
}
if($page > 1){
echo 'Prev'; // Print Prev Page
}
for($i = 1; $i <= $pages){
echo ''.$i.''; // Print Page Number
}
if($page < $pages){
echo 'Next'; // Print Next Page
}
Hope it helps.
I have a php pagination page with post submit by user to search something, query mysql in first page is ok, but in NEXT page, i have get white blank page.
Below is the paging code.
$_SESSION['nationality'] = $_POST['nationality'];
$reclimit = 2;
if(isset($_GET['page'])){
$page = $_GET['page'];
} else {
$page = 1;
}
$start = (($page-1) * $reclimit);
$sql = "SELECT userid, name, LEFT(hkid, 4) as hkid, description, nationality, photo_1, photo_2, photo_3 FROM $tbl_name WHERE `nationality` LIKE '%$_SESSION[nationality]%'";
$records = $con->query($sql);
$total = $records->num_rows;
$tpages = ceil($total / $reclimit);
$rec = "SELECT userid, name, LEFT(hkid, 4) as hkid, description, nationality, photo_1, photo_2, photo_3 FROM $tbl_name WHERE `nationality` LIKE '%$_SESSION[nationality]%' LIMIT $start, $reclimit";
$records = $con->query($rec);
while ($row = mysqli_fetch_assoc($records)){
// Loop record
}
// Paging
echo '<ul class="pagination pagination-lg">';
for( $i=1; $i <= $tpages; $i++ ) {
$active = $i == $page ? 'class="active"' : '';
echo "<li $active ><a href='$_SERVER[PHP_SELF]?page=" .$i. "'>" .$i. "</a></li>";
}
echo '</ul>';
This works for me:
<?php
$limit =3;
if (!isset($_GET['pg'])) {
$pg = 1;
} else {
$pg = $_GET['pg'];
}
$start = ($pg - 1 ) * $limit;
$sql = "SELECT * FROM tableName LIMIT $start , $limit";
?>
I used this code in my all pages. Everywhere this code work well. In case of my search page this code count all page by search words but when I click next(2nd or 3rd) page, its cannot display any item. That means sql query for 2nd or 3rd or any others pages not worked. No error also. Pagination page url goes well also.
My Search code:
if(isset($_POST['searchword'])){
$w = mysqli_real_escape_string($dbh, $_POST['searchword']);
$q = strip_tags($w);
$q = trim ($w);
$limit = 12;
if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; };
$start_from = ($page-1) * $limit;
$sql = "SELECT * FROM search_data WHERE MATCH(detail) AGAINST('+$q*' IN BOOLEAN MODE) ORDER BY id ASC LIMIT $start_from, $limit";
$execute = $dbh->query("$sql");
$rowcount = $execute->num_rows ;
if ($rowcount > 0 ) {
$row = $dbh->query($sql) ;
while ($row = $execute->fetch_assoc()) {
$detail = $row['detail'];
$url = $row['url'];
echo $detail;
}
}
}
//Pagination
$sql = "SELECT COUNT(id) FROM search_data WHERE MATCH(detail) AGAINST('+$q*' IN BOOLEAN MODE)";
$result = mysqli_query($dbh,$sql);
$row = mysqli_fetch_row($result);
$total_records = $row[0];
if($total_records > 0){$total_pages = ceil($total_records / $limit); }
$pagLink = "<ul class='pagination'>";
if(!empty($total_pages)){for ($i=1; $i<=$total_pages; $i++) {
$pagLink .= "<li><a href='".$thispage."?page=".$i."'>".$i."</a> </li>";
};
echo $pagLink . "</ul>";
}