multiple checkboxes with paging in php without using javascript and jquery - php

Good day, I have an HTML table and I use paging on it so that only a certain amount of items is shown. The problem is that I need to have multiple selections with checkboxes and that works for a single page but I need that to work between pages. So for example on page 1 you choose 3 items and in the next page you choose 5 items and when GET happens I need to have all those items in one place so that I can store them in a variable.
<?php
include("connect.php"); //database connection file
$limit = 7;
if ( isset($_GET['page']) ) {
$page_no = $_GET['page'];
} else {
$page_no = 1;
}
$start_from = ($page_no-1)*$limit;
$sql = "SELECT * FROM emp_info LIMIT $start_from,$limit ";
$result = mysqli_query($conn , $sql);
?>
<form method="GET" action="project.php?name=<?php echo
$data['name']; ?>">
<div class="container">
<h2>employee information:</h2>
<table class="table table-striped table-hover">
<thead>
<tr>
<th>EmpId</th>
<th>Name</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<?php
$info = "SELECT * FROM emp_info LIMIT $start_from,$limit ";
//query to select the data from database
$query = mysqli_query ($conn , $info);
while ( $data = mysqli_fetch_assoc ($query) )
{ //query to fetch the data
$_SESSION['emp_name']=$data['name'];
?> <tr>
<td><?php echo $data['emp_id'];?></td>
<td>
<a href="project.php?id=<?php echo $data['emp_id'];?>&name=<?php echo $data['name']; ?>">
<input type="checkbox" name="check_list[]" value="<?php echo $data['name'];?>">
</a> <?php echo $data['name'];?>
</td>
<td><?php echo $data['email'];?></td>
</tr>
<?php }
?>
</tbody>
</table>
<ul class="pagination">
<?php
$sql = "SELECT COUNT(*) FROM emp_info";
$result = mysqli_query($conn , $sql);
$row = mysqli_fetch_row($result);
$total_records = $row[0];
// Number of pages required.
$total_pages = ceil($total_records /
$limit);
$pagLink = "";
for ( $i = 1; $i <= $total_pages; $i++) {
if ( $i == $page_no) {
$pagLink .= "<p>Pages:</p><li class='active'><a href='datatable.php?id=" . $data['emp_id'] .
"&page=" . $i ."'>". $i ."</a></li>";
} else {
$pagLink .= "<li><a href='datatable.php?page=". $i ."'>". $i ."</a></li>";
}
};
echo $pagLink;
?>
</ul>
</div>
<button type="submit" formaction="project.php"
name="select_proj">Select Project</button>
<button type="submit"
formaction="addnewproj.php" name="add_proj">Add New
Project</button>
</form>
</body>
</html>

I recommend reseaching abit of Javascript and more specificly aJax to solve this issue.
You need somewhere to store the information that has been selected in order to use it somewhere else.

Related

PHP SQLSRV Pagination function returns one row

