Pagination does not display the last two data from the database - php

This is where I set the number of pages and starting point:
<?php
$connection = mysqli_connect("localhost", "root");
$db = mysqli_select_db($connection, 'db_hotelreservation');
if(isset($_GET['page'])){
$page = $_GET['page'];
}
else {
$page = 1;
}
$number_per_page = 02;
$start_from = ($page-1)*02;
echo "$start_from";
$query = "SELECT * FROM tbl_reservation WHERE room_status = 'Pending' OR room_status = 'Booked' LIMIT $start_from,$number_per_page";
$query_run = mysqli_query($connection, $query);
$resultCheck = mysqli_num_rows($query_run);
?>
This is where the looping and the buttons:
<?php
$query_result = "SELECT * FROM tbl_reservation WHERE room_status = 'Pending' OR room_status = 'Booked'";
$result_query = mysqli_query($connection,$query_result);
$total_records = mysqli_num_rows($result_query);
echo "Total Records: $total_records <br>";
$total_page = ceil($total_records/$number_per_page);
echo "Total Pages: $total_page <br>";
for($i=1;$i<$total_page;$i++){
echo "<a href='SuperAdminReserveManage.php?page=".$i."' class='btn btn-primary'>$i</a>";
}
?>
Total Records: 8
Total Pages: 4
But then it only prints 3 buttons, kinda confused where did I go wrong.

