I am learning PHP. I am trying to use pagination in my one of page. If its first page, all is working fine but as soon as I press next page button its giving me error like
Notice: Undefined index: number1 in C:\xampp\htdocs\mycode\compare.php on line 6
Notice: Undefined index: number2 in C:\xampp\htdocs\mycode\compare.php on line 7
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\mycode\compare.php on line 18
I think its due to $_GET which I am using in line 6 and 7 but I do not know why its getting destroyed as soon as I try to go on next page. My full code is like below
<?php include("includes/header.php");
require("includes/function.php");
require("language/language.php");
$number1 = $_GET['number1'];
$number2 = $_GET['number2'];
$tableName="number_status";
$targetpage = "compare.php";
$limit = 10;
$sr=0;
$query = "SELECT COUNT(*) as num FROM $tableName WHERE number = $number1 OR number = $number2 ";
$total_pages = mysqli_fetch_array(mysqli_query($mysqli,$query));
$total_pages = $total_pages['num'];
$stages = 3;
$page=0;
if(isset($_GET['page'])){
$page = mysqli_real_escape_string($mysqli,$_GET['page']);
}
if($page){
$start = ($page - 1) * $limit;
}else{
$start = 0;
}
$quotes_qry="SELECT * FROM number_status WHERE number = $number1 OR number = $number2 ORDER BY id DESC LIMIT $start, $limit";
$result=mysqli_query($mysqli,$quotes_qry);
?>
<div class="row">
<div class="col-xs-12">
<div class="card mrg_bottom">
<div class="page_title_block">
<div class="col-md-5 col-xs-12">
<div class="page_title">Number Details</div>
</div>
</div>
<div class="clearfix"></div>
<div class="row mrg-top">
<div class="col-md-12">
<div class="col-md-12 col-sm-12">
<?php if(isset($_SESSION['msg'])){?>
<div class="alert alert-success alert-dismissible" role="alert"> <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
<?php echo $client_lang[$_SESSION['msg']] ; ?></a> </div>
<?php unset($_SESSION['msg']);}?>
</div>
</div>
</div>
<div class="col-md-12 mrg-top">
<table class="table table-striped table-bordered table-hover">
<thead>
<tr>
<th>#SR</th>
<th>Number</th>
<th>Online Time</th>
<th>Offline Time</th>
<th>Duration</th>
</tr>
</thead>
<tbody>
<?php
$i=0;
while($row=mysqli_fetch_array($result))
{
$number = $row['number'];
$start_time = "<span style='color:green;'>".$row['start_time']."</span>";
$end_time = "<span style='color:red'>".$row['end_time']."</span>";
$tmp = strtotime($row['end_time']) - strtotime($row['start_time']);
$duration = floor($tmp/3600) . ":" . floor($tmp/60) . ":" . ($tmp%60);
?>
<tr>
<td><?php echo ++$sr+$start;?></td>
<td><?php echo $number ?></td>
<td><?php echo nl2br($start_time);?></td>
<td><?php echo nl2br($end_time);?></td>
<td><?php echo nl2br ($duration); ?></td>
</tr>
<?php
$i++;
}
?>
</tbody>
</table>
</div>
<div class="col-md-12 col-xs-12">
<div class="pagination_item_block">
<nav>
<?php if(!isset($_POST["quotes_search"])){ include("pagination.php");}?>
</nav>
</div>
</div>
<div class="clearfix"></div>
</div>
</div>
</div>
<?php include("includes/footer.php");?>
My pagination.php is like this
<?php
// Initial page num setup
if ($page == 0){$page = 1;}
$prev = $page - 1;
$next = $page + 1;
$lastpage = ceil($total_pages/$limit);
$LastPagem1 = $lastpage - 1;
$paginate = '';
if($lastpage > 1)
{
$paginate .= "<ul class='pagination'>";
// Previous
if ($page > 1){
$paginate.= "<li><a href='$targetpage?page=$prev' aria-label='Previous'><span aria-hidden='true'>«</span></a></li>";
}else{
$paginate.= "<li><span aria-hidden='true'>«</span></li>"; }
// Pages
if ($lastpage < 7 + ($stages * 2)) // Not enough pages to breaking it up
{
for ($counter = 1; $counter <= $lastpage; $counter++)
{
if ($counter == $page){
$paginate.= "<li class='active'><span class='active'>$counter</span></li>";
}else{
$paginate.= "<li><a href='$targetpage?page=$counter'>$counter</a></li>";}
}
}
elseif($lastpage > 5 + ($stages * 2)) // Enough pages to hide a few?
{
// Beginning only hide later pages
if($page < 1 + ($stages * 2))
{
for ($counter = 1; $counter < 4 + ($stages * 2); $counter++)
{
if ($counter == $page){
$paginate.= "<li class='active'><span class='active'>$counter</span></li>";
}else{
$paginate.= "<li><a href='$targetpage?page=$counter'>$counter</a></li>";}
}
$paginate.= "<li>...</li>";
$paginate.= "<li><a href='$targetpage?page=$LastPagem1'>$LastPagem1</a></li>";
$paginate.= "<li><a href='$targetpage?page=$lastpage'>$lastpage</a></li>";
}
// Middle hide some front and some back
elseif($lastpage - ($stages * 2) > $page && $page > ($stages * 2))
{
$paginate.= "<li><a href='$targetpage?page=1'>1</a></li>";
$paginate.= "<li><a href='$targetpage?page=2'>2</a></li>";
$paginate.= "<li>...</li>";
for ($counter = $page - $stages; $counter <= $page + $stages; $counter++)
{
if ($counter == $page){
$paginate.= "<li class='active'><span class='active'>$counter</span></li>";
}else{
$paginate.= "<li><a href='$targetpage?page=$counter'>$counter</a></li>";}
}
$paginate.= "<li>...</li>";
$paginate.= "<li><a href='$targetpage?page=$LastPagem1'>$LastPagem1</a></li>";
$paginate.= "<li><a href='$targetpage?page=$lastpage'>$lastpage</a></li>";
}
// End only hide early pages
else
{
$paginate.= "<li><a href='$targetpage?page=1'>1</a></li>";
$paginate.= "<li><a href='$targetpage?page=2'>2</a></li>";
$paginate.= "<li>...</li>";
for ($counter = $lastpage - (2 + ($stages * 2)); $counter <= $lastpage; $counter++)
{
if ($counter == $page){
$paginate.= "<li class='active'><span class='active'>$counter</span></li>";
}else{
$paginate.= "<li><a href='$targetpage?page=$counter'>$counter</a></li>";}
}
}
}
// Next
if ($page < $counter - 1){
$paginate.= "<li><a href='$targetpage?page=$next' aria-label='Next'><span aria-hidden='true'>»</span></a></li>";
}else{
$paginate.= "<li><span aria-hidden='true'>»</span></li>";
}
$paginate.= "</ul>";
}
// pagination
echo $paginate;
?>
Let me know if someone can look and help me for get out of it. Thanks