My pagination isn't working correctly. I get the page numbers but each page only has one row when it should have 10 rows. I new to php and am adapting code that I found on this site. When I load the page I can tell by the id number that the result showing is what I would expect the 10th row in the query to be...rows 1-9 are missing.
<?php
//Function to return rows for each page
function getPage($stmt, $pageNum, $rowsPerPage)
{
$offset = ($pageNum - 1) * $rowsPerPage;
$rows = array();
$i = 0;
while(($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_NUMERIC,SQLSRV_SCROLL_ABSOLUTE,$offset + $i)) && $i < $rowsPerPage)
{
array_push($rows, $row);
$i++;
}
return $rows;
}
?>
<?php
// Set the number of rows to be returned on a page.
$rowsPerPage = 10;
$usr = $_SESSION['user'];
if ($_SESSION['admin']="YES") {
$query = "SELECT iID,FirstName,LastName, convert(varchar, SubmitDate, 101) as SubDate,LEFT(ImproveIdea,30) as MyIdea,Status FROM Idea where Status='Pending' ORDER BY iID desc";
}else {
$query = "SELECT iID,FirstName,LastName,convert(varchar, SubmitDate, 101) as SubDate,LEFT(ImproveIdea,30) as MyIdea,Status FROM Idea where SubmitBy='".$usr."' ORDER BY iID desc";
}
$stmt = sqlsrv_query($connect,$query, array(), array( "Scrollable" => 'static' ));
if ( !$stmt )
die( print_r( sqlsrv_errors(), true));
?>
<table class="table table-striped table-bordered">
<thead class="thead-light">
<tr>
<th width="54%" scope="col">Submitted By</th>
<th width="14%" scope="col">Date Submitted</th>
<th width="13%" scope="col">Status</th>
<th width="19%" scope="col">Actions</th>
</tr>
</thead>
<tbody>
<?php
$pageNum = isset($_GET['pageNum']) ? $_GET['pageNum'] : 1;
$page = getPage($stmt, $pageNum, $rowsPerPage);
foreach($page as $row)
$ideanum = $row[0];
$fname = $row[1];
$lname = $row[2];
$submitdate = $row[3];
$idea = $row[4];
$status = $row[5];
echo '<tr>';
echo '<td>'.$ideanum.' '.$fname.' '.$lname.' - '.$idea.'...</td>';
echo '<td>'.$submitdate.'</td>';
echo '<td>'.$status.'</td>';
echo '<td><button type="button" class="btn btn-success"><i class="la la-eye"></i></button><button type="button" class="btn btn-info"><i class="la la-edit"></i></button></td>';
echo "</tr>";
?>
<?php
// Get the total number of rows returned by the query.
// Display links to "pages" of rows.
$rowsReturned = sqlsrv_num_rows($stmt);
if($rowsReturned === false)
die( print_r( sqlsrv_errors(), true));
elseif($rowsReturned == 0)
{
echo "No rows returned.";
exit();
}
else
{
// Display page links.
$numOfPages = ceil($rowsReturned/$rowsPerPage);
for($i = 1; $i<=$numOfPages; $i++)
{
$pageLink = "?pageNum=$i";
print("<a href=$pageLink>$i</a> ");
}
echo "<br/><br/>";
}
?>
</tbody>
</table>
SOLVED: The issue was that I was missing the brackets in my foreach statement as a redditor pointed out to me.

Table pagination not going to a different page when selected