my guess is you're starting from 1 instead of 0
or use
for($i=1;$i<=$total_page;$i++){
....
instead

Related

PHP if else statement not executing code when value is true

This is for a quiz application.
This is my comparison where $number is the question number and will increase every time the user submits an answer and we will assume $totalquestions = 3 for now as the category has only 3 questions.
I've echoed both values to make sure they are what I expect them to be and done var_dump($number == $totalQuestions); which returns true when $number = 3 but does not execute my code to redirect to the finish page once all questions are done and I have no clue why.
Any help would be great as I've been staring at this problem for hours!
<?php include('server.php'); ?>
<?php
if(!isset($_SESSION['geoScore'])){
$_SESSION['geoScore'] = 0;
}
//Get quiz category
$activeCategoryID = (int) $_GET['c'];
$query = "SELECT * FROM `category` WHERE CategoryID = $activeCategoryID";
$result = mysqli_query($db, $query);
$categoryName = mysqli_fetch_assoc($result);
echo $categoryName['CategoryName'];
//Get question number
$number = (int) $_GET['n'];
$query = "SELECT * FROM `questions` WHERE QuestionID = $number AND CategoryID = $activeCategoryID";
$result = mysqli_query($db, $query);
$question = mysqli_fetch_assoc($result);
//Get choices for question
$query = "SELECT * FROM `answers` WHERE QuestionID = $number";
$choices = mysqli_query($db, $query);
//Get total questions for category
$query = "SELECT * FROM `questions` WHERE CategoryID = $activeCategoryID";
$result = mysqli_query($db, $query);
$totalQuestions = mysqli_num_rows($result);
echo $totalQuestions;
echo $number;
var_dump($number == $totalQuestions); // returns true because values are equal
$result = mysqli_query($db, $query);
$row = mysqli_fetch_assoc($result);
$correctChoice = $row['AnswerID'];
echo $correctChoice;
if($_SERVER["REQUEST_METHOD"] == "POST"){
$number = $_POST['number'];
$selectedChoice = $_POST['choice'];
$next = $number+1;
$activeCategory = $_POST['activeCategoryID'];
$query = "SELECT * FROM `questions` WHERE CategoryID = activeCategoryID";
$result = mysqli_query($db, $query);
$totalQuestions = mysqli_num_rows($result);
//Get correct choice
$query = "SELECT * FROM `answers` WHERE QuestionID = $number AND Correct = 1";
$result = mysqli_query($db, $query);
$row = mysqli_fetch_assoc($result);
$correctChoice = $row['AnswerID'];
if($correctChoice == $selectedChoice){
$_SESSION['geoScore']++;
}
//Check to see if questions have finished
if($number == $totalQuestions){
header("Location: finish.php");
}else{
header("Location: geoQuiz.php?n=".$next."&c=".$activeCategory);
}
}
?>
EDIT: Previous working version that did not dynamically get the quiz category
<?php
if(!isset($_SESSION['geoScore'])){
$_SESSION['geoScore'] = 0;
}
//Get question number
$number = (int) $_GET['n'];
$query = "SELECT * FROM `questions` WHERE QuestionID = $number AND CategoryID = 1";
$result = mysqli_query($db, $query);
$question = mysqli_fetch_assoc($result);
//Get choices for question
$query = "SELECT * FROM `answers` WHERE QuestionID = $number";
$choices = mysqli_query($db, $query);
//Get total questions for category
$query = "SELECT * FROM `questions` WHERE CategoryID = 1";
$result = mysqli_query($db, $query);
$totalQuestions = mysqli_num_rows($result);
if($_SERVER["REQUEST_METHOD"] == "POST"){
$number = $_POST['number'];
$selectedChoice = $_POST['choice'];
$next = $number+1;
$_SESSION['activeCategory'] = "Geography";
$query = "SELECT * FROM `questions` WHERE CategoryID = 1";
$result = mysqli_query($db, $query);
$totalQuestions = mysqli_num_rows($result);
//Get correct choice
$query = "SELECT * FROM `answers` WHERE QuestionID = $number AND Correct = 1";
$result = mysqli_query($db, $query);
$row = mysqli_fetch_assoc($result);
$correctChoice = $row['AnswerID'];
echo $correctChoice;
echo $selectedChoice;
if($correctChoice == $selectedChoice){
$_SESSION['geoScore']++;
}
echo $geoScore;
//Check to see if questions have finished
if($number == $totalQuestions){
header("Location: finish.php");
}else{
header("Location: geoQuiz.php?n=".$next);
}
}
?>
The above code was my previous version which worked but I would needed to have had multiple php pages for each quiz category so I tried to change this to take the category using $_GET[] in the link from index page and it seems to all work apart from when the questions are finished and to head to finish.php

How would i display a message if mysql doesn't return a result or empty

how would i get this to display a message in place of the Query if no result is found i updated the code but its just showing "N"
<?php
$hostname = "...";
$username = "";
$password = "";
$db = "";
$dbconnect=mysqli_connect($hostname,$username,$password,$db);
if ($dbconnect->connect_error) {
die("Database connection failed: " . $dbconnect->connect_error);
}
$query=mysqli_query($dbconnect,"SELECT DISTINCT companyname,client_id,feedback,status from review WHERE status=1 ORDER BY RAND() LIMIT 4");
$rows_get = mysqli_num_rows($query);
if ($rows_get >0)
{
$query2=mysqli_query($dbconnect,"SELECT DISTINCT companyname,client_id,feedback,status from review WHERE status=1 ORDER BY RAND() LIMIT 4");
$row1 = mysqli_fetch_assoc($query2);
$row2 = mysqli_fetch_assoc($query2);
$row3 = mysqli_fetch_assoc($query2);
$row4 = mysqli_fetch_assoc($query2);
$row5 = mysqli_fetch_assoc($query2);
}else {
$row1 = "N0 Data";
$row2 = "N0 Data";
$row3 = "N0 Data";
$row4 = "N0 Data";
$row5 = "N0 Data";
}
?>
Do as follows:
After $query insert this:
$rows_get = mysqli_num_rows($query);
if ($rows_get >0)
{
//do all database operation
}else {
echo " No data found";
}
Hope this helps.
Amend your code for example..
if ($row_get>0){
//i assume you are getting multiple rows
while ($data =mysqli_fetch_assoc ($query))
{
//run this loop and you will get all you rows.
}
}

prevent insert same id if the user/student not put timeout

i have two button on my homepage one is time-in and the other is time-out,
i want to prevent the user/student to time-in using same id if he did not put time-out on his last time-in to create valid entry. Hope you can help me.
here is my php code:
<?php
include_once('connection.php');
if(isset($_POST['submit0'])){
$rfid = $_POST['rfid'];
$time=date("H:i:s");
$sql = mysqli_query($conn, "SELECT * FROM stud WHERE rfid_num = '$rfid'");
$count = mysqli_num_rows($sql);
if ($count == 0 ) {
header("location:notexist.php");
} elseif (empty($row['timeout'])) {
header("location:page say the user/student need to put timeout first before time-in again");
} else {
while( $row = mysqli_fetch_array($sql)) {
$rfid=$row['rfid_num'];
$id=$row['id'];
$name0 = $row['name'];
$course0 = $row['course'];
$image = $row['image'];
$InsertSql = "INSERT INTO student_att(rfid_num,id,name,course,image,timein) VALUES ('$rfid','$id','$name0','$course0','$image','$time')";
$res = mysqli_query($conn, $InsertSql);
}
}
}
?>
this is my answer just wanna share it, i just add select student_att table
to fetch the data and check if timeout column is empty.
<?php
include_once('connection.php');
if(isset($_POST['submit0'])){
$rfid = $_POST['rfid'];
$time=date("H:i:s");
$sql = mysqli_query($conn,"select * from stud where rfid_num ='$rfid' ");
$count = mysqli_num_rows($sql);
if ($count == 0) {
header("location:notexist.php");
}else{
while( $row = mysqli_fetch_array($sql)) {
$rfid=$row['rfid_num'];
$id=$row['id'];
$name0 = $row['name'];
$course0 = $row['course'];
$image = $row['image'];
$sql1 = mysqli_query($conn,"select * from student_att where rfid_num ='$rfid' order by number DESC limit 1 ");
while( $row = mysqli_fetch_array($sql1)) {
if(empty($row['timeout'])){
header("location:logout.php");
}else{
$InsertSql = "INSERT INTO student_att(rfid_num,id,name,course,image,timein) VALUES ('$rfid','$id','$name0','$course0','$image','$time')";
$res = mysqli_query($conn, $InsertSql);
}
}
}
}
}
?>

How to make pagination in PHP, mysql

I wonder how to make pagination in PHP
May I ask you a favor of you?
This code have not error.
But I do not know what $current_page make
<?php
$con = mysqli_connect("localhost", "root", "", "dbdb") or die("error");
$page_size = 10;
$page_list_size = 10;
if (isset($_POST['search'])) {
$valueTosearch = $_POST['valueTosearch'];
$query = "SELECT * FROM `shipment` WHERE CONCAT(`Ship_Date`,`Model_No`,`Serial_No`)LIKE'%" . $valueTosearch . "%' ORDER BY Ship_Date DESC LIMIT $page_size";
$search_result = filterTable($query);
} else {
$query = "SELECT * FROM `shipment` ORDER BY Ship_Date DESC LIMIT $page_size";
$search_result = filterTable($query);
}
function filterTable($query) {
$con = mysqli_connect("localhost", "root", "", "dbdb");
$filter_Result = mysqli_query($con, $query);
return $filter_Result;
}
echo "<center><h1>info</h1></center> <br><br>";
$ret = mysqli_query($con, $query);
if ($ret) {
echo mysqli_num_rows($ret), " <br><br>";
$count = mysqli_num_rows($ret);
} else {
echo "error :" . mysqli_error($con);
exit();
}
$query = "SELECT count(*) FROM shipment";
$result_count = mysqli_query($con, $query);
$result_row = mysqli_fetch_array($result_count);
$total_row = $result_row[0];
if ($total_row <= 0)
$total_row = 0;
$total_page = ceil($total_row / $page_size);
$current_page = ceil();
?>
I wonder how to make pagination in PHP
May I ask you a favor of you?
This code have not error.
But I do not know what $current_page make
I think you have a mistake on the mysql query, limitting to $page_size you will get only a number of results = $page_size, but you will always get the $page-size 1st elements, if you want to page results you need to add offset.
This way first page will be $page_size elements with offset 0, second page $page_size elements with $page_size offset, and in general the nth page will be $page_size elements with (n-1) times $page_size offset.
In the last line you are missing an argument:
$current_page = ceil(); should be $current_page = ceil($lowest_element_in_result_set/$page_size);
$current_page should be the number of the page user want to view and this would be a input from user.
this is my implementation.
hope this will help you .
$pagenum = $_GET['pagenum'];//get the page number user want to view
$con = mysqli_connect("localhost", "root", "9272", "new_platform_7");
$query = 'select * from table1';
$filter_Result = mysqli_query($con, $query);//execute query without limit to get total number of rows
$rows = mysqli_num_rows($filter_Result);//total number of rows
if (!(isset($pagenum)))
{
$pagenum = 1;//if user hasn't give page number set page to page number 1
}
$page_rows = 30;//the page size (number of rows in each page)
$last = ceil($rows/$page_rows);//total number of pages //use ceil to avoid fractions
if ($pagenum < 1){ //if user give negative page number. set page number to 1
$pagenum = 1; //this will happen if user clicked the Previous page link when current page is 1
}elseif ($pagenum > $last) //if page number is greater than calculated number of pages . set page number to last page
{
$pagenum = $last; //this will happen if user clicked the next page link when current page is the last page
}
//($pagenum - 1) * $page_rows calculates where to start the lmit
$query.= ' limit ' .($pagenum - 1) * $page_rows .',' .$page_rows;//limit result
echo $query;
$filter_Result = mysqli_query($con, $query);//execute query with limits
//pagination links
echo 'First Page';
echo 'Previous Page';
echo 'Next Page';
echo 'Last Page';
Try this in a simple way.
<?php
$link = mysqli_connect("localhost","roo","","dbname");
if(!$link):
echo "something wrong with the database connection";
endif;
//check to see if the page (value) is available
if(isset($_GET['pageNo']):
$page = $_GET['pageNo'];
else:
$page = "";
endif;
if($page == "" || $page == 1):
$start_to_count_rowNo = 0;
else:
$start_to_count_rowNo = ($page * &item_displayed_per_page) - &item_displayed_per_page;
endif;
//here choose how many items do you want to display per page
&item_displayed_per_page = 10;
//Now select the data to display them
$query = "SELECT * FROM table_name LIMIT $start_to_count_rowNo, $item_displayed_per_page";
$readData = mysqli_query($link, $query);
//obtaining total number of data(rows) in a database
$sql = mysqli_query($link,"SELECT * FROM table_name");
$total_row = mysqli_num_rows($sql);
$pageNo = ceil($total_row / &item_displayed_per_page);
//using bootstrap add class pager in unordered list to style the list
?>
<ul class="pager">
<?php
//display out the page # like 1,2,3 ....
for($i = 0; $i <= $pageNo; $i++){
echo "</li><a href='pagename.php?pageNo={$i}'>{$i}</a></li>";
}
?>
</ul>

Php code returns empty page. tried all other solutions

I am trying to learn php. I wrote code to insert post to database and get posts to display on the home page. I wrote pagination code as well and tried to run it. but as a result php is returning an empty page with no code and no error.
Tried every thing. Went to through most of the answers here. I still can figure out the error.
<?php
$con = mysqli_connect("localhost","root","password","social") or die ("Connection was not established");
//function for getting topics
function getTopics() {
global $con;
$get_topics = "select * from topics";
$run_topics = mysqli_query($con,$get_topics);
while($row=mysqli_fetch_array($run_topics)) {
$topic_id = $row['topic_id'];
$topic_title = $row['topic_title'];
echo "<option value='$topic_id'>$topic_title</option>";
}
}
//function for insert posts to database
function insertPost() {
if(isset($_POST['sub'])) {
global $con;
$title = $_POST['title'];
$content = $_POST['content'];
$topic = $_POST['topic'];
$user = $_SESSION['user_email'];
$get_user = "SELECT * FROM users WHERE user_email = '$user'";
$run_user = mysqli_query($con,$get_user);
$row=mysqli_fetch_array($run_user);
$user_id = $row['user_id'];
$insert = "INSERT INTO posts (user_id,topic_id,post_title,post_content,post_date) VALUES ('$user_id','$topic','$title','$content',NOW())";
$run = mysqli_query($con,$insert);
if($run) {
echo "<h3>Posted to timeline. Looks great.</h3>";
$update = "Update users set posts='yes' where user_id='$user_id'";
$run_update = mysqli_query($con,$update);
}
}
}
//function to get posts
function get_posts() {
global $con;
$per_page=5;
if (isset($_GET['page'])) {
$page = $_GET['page'];
}
else {
$page=1;
}
$start_from = ($page-1)*$per_page;
$get_posts = "SELECT * FROM posts ORDER by 1 DESC LIMIT $start_from, $per_page";
$run_posts = mysqli_query($con,$get_posts);
while($row_posts=mysqli_fetch_array($run_posts)) {
$post_id = $row_posts['post_id'];
$user_id = $row_posts['user_id'];
$post_title = $row_posts['post_title'];
$content = $row_posts['post_content'];
$post_date = $row_posts['post_date'];
//gettin the user who posted the thread
$user = "SELECT * FROM users WHERE user_id='$user_id' AND posts='yes'";
$run_user = mysqli_query($con,$user);
$row_user = mysqli_fetch_array($run_user);
$user_name = $row_user['user_name'];
$user_image = $row_user['user_image'];
//displaying all at once.
echo "<div id='posts'>
<p><img src='user_images/$user_image' width='50' height='50'</p>
<h3><a href='user_profile.php?luser_id=$user_id'>$user_name</h3>
<h3>$post_title</h3>
<p>$post_date</p>
<a href='single.php?post_id=$post_id' style='float:right'><button>See replies</button></a>
</div>";
}
include("pagination.php");
}
?>
pagination code goes like this.
<?php
$query = "SELECT * FROM posts";
$result = mysqli_query($con,$query);
//count the total records
$total_posts = mysqli_num_rows($result);
//using ceil function to divide the total records per page
$total_pages = ceil($total_posts / $per_page);
//going to first page
echo "
<center>
<div id='pagination'>
<a href='welcome.php?page=1'>First Page</a>
";
for ($i=1; $i=$total_pages; $i++) {
echo "<a href='welcome.php?page=$i'>$i</a>";
}
//going to last page
echo "<a href='welcome.php?page=$total_pages'>Last Page</a></center>";
?>

Categories