<html>
<body>
<center>
<div>
<form method="post" action="admission_list_fetch5.php">
Enter Cousre Code: <input name="course_code" placeholder="course code">
<input type="submit" name="submit" value="Submit">
</form>
<div>
<table border="2" id="enquirytable" style= "border-collapse:collapse;"">
<thead>
<tr>
<th style="width:50;">SL No</th>
<th>Student ID</th>
<th>Enrol No</th>
<th>Name</th>
<th>Course</th>
<th>Semester</th>
</tr>
</thead>
<tbody>
<?php
//error_reporting(0);
$num_rec_per_page=10;
mysql_connect('localhost','root','');
mysql_select_db('nobledatabase');
if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; };
$start_from = ($page-1) * $num_rec_per_page;
$course_code=$_POST['course_code'];
$sql = "SELECT * FROM admission_list where course_code='$course_code' LIMIT $start_from, $num_rec_per_page";
$rs_result = mysql_query ($sql); //run the query
$i=1;
while( $row = mysql_fetch_assoc( $rs_result ) )
{
echo ("<tr><td style='text-align:center'>$i</td>".
"<td>{$row['student_id']}</td>".
"<td>{$row['enrol']}</td>".
"<td>{$row['name']}</td>".
"<td>{$row['course']}</td>".
"<td>{$row['sem_year']}</td></tr>\n");
$i++;
}
?>
</tbody>
</table>
<?php
$sql="SELECT * FROM admission_list where course_code='$course_code'";
$rs_result = mysql_query($sql); //run the query
$total_records = mysql_num_rows($rs_result); //count number of records
$total_pages = ceil($total_records / $num_rec_per_page);
?>
</div><!--enquirytable-->
<div class="enquirypages">
<br>
<?php
echo "<a href='admission_list_fetch5.php?page=1&course_code=$course_code'>".'<<'."</a> "; // Goto 1st page
for ($i=1; $i<=$total_pages; $i++) {
echo "<a href='admission_list_fetch5.php?page=".$i."&course_code=$course_code'> ".$i." ";
};
echo "<a href='admission_list_fetch5.php?page=$total_pages&course_code=$course_code'>".'>>'."</a> "; // Goto last page
?>
</div>
</div>
</center>
</body>
</html>
Even if I pass the parameters to all the pages, I am getting the same error message:
Notice: Undefined index: course_code in C:\wamp\www\admission_list_fetch5.php on line 33
When I look at the logic it seems that the only value that supplies the course_code variable comes from the $_POST['course_code']. This means that you have to submit from a form post every time to populate this variable.
The assumption is that the 2nd code peace is the admission_list_fetch5.php.
Whenever paging occurs then clicking on the <a href link passing query strings at no point does course_code get assigned using a $_GET['course_code']. Therefore course_code query string will always be empty.
I would suggest the following on the assumption admission_list_fetch5.php
if(isset($_POST['course_code']))
{
$course_code = $_POST['course_code'];
}
if(isset($_GET['course_code']))
{
$course_code = $_GET['course_code'];
}
Not very elegant but serves the purpose. When you post to this page there should be not query string therefore the code will default to the $_POST['course_code']. If a query string was set for then it will override using $_GET['course_code'].
Related
I'm working on a PHP script that uses delete and I only want to delete 1 row. But everytime I tried to delete 1 row, the SQL executes but deletes the entire rows in the table.
The following is my PHP script
<table>
<tr>
<th>No.</th>
<th>Publisher Code</th>
<th>Publisher Name</th>
<th>City</th>
<th> </th>
</tr>
<!-- PHP FETCH/RETRIEVE FROM DB -->
<?php
include_once 'dbconnect.php';
$sql = mysqli_query($conn, "SELECT * FROM tbl_publisher");
$ctr = 1;
$record = mysqli_num_rows($sql);
if ($record > 0) {
while ($record = mysqli_fetch_array($sql)) {
?>
<tr>
<td> <?php echo $ctr++ ?> </td>
<td> <?php echo $record['TBL_PUBLISHER_CODE']; ?> </td>
<td> <?php echo $record['TBL_PUBLISHER_NAME']; ?> </td>
<td> <?php echo $record['CITY']; ?> </td>
<td id="actions">
<center>
Delete
</center>
</td>
</tr>
<!-- closing tag for php script -->
<?php
}
}
?>
<!-- -->
</table>
and below is my delete.php
if(isset($_GET['del-pub']))
{
$row = intval($_GET['del-pub']);
$sql = mysqli_query($conn,"DELETE FROM tbl_publisher WHERE TBL_PUBLISHER_CODE = $row;");
if ($sql) {
header("Location: publisher.php");
}
else {
echo "<script>alert('Publisher was not deleted.');</script>";
header("Location: publisher.php");
}
}
I want to know how to delete only the 1 row. But it keeps deleting the entire rows in the table.
After dumping the contents of $sql, I found out that my sql syntax is the culprit.
Since TBL_PUBLISHER_CODE uses char as ID. Instead of doing "="
DELETE FROM tbl_publisher WHERE TBL_PUBLISHER_CODE = $row;
I used
DELETE FROM tbl_publisher WHERE TBL_PUBLISHER_CODE LIKE '".$row."'
To do list:
I have created the HTML and php code to insert data into the database, and read from it.
Database is looking like this:
task_id |task_desc |task_target |task_finished_date |task_status
40 |test p |2020-07-25 |2020-07-23 |completed
Task_target and task_finished_date are using type date in the database.
task_target is getting value on creation of the task, and task_finished_date is getting automatically value when we mark the job completed using now():
$task_id = $_GET['completed'];
mysqli_query($db, "UPDATE tasks SET task_status = 'completed' WHERE task_id=". $task_id);
mysqli_query($db, "UPDATE tasks SET task_finished_date = now() WHERE task_id=". $task_id);
mysqli_query($db, "ORDER BY task_finished_date DESC ". $task_id);
header("Location: index.php" );
}
I am currently trying to program it when some task is completed AFTER the deadline to be displayed in "Failed" table, and if is closed before the deadline to be displayed in completed table.
<tbody>
<?php $i = 1; while ($row = mysqli_fetch_array($finished_tasks)) { ?>
<tr>
<?php
if($targetd > $finished_d){ ?>
<td class="successful"> <?php echo $i; ?> </td>
<td class="successful"> <?php echo $row['task_desc']; ?> </td>
<td class="successful"> <?php echo $row['task_finished_date']; ?> </td>
<?php }?>
</tr>
<?php $i++; } ?>
</tbody>
<thead>
<tr>
<th>N</th>
<th>Failed Tasks</th>
<th style="width: 197px;">Task finished time</th>
</tr>
</thead>
<tbody>
<?php $i = 1; while ($row = mysqli_fetch_array($finished_tasks)) { ?>
<tr>
<?php
if($targetd < $finished_d){ ?>
<td class="failed"> <?php echo $i; ?> </td>
<td class="failed"> <?php echo $row['task_desc']; ?> </td>
<td class="failed"> <?php echo $row['task_finished_date']; ?> </td>
<?php }?>
</tr>
<?php $i++; } ?>
</tbody>
As in the $finished_tasks variable i have storing the values for the completed tasks:
$finished_tasks = mysqli_query($db, "SELECT * FROM tasks WHERE task_status = 'completed' ");
$finished_task_time = mysqli_query($db, "SELECT task_finished_date FROM tasks WHERE task_status = 'completed' ");
$planned_target_time = mysqli_query($db, "SELECT task_target FROM tasks WHERE task_status = 'completed' ");
while ($row = mysqli_fetch_array($finished_tasks)) {
$targetd = $row['task_target'];
$finished_d = $row['task_finished_date'];
Have tried to get the deadline(task_target)and target(task_finished_date) and insert them in both variables and then compare them, and based on this comparison to display them where I want, but the logic here is not right I think.
How I can separate the tasks based on the desired condition?
Entire Code Below:
<?php
// initialize errors variable
$errors = "";
// connect to database
$db = mysqli_connect("localhost", "root", "", "todo");
// insert a quote if submit button is clicked
if (isset($_POST['submit'])) {
if (empty($_POST['task_desc'])) {
$errors = "You must fill in the task";
}else{
$task_desc = $_POST['task_desc'];
$task_target = $_POST['task_target'];
$sql = "INSERT INTO tasks (task_desc, task_target) VALUES ('$task_desc', '$task_target')";
mysqli_query($db, $sql);
header('location: index.php');
}
}
// delete task
if (isset($_GET['del_task'])) {
$task_id = $_GET['del_task'];
mysqli_query($db, "DELETE FROM tasks WHERE task_id=".$task_id);
header('location: index.php');
}
if(isset($_GET['completed'])){
$task_id = $_GET['completed'];
mysqli_query($db, "UPDATE tasks SET task_status = 'completed' WHERE task_id=". $task_id);
mysqli_query($db, "UPDATE tasks SET task_finished_date = now() WHERE task_id=". $task_id);
mysqli_query($db, "ORDER BY task_finished_date DESC ". $task_id);
header("Location: index.php" );
}
// select all tasks if page is visited or refreshed
$tasks = mysqli_query($db, "SELECT * FROM tasks WHERE task_status = 'Active' ");
?>
<!DOCTYPE html>
<html>
<head>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script>
<link rel="stylesheet" type="text/css" href="style.css">
<title>ToDo List Application PHP and MySQL</title>
</head>
<body>
<!-- Page Content -->
<div class="container">
<div class="row">
<!-- Blog Entries Column -->
<div class="col-md-12">
<div class="heading">
<h2 style="font-style: 'Hervetica';">ToDo List Application PHP and MySQL database</h2>
</div>
<form method="post" action="index.php" class="input_form">
<?php if (isset($errors)) { ?>
<p><?php echo $errors; ?></p>
<?php } ?>
<label for="task_target">Select Date:</label>
<input type="date" class="form-control" name="task_target">
<label for="task_desc">Describe your task:</label>
<textarea class="form-control" name="task_desc" id="body" cols="30" rows="5"></textarea>
<button type="submit" name="submit" id="add_btn" class="form-control">Add Task</button>
</form>
<table class="table table-bordered">
<thead>
<tr>
<th>N</th>
<th>Ongoing Tasks</th>
<th>Target Date</th>
<th>Action</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php $i = 1; while ($row = mysqli_fetch_array($tasks)) { ?>
<tr>
<td> <?php echo $i; ?> </td>
<td> <?php echo $row['task_desc']; ?> </td>
<td> <?php echo $row['task_target']; ?> </td>
<td>
Delete
</td>
<td>
Complete
</td>
</tr>
<?php $i++; } ?>
</tbody>
</table>
<?php
$finished_tasks = mysqli_query($db, "SELECT * FROM tasks WHERE task_status = 'completed' ");
$finished_task_time = mysqli_query($db, "SELECT task_finished_date FROM tasks WHERE task_status = 'completed' ");
$planned_target_time = mysqli_query($db, "SELECT task_target FROM tasks WHERE task_status = 'completed' ");
while ($row = mysqli_fetch_array($finished_tasks)) {
$targetd = $row['task_target'];
$finished_d = $row['task_finished_date'];
?>
<table class="table table-bordered">
<thead>
<tr>
<th>N</th>
<th>Successful Tasks</th>
<th style="width: 197px;">Task finished time</th>
</tr>
</thead>
<tbody>
<?php $i = 1; while ($row = mysqli_fetch_array($finished_tasks)) { ?>
<tr>
<?php
if($targetd > $finished_d){ ?>
<td class="successful"> <?php echo $i; ?> </td>
<td class="successful"> <?php echo $row['task_desc']; ?> </td>
<td class="successful"> <?php echo $row['task_finished_date']; ?> </td>
<?php }?>
</tr>
<?php $i++; } ?>
</tbody>
</table>
<table class="table table-bordered">
<thead>
<tr>
<th>N</th>
<th>Failed Tasks</th>
<th style="width: 197px;">Task finished time</th>
</tr>
</thead>
<tbody>
<?php $i = 1; while ($row = mysqli_fetch_array($finished_tasks)) { ?>
<tr>
<?php
if($targetd > $finished_d){ ?>
<td class="failed"> <?php echo $i; ?> </td>
<td class="failed"> <?php echo $row['task_desc']; ?> </td>
<td class="failed"> <?php echo $row['task_finished_date']; ?> </td>
<?php }?>
</tr>
<?php $i++; } ?>
</tbody>
</table>
<?php } ?>
</div>
</div>
</div>
</body>
</html>
Your question is quite ambiguous, what kind of error or unexpected output are you getting?
Had to strip your code of the HTML part and the PHP part not affecting the logic, here's what I have.
$finished_tasks = mysqli_query($db, "SELECT * FROM tasks WHERE task_status = 'completed' ");
$finished_task_time = mysqli_query($db, "SELECT task_finished_date FROM tasks WHERE task_status = 'completed' ");
$planned_target_time = mysqli_query($db, "SELECT task_target FROM tasks WHERE task_status = 'completed' ");
while ($row = mysqli_fetch_array($finished_tasks)) {
$targetd = $row['task_target'];
$finished_d = $row['task_finished_date'];
$i = 1;
while ($row = mysqli_fetch_array($finished_tasks))
{
if($targetd > $finished_d)
{
echo $i;
echo $row['task_desc'];
echo $row['task_finished_date'];
$i++;
}
$i = 1;
while ($row = mysqli_fetch_array($finished_tasks))
{
if($targetd > $finished_d)
{
echo $i;
echo $row['task_desc'];
echo $row['task_finished_date'];
}
$i++;
}
}
All three while loops assign a value to the $row variable. This would cause some unexpected results as some values may be skipped.
You're referencing the SQL result indexes inappropriately.
I have managed to find a easy fix of my question.
First of all thanks to all who tried to help me and advised way of effective learning.
Really like this place.
So, on the answer:
As in the begging i have described that the Deadline of the task is defining during the creation of the task, and the closing time is getting to the database when the user click Completed button.
So i have decided to have the sort directly into the database modifying the queries like this:
$finished_tasks = mysqli_query($db, "SELECT * FROM tasks WHERE task_status = 'completed' AND task_finished_date < task_target ");
$finished_failed_tasks = mysqli_query($db, "SELECT * FROM tasks WHERE task_status = 'completed' AND task_finished_date > task_target ");
In this way i have all of the completed successfully tasks in $finished_tasks variable and all of the failed in $finished_failed_tasks, after that the displaying in the page is easy.
I am not quite sure if this is practical solution, but in this way working as i desired.
Next step is to strip and refactor the code as you suggested.
Once again, thanks really much and have a nice day.
Regards, Kristiyan
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.
I'm a PHP student and I'm developing my first app. I need to add pagination on the search results with this code below. I can't use datatables or another plug-ins because it's hard for me to put action buttons and my data on table.
If you know some simple method that can be not so hard to implement will help a lot.
I'm using the example from this dev: how to search and filter with php
if(isset($_POST['search']))
{
$valueToSearch = $_POST['valueToSearch'];
// search in all table columns
// using concat mysql function
$query = "SELECT * FROM `users` WHERE CONCAT(`id`, `fname`, `lname`, `age`) LIKE '%".$valueToSearch."%'";
$search_result = filterTable($query);
}
else {
$query = "SELECT * FROM `users`";
$search_result = filterTable($query);
}
// function to connect and execute the query
function filterTable($query)
{
$connect = mysqli_connect("localhost", "root", "", "test_db");
$filter_Result = mysqli_query($connect, $query);
return $filter_Result;
}
?>
<!DOCTYPE html>
<html>
<head>
<title>PHP HTML TABLE DATA SEARCH</title>
<style>
table,tr,th,td
{
border: 1px solid black;
}
</style>
</head>
<body>
<form action="php_html_table_data_filter.php" method="post">
<input type="text" name="valueToSearch" placeholder="Value To Search"><br><br>
<input type="submit" name="search" value="Filter"><br><br>
<table>
<tr>
<th>Id</th>
<th>First Name</th>
<th>Last Name</th>
<th>Age</th>
</tr>
<!-- populate table from mysql database -->
<?php while($row = mysqli_fetch_array($search_result)):?>
<tr>
<td><?php echo $row['id'];?></td>
<td><?php echo $row['fname'];?></td>
<td><?php echo $row['lname'];?></td>
<td><?php echo $row['age'];?></td>
</tr>
<?php endwhile;?>
</table>
</form>
</body>
</html>
Below example will work wells for requirements :
mysql_connect("localhost","wellho","wawawawa");
mysql_select_db("wellho");
$perpage = 10;
$html = "";
$startat = $_REQUEST[page] * $perpage;
$limlim = "%".$_REQUEST[look4]."%";
$q = mysql_query("select count(entry_id) from mt_entry where entry_title like '$limlim'");
$row = mysql_fetch_array($q);
$havesome = $row[0];
$pages = floor(($row[0]-1) / $perpage) +1 ;
$q = mysql_query("select * from mt_entry where entry_title like '$limlim' order by entry_id desc limit $startat,$perpage");
while ($row = mysql_fetch_assoc($q)) {
$text = strip_tags($row[entry_text]);
$text = substr($text,0,300);
$html .= "<dt>$row[entry_id] - <a href=/mouth/$row[entry_id]_.html target=pix>$row[entry_title]</a></dt>";
$html .= "<dd>$text ....<br><br></dd>";
};
$lynx = "Please choose the next page you want to view:";
for ($k=0; $k<$pages; $k++) {
if ($k != $_REQUEST[page]) {
$lynx .= " ".($k+1)."";
} else {
$lynx .= " <b>--".($k+1)."--</b>";
}
}
if ($pages < 2) {
$lynx = "All results shown on this page";
}
if ($havesome == 0) {
$lynx = "Sorry - no titles matched. Please change your search string";
}
?>
<html><head>
<title>Showing blog entries</title>
<body>
<h2>Search titles on "The Horse's Mouth"</h2>
<form>Search only for titles including ... <input name=look4
value="<?= htmlspecialchars(stripslashes($_REQUEST[look4])) ?>">
(Please leave box empty to select all titles)<br>
<input type=submit></form><br>
<h2>Here are the entries you selected - page <?= $_REQUEST[page]+1 ?>:</h2><br>
<?= $html ?>
<?= $lynx ?>
</body>
You should use limit and offset to paginate the results.
<?php
...
// If no 'page' parameter is found, default to 1
$currentPage = isset($_GET['page']) ? $_GET['page'] : 1;
// Results per page
$limit = 10;
// Offset = (page - 1) * limit. (page 1 = 0, page 2 = 10, etc...)
$offset = ($currentPage - 1) * $limit;
if(isset($_POST['search']))
{
$valueToSearch = $_POST['valueToSearch'];
// search in all table columns
// using concat mysql function
$query = "SELECT * FROM `users` WHERE CONCAT(`id`, `fname`, `lname`, `age`) LIKE '%".$valueToSearch."%' LIMIT $limit OFFSET $offset";
$search_result = filterTable($query);
}
else {
$query = "SELECT * FROM `users` LIMIT $limit OFFSET $offset";
$search_result = filterTable($query);
}
// function to connect and execute the query
function filterTable($query)
{
$connect = mysqli_connect("localhost", "root", "", "test_db");
$filter_Result = mysqli_query($connect, $query);
return $filter_Result;
}
?>
<!DOCTYPE html>
<html>
<head>
<title>PHP HTML TABLE DATA SEARCH</title>
<style>
table,tr,th,td
{
border: 1px solid black;
}
</style>
</head>
<body>
<form action="php_html_table_data_filter.php" method="post">
<input type="text" name="valueToSearch" placeholder="Value To Search"><br><br>
<input type="submit" name="search" value="Filter"><br><br>
<table>
<tr>
<th>Id</th>
<th>First Name</th>
<th>Last Name</th>
<th>Age</th>
</tr>
<!-- populate table from mysql database -->
<?php while($row = mysqli_fetch_array($search_result)):?>
<tr>
<td><?php echo $row['id'];?></td>
<td><?php echo $row['fname'];?></td>
<td><?php echo $row['lname'];?></td>
<td><?php echo $row['age'];?></td>
</tr>
<?php endwhile;?>
</table>
</form>
Previous
Next
</body>
</html>
PS: In the previous example i didn't checked if the page is the first or last, you should do that.
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.