Folks,
90% of my pagination is complete with mysqli.
The bottom part where the page numbers supposed to show up don’t show up
Why is that ?
Problem is here:
[code]
if($page_number>$total_pages)
{
echo “?><a href=”pagination_test.php?limit=$limit&page=$previous_page”>”;?><?php echo “<b> Previous </b>”;?></a><?php
}
else
{
$i = 1;
while($i<=$total_pages)
{
if($i<$total_pages)
{
echo “<a href=”pagination_test.php?limit=$limit&page=$i”>”;?><?php echo ” $i “;?></a><?php
}
elseif($i==$page_number)
{
echo “<a href=”pagination_test.php?limit=$limit&page=$i”>”;?><?php echo “<b> $i </b>”;?></a><?php
}
$i++;
}
}
How to fix the above ?
Full Code:
[code]
<?php
require ‘conn.php’;
require ‘error_reporting.php’;
?>
<!DOCTYPE HTML”>
<html>
<head>
<meta name=”viewport” content=”width-device=width, initial-scale=1″>
</head>
<body>
<?php
if($_SERVER[‘REQUEST_METHOD’] === ‘GET’)
{
if(ISSET($_GET[‘search’]))//THERE WILL BE MORE THAN ONE BUTTON ON THIS PAGE. HENCE THIS LINE.
{
if(ISSET($_GET[‘keywords’]))
{
$keywords = $_GET[‘keywords’];
}
echo __LINE__; echo “<br>”;//THIS IS LINE 24
$query_1 = “SELECT COUNT(id) FROM links WHERE keywords = ? OR keyphrases = ?”;
$stmt_1 = mysqli_stmt_init($conn);
if(mysqli_stmt_prepare($stmt_1,$query_1))
{
mysqli_stmt_bind_param($stmt_1,”ss”,$keywords,$keywords);
mysqli_stmt_execute($stmt_1);
$result_1 = mysqli_stmt_bind_result($stmt_1,$row_count);
if($bind_result_1 === FALSE)
{
echo __LINE__;echo “<br>”;
printf(“Error: %s.n”, mysqli_stmt_error($stmt));
printf(“Error: %d.n”, mysqli_stmt_errno($stmt));
die;
}
mysqli_stmt_fetch($stmt_1);
echo “Row Count: $row_count<br>”;
}
else
{
echo __LINE__;echo “<br>”;
die(“prepare failed!”);
}
mysqli_stmt_close($stmt_1);
//mysqli_close($conn);//THIS LINE CAUSED THE WHOLE PAGINATION TO MALFUNCTION GIVING FALSE ERROR PREPARE FAILED!
//PAGINATION
if(!ISSET($_GET[‘page’]))
{
$page = 1;
}
else
{
$page = $_GET[‘page’];
}
if(!ISSET($_GET[‘limit’]))
{
$limit = 2;
}
else
{
$limit = $_GET[‘limit’];
}
echo “Page: $page”; echo “<br>”;
echo “Limit: $limit”; echo “<br>”;
$offset = (($page * $limit) – $limit);
$previous_page = $page-1;
$next_page = $page+1;
echo “Row Start: $offset”; echo “<br>”;
echo __LINE__; echo “<br>”; //THIS IS LINE 78
$query_2 = “SELECT id,page_url,link_anchor_text,page_description,keyphrases,keywords FROM links WHERE keywords = ? OR keyphrases = ? ORDER by id LIMIT $offset,$limit”;
$stmt_2 = mysqli_stmt_init($conn);
if(mysqli_stmt_prepare($stmt_2,$query_2))//WHY PREPARE FAILING ON THIS LINE ?
{
mysqli_stmt_bind_param($stmt_2,”ss”,$keywords,$keywords);
//mysqli_stmt_execute($stmt_2);
if(mysqli_stmt_execute($stmt_2) === FALSE)
{
echo __LINE__;echo “<br>”;
printf(“Error: %s.n”, mysqli_stmt_error($stmt_2));
printf(“Error: %d.n”, mysqli_stmt_errno($stmt_2));
die;
}
$result_2 = mysqli_stmt_get_result($stmt_2);
//Grab total number of pages to paginate.
//$total_pages = ceil($result_1/$limit); //SHOULD I KEEP THIS LINE OR THE ONE BELOW THIS ONE ?
$total_pages = ceil($row_count/$limit); //SHOULD I KEEP THIS LINE OR THE ONE ABOVE THIS ONE ?
echo “TOTAL PAGES: $total_pages<br><br>”;
if(!$result_2)
{
die(“fetching Error”);
}
while($row = mysqli_fetch_array($result_2,MYSQLI_ASSOC))
{
//Retrieve Values.
echo __LINE__;echo “<br>”;
$id = $row[“id”];
$page_url = $row[“page_url”];
$link_anchor_text = $row[“link_anchor_text”];
$page_description = $row[“page_description”];
$keyphrases= $row[“keyphrases”];
$keywords = $row[“keywords”];
echo “Id: $id<br>”;
echo “Page Url: $page_url<br>”;
echo “Link Anchor Text: $link_anchor_text<br>”;
echo “Page Description: $page_description<br>”;
echo “Keyphrases: $keyphrases<br>”;
echo “Keywords: $keywords<br>”;
echo “<br>”;
echo “<br>”;
}
}
else
{
echo __LINE__;echo “<br>”; //THIS IS LINE 127
printf(“Error: %s.n”, mysqli_stmt_error($stmt_2));
printf(“Error: %d.n”, mysqli_stmt_errno($stmt_2));
die(“Prepare failed!”);
}
//Close Statement.
mysqli_stmt_close($stmt_2);
if($page_number>$total_pages)
{
echo “?><a href=”pagination_test.php?limit=$limit&page=$previous_page”>”;?><?php echo “<b> Previous </b>”;?></a><?php
}
else
{
$i = 1;
while($i<=$total_pages)
{
if($i<$total_pages)
{
echo “<a href=”pagination_test.php?limit=$limit&page=$i”>”;?><?php echo ” $i “;?></a><?php
}
elseif($i==$page_number)
{
echo “<a href=”pagination_test.php?limit=$limit&page=$i”>”;?><?php echo “<b> $i </b>”;?></a><?php
}
$i++;
}
}
}
//Close Connection.
mysqli_close($conn);
}
?>
<form method=’GET’ action=”<?php echo $_SERVER[‘PHP_SELF’];?>?limit=$limit&page=$page”>
<label for=”keywords”>Keywords:*</label>
<input type=”text” name=”keywords” id=”keywords” placeholder=”Input Keywords” required>
<br>
<label for=”limit”>Results per Page</label>
<select name=”limit” id=”limit”>
<option value=”10″>10</option>
<option value=”10″>25</option>
<option value=”10″>50</option>
<option value=”10″>100</option>
</select>
<br>
<button name=search value=” “>Search</button><br>
<button type=”submit” name=”search” value=”search”>Search</button>
<br>
<input type=”reset”>
<br>
</form>