I added pagination to a table a I created a while back and I have not ever been able to get it to work correctly since.
The table limit works, but that's it. If I select "First, Last or the page number" it just reloads the page, but the new page does not display.
If I set the page limit to a low number like 5 and select 'Last Page', when the page loads it shows =1 like it doesn't know there are other pages.
<?php
$con = mysqli_connect("localhost","root","","bfb");
$per_page=20;
if(isset($_POST["page"])) {
$page = $_POST["page"];
}
else {
$page = 1;
}
//Page will start from 0 and multiply per page
$start_from = ($page-1)*$per_page;
//Selecting the data from the table but with limit
$query = "SELECT * FROM orders LIMIT $start_from, $per_page";
$result = mysqli_query($con, $query);
?>
<table class="tableproduct">
<tr>
<th class="thproduct">Order ID</th>
<th class="thproduct">Customer Name</th>
<th class="thproduct">Product</th>
<th class="thproduct">Total Price</th>
<th class="thproduct"></th>
<th class="thproduct"></th>
</tr>
<?php
if( $result ){
while($row = mysqli_fetch_assoc($result)) :
?>
<form method="POST" action="orderhistory.php">
<tr>
<td class="tdproduct"><?php echo $row['order_id']; ?> </td>
<td class="tdproduct"><?php echo $row['customer_name']; ?> </td>
<td class="tdproduct"><?php echo $row['product_name']; ?> </td>
<td class="tdproduct"><?php echo $row['total_price']; ?> </td>
<td class="tdproduct"><a href='editorderhistory.php?id=<?php echo $row['id']; ?>'>EDIT</a></td>
<input type="hidden" name="product_id" value="<? echo $row['id']; ?>"/>
<td class="tdproduct"><input name="delete" type="submit" value="DELETE "/></td>
</tr>
</form>
<?php
endwhile;
}
?>
</table>
<?php
//Count the total records
if ($result != false) {
$total_records = mysqli_num_rows($result);
if ($total_records == 0) {
echo 'No results founds.';
}
}
//Using ceil function to divide the total records on per page
$total_pages = ceil($total_records /$per_page);
?>
<span class="spandarkblue">
<?php
//Going to first page
echo "<center><a href='orderhistory.php?page=1'>".'First Page'."</a> ";
for ($i=1; $i<=$total_pages; $i++) {
echo "<a href='orderhistory.php?page=".$i."'>".$i."</a> ";
};
// Going to last page
echo "<a href='orderhistory.php?page=$total_pages'>".'Last Page'."</a></center>";
?>
Any ideas?
Found below issues in you code.
1) You are trying to get page value from URL using POST, where as you need to GET method to fetch values from URl. Using POST is returning null value, so $page value is always set to 1
So use $_GET["page"] instead of $_POST["page"]
2) You are preparing pagination by considering row count of the query which you are executing. But as you have added limits , your $total_records is always equals to $per_page value or less, resulting in only one page number.
Use the following code for generate pazination
$query1 = "SELECT count(*) as totalRecords FROM orders";
$result1 = mysqli_query($con, $query1);
if ($result1) {
$row1 = mysqli_fetch_assoc($result1);
$total_records = $row1['totalRecords'];
if ($total_records == 0) {
echo 'No results founds.';
}
}
Instead of below code
if ($result != false) {
$total_records = mysqli_num_rows($result);
if ($total_records == 0) {
echo 'No results founds.';
}
}
Hope it helps you.
You need to fetch all the data to know the total of your records, then you show only the desired data whithin a for() loop, so remove LIMIT $start, $per_page from your query, in this node you will always have 20 or less results
In your while() save the data in arrays like this:
$index = 0;
while($row = mysqli_fetch_assoc($result)) {
$ordID[$index] = $row["order_id"];
// The rest of your data...
$index++; // Increment the index
}
Then you chan show only the desired data:
for($i = (($page-1)*$perPage); $i < min(($page*$perPage), $total); $i++) {
echo $orderID[$i];
// And so on...
}
The min() function returns the lowest value between two vars, in this way in the last page, if you have 17 products instead of 20 you will not have errors.

Showing data from category with php mysql

My tables are:
barangtbl: id, judul_barang, judul_seo, keywords, deskripsi, id_kat, id_sub, id_supersub, kategori_seo, view, gambar
kategori: id_kat, nama_kat
subkategori: id_sub, id_kat, nama_sub
supersubkategori: id_supersub, id_sub, id_kat, nama_supersub
I have a problem with showing data in category from database with PHP, the problem is when i click link: localhost/test/category.php?name=HPI, it doesn't show any data, but if I change HPI with number: 15, it show all.
15 is id_supersub data on supersubkategori table where I join with barangtbl table. So, all i want is if someone click: localhost/test/category.php?name=HPI it will show data with HPI category inside. How solve this problem?
<?php
if (isset($_GET['name']))
{
$kategori = $_GET['name'];
}
include "config.php";
if ((isset($kategori)) =='')
{
$query = "SELECT * FROM barangtbl INNER JOIN supersubkategori on supersubkategori.id_supersub = barangtbl.id_supersub ORDER BY id DESC LIMIT 0,12";
$hasil = mysql_query($query);
$numrows = mysql_num_rows($hasil);
}
else
{
echo "
<table width=\"100%\">
<tr>
<td align=\"center\"><b><font color=\"red\" size=\"2.5\">[ ".$_GET['name']." ]</b></font></td>
</tr>
</table>";
$query = "SELECT * FROM barangtbl WHERE id_supersub = '$kategori' ORDER BY id";
$hasil = mysql_query($query);
$numrows = mysql_num_rows($hasil);
}
?>
<table cellpadding="10" cellspacing="2" align="center">
<tr>
<?php
$kolom=3;
$x = 0;
if($numrows > 0)
{
while($data=mysql_fetch_array($hasil))
{
if ($x >= $kolom)
{
echo "</tr><tr>";
$x = 0;
}
$x++;
?>
<th>
<div id="title">
<a href="product.php?id=<?php echo $data['id']; ?>">
<?php echo $data['judul_barang']; ?>
</a>
<br><br>
</div>
<div id="image">
<a href="product.php?id=<?php echo $data['id']; ?>">
<img width='150' height='150' valign='top' border='1,5' src="product/<?php echo $data['gambar']; ?>" />
</a>
<br><br>
</div>
<div id="action">
<?php
echo '
<a href="product.php?id='.$data['id'].'">
<img src="images/detail.jpg"\ title="Detail Barang" border="0" width=\"50\" height=\"30\">
</a>';
?>
</div>
<hr />
</th>
<?php
}
}
?>
</tr>
</table>
Try removing the quotes
$query = "SELECT * FROM barangtbl WHERE id_supersub = $kategori ORDER BY id";

Jquery Pagination doesn't work when the Next Button is pressed

I have made ​​a script for Pagination and it can be displayed.
I will be a problem when the Next Button is pressed, the Pagination can't continue to the next page.
when the url has changed but the page remains in the first place.
eg:
[1]. http:// localhost /pagination/
This works and can display data 1-10 on page 1.
[2] http:// localhost /pagination/?page=2
paging should've been on page 2, but the data shown is 1-10 (supposedly 11-20)
This is the code:
// data_students.php
<?php
require 'conn.php';
open_conn();
?>
<table class="table table-condensed table-bordered table-hover" cellpadding="0" cellspacing="0">
<thead>
<tr>
<th style="width:20px">No</th>
<th style="width:120px">NIM</th>
<th style="width:200px">Name</th>
<th>Address</th>
<th style="width:120px">Room</th>
<th style="width:120px">Status</th>
<th style="width:40px"></th>
</tr>
</thead>
<tbody>
<?php
$i = 1;
$num_pages = 10;
$num_data = mysql_num_rows(mysql_query("SELECT * FROM students"));
$total_page = ceil($num_data / $num_pages);
// query searching
if(isset($_POST['search'])) {
$key = $_POST['search'];
echo "<strong>Search results for keyword: $key</strong>";
$query = mysql_query("
SELECT * FROM students
WHERE nim LIKE '%$key%'
OR name LIKE '%$key%'
OR address LIKE '%$key%'
OR room LIKE '%$key%'
OR status LIKE '%$key%'
");
// query if the specified page number
} elseif(isset($_GET['page']) && $_GET['page']!="") {
$page = $_GET['page'];
$i = ($page - 1) * $num_pages + 1;
$query = mysql_query("SELECT * FROM students ORDER BY name ASC LIMIT ".(($page - 1) * $num_pages).", $num_pages");
// query when there is no parameter page and search
} else {
$query = mysql_query("SELECT * FROM students ORDER BY name ASC LIMIT 0, $num_pages");
}
// show database students
while($data = mysql_fetch_array($query)) {
if($data['status']==1) {
$status = "Active";
} else {
$status = "Off";
}
?>
<tr>
<td><?php echo $i ?></td>
<td><?php echo $data['nim'] ?></td>
<td><?php echo $data['name'] ?></td>
<td><?php echo $data['address'] ?></td>
<td><?php echo $data['room'] ?></td>
<td><?php echo $status ?></td>
<td>
<a href="#dialog-students" id="<?php echo $data['kd_mhs'] ?>" class="change" data-toggle="modal">
<i class="icon-pencil"></i>
</a>
<a href="#" id="<?php echo $data['kd_mhs'] ?>" class="delete">
<i class="icon-trash"></i>
</a>
</td>
</tr>
<?php
$i++;
}
?>
</tbody>
</table>
<?php if(!isset($_POST['search'])) { ?>
<!-- to display the menu page -->
<div class="pagination pagination-right">
<ul>
<?php for($i = 1; $i <= $total_page; $i++) { ?>
<li class="page" id="<?php echo $i ?>"><?php echo ''.$i.'' ;?></li>
<?php } ?>
</ul>
</div>
<?php } ?>
<?php
close_conn();
?>
this is jquery:
// students-app.js
(function($) {
// function is executed after the entire document is displayed
$(document).ready(function(e) {
var kd_mhs = 0;
var main = "data_students.php";
// show data students from data_students.php in index.php <div id="data-students"></div>
$("#data-students").load(main);
// when the search inputbox filled
$('input:text[name=searching]').on('input',function(e){
var v_search = $('input:text[name=searching]').val();
if(v_search!="") {
$.post(main, {search: v_search} ,function(data) {
$("#data-students").html(data).show();
});
} else {
$("#data-students").load(main);
}
});
// when the button page is pressed
$('.page').live("click", function(event){
// take the value of the inputbox
kd_page = this.id;
$.post(main, {page: kd_page} ,function(data) {
$("#data-students").html(data).show();
});
});
});
}) (jQuery);

PHP - MySQL query with Pagination

How would I go about making a pagination script for this MySQL & PHP query.
if (isset($_GET['c'])) {
$c = $_GET['c'];
}
$query = mysql_query("SELECT * FROM Categories WHERE category = '$c' ");
WHILE($datarows = mysql_fetch_array($query)):
$id = $datarows['id'];
$category = $datarows['category'];
$code = $datarows['code'];
endwhile;
$query2 = mysql_query("SELECT * FROM Games WHERE category = '$code' ");
WHILE($datarows_cat = mysql_fetch_array($query2)):
$title = $datarows_cat['title'];
$description = $datarows_cat['description'];
$imgurl = $datarows_cat['image_name'];
$category = $datarows_cat['category'];
$views = $datarows_cat['view_count'];
$pagename = $datarows_cat['pagename'];
$featured = $datarows_cat['featured'];
if ($featured =="1") {$f = "<img src='http://my-site.com/images/star.png' width='13px' title='Featured Game' /> Featured"; } else {$f = "";}
if(is_int($views/2)) {
$views = $views / 2;
} else { $views = $views / 2 + .5; }
if (strlen($description) > 95) {
$desc= substr($description,0,95);
$desmod = "$desc...<br/>- Read More";
}
else {$desmod = "$description";}
echo "$f - $title - $desmod<br/>";
endwhile;
And when I visit http://my-site.com/categories/Action for instance, The code looks up that category in my category table, then once it gets the unique code for that category, It runs another query to find all games in another table with that category code. Currently, however, I have 200+ games loading for a single category which causes a great amount of loading time.
Thanks for your help!
First of all find out how many games are there for a specific category
change the line
$query2 = mysql_query("SELECT * FROM Games WHERE category = '$code' ");
to
$sql="SELECT * FROM Games WHERE category = '$code' ";
$query_count=mysql_query($sql);
Add following after it
$per_page =30;//define how many games for a page
$count = mysql_num_rows($query_count);
$pages = ceil($count/$per_page);
if($_GET['page']==""){
$page="1";
}else{
$page=$_GET['page'];
}
$start = ($page - 1) * $per_page;
$sql = $sql." LIMIT $start,$per_page";
$query2=mysql_query($sql);
Then display the numbers of pages where you want
<ul id="pagination">
<?php
//Show page links
for ($i = 1; $i <= $pages; $i++)
{?>
<li id="<?php echo $i;?>"><?php echo $i;?></li>
<?php
}
?>
</ul>
Use CSS for pagination this will do the trick
//database connation
<?php
$conn = new mysqli("localhost", "root", "","database_name");
?>
<!DOCTYPE html>
<html>enter code here
<head>
<title>View Student Details</title>
<h1 align="center"> Student Details </h1>
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="bootstrap/bootstrap.min.js"></script>
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
</head>
<body>
<div class="row">
<div class="col-sm-2"></div>
<div class="col-sm-8">
<form>
<table class="table table-striped">
<tr>
<th>Sr.No.</th>
<th>Student ID</th>
<th>Student Name</th>
<th>Class</th>
<th>Gender</th>
<th>Birth of Date</th>
<th>Contact No.</th>
<th>Action</th>
</tr>
<?php
$count=0;
if(isset($_GET['page_count']))
{
$count=1;
$page_count=$_GET['page_count']-1;
$count=$page_count*10;
}
$q="SELECT * from student_detail LIMIT $count,10";
$result=$conn->query($q);
$j=0;
while($data=$result->fetch_array())
{ $j=$j+1;
?>
<tr>
<td><?php echo $j ?></td>
<td><?php echo $data['std_id'] ?></td>
<td><?php echo $data['std_name'] ?></td>
<td><?php echo $data['std_class'] ?></td>
<td><?php echo $data['gender'] ?></td>
<td><?php echo $data['bod'] ?></td>
<td><?php echo $data['contact'] ?></td>
<td>
<div class="row">
<div class="col-sm-12">
Delete
Update
</div>
</div>
</td>
</tr>
<?php } ?>
</table>
<ul class="pagination">
<?php
$q="SELECT count(std_id) from student_detail";
$result=$conn->query($q);
$data=$result->fetch_array();
$total=$data[0];
$total_page=ceil($total/10);
if($total_page>1)
{
for($i=1;$i<=$total_page;$i++)
{
?>
<li class="active"><?php echo $i; ?></li>
<?php
}
}
?>
</ul>
</form>
<div class="col-sm-2"></div>
</div>
</div>
</body>
</html>
Pagiantion, it is working simple and easy
<?php
$sql = "SELECT COUNT(id) FROM contact_info";
$rs_result = $conn->query($sql);
$row = mysqli_fetch_row($rs_result);
$total_records = $row[0];
echo $total_records;
$previous = 1;
$total_pages = ceil($total_records / $limit);
$next = $_GET["page"] + 1;
$previous = $_GET["page"] - 1;
$pagLink = "<span>";
if($previous ==0)
{
$prev = "<a href='javascript:void(0);' >Previous</a>";
}
else
{
$prev = "<a href='http://homeacresfinefurniture.com/all-queries.php?page=".$previous."' style='color:black;'>Previous</a>";
};
echo $prev;
"</span><div class='pagination'>";
for ($i=1; $i<=$total_pages; $i++)
{
$pagLink .= "<a href='http://homeacresfinefurniture.com/all-queries.php?page=".$i."'>".$i."</a>";
};
echo $pagLink;
$nex = "<span><a href='http://homeacresfinefurniture.com/all-queries.php?page=".$next."' style='color:black;'>Next</a></span>";
echo $nex;
";
</div>";
?>
Get all data from database.
$limit = 1;
if (isset($_GET["page"]))
{
$page = $_GET["page"];
}
else
{
$page=1;
}
$start_from = ($page-1) * $limit;
$sql = "SELECT * FROM contact_info ORDER BY id desc LIMIT $start_from , $limit";
$page = 1;
$limit = 10;
$offset = ($limit * $page) - $limit;
$query = mysqli_query(
$connect,
"SELECT * FROM Games WHERE category = '$code' limit $limit offset $offset"
);

Categories