Php Folks,
Creating a pagination.
Url looks like this:
Look at these 2 buttons.
““
<button name=search value=” “>Search</button><br>
““
<button type=”submit” name=”search” value=”search”>Search</button>
Since both buttons are valid, I am testing their activity using both of them.
Here is the result.
When I click the 1st button, I am taken to a url that looks like this:
I don’t want the “search=” appearing in the url. The form name is “search” (form name=”search”) and that is appearing in the url which I don’t want.
Q1A. How to fix this ?
Also, I do not want the “+” appearing in the url.
Q1B. How to fix this without deleting the form name ? And where did this “+” come from ?
When I click the 2nd button, I am taken to a url that looks like this:
I don’t want the “search=” appearing in the url. The form name is “search” (form name=”search”) and that is appearing in the url which I don’t want.
Q2A. How to fix this without deleting the form name ?
Q3.
Why I get displayed no results from db tbl ? The tbl is “links” and column is “keywords” and entry is “heman”. This entry exists in row 0 in tbl. And so, the result should have been displayed.
conn.php
““
<?php
//https://phpdelusions.net/mysqli/mysqli_connect
$server = ‘localhost’;
$db_user = ‘root’;
$db_password = ”;
$db = ‘test’;
mysqli_report(MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT);
$conn = mysqli_connect($server,$db_user,$db_password,$db);
mysqli_set_charset($conn,’utf8mb4′);
if(mysqli_connect_error())
{
echo “Mysqli Connection Error” . mysqli_connect_error();
}
elseif(mysqli_connect_errno())
{
echo “Mysqli Connection Error Number” . mysqli_connect_errno();
}
?>
error_reporting.php
““
<?php
//https://phpdelusions.net/articles/error_reporting
ini_set(‘display_errors’,’1′);
ini_set(‘display_startup_errors’,’1′);
ini_set(‘error_reporting’,’E_ALL’);//error_reporting(E_ALL);
?>
pagination_test.php
““
<?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’];
}
$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);
mysqli_stmt_fetch($stmt_1);
}
mysqli_stmt_close($stmt_1);
mysqli_close($conn);
//PAGINATION
$page_number = $_GET[‘page’];
$result_per_page = $_GET[‘page_limit’];
$offset = (($page_number * $result_per_page) – $result_per_page); //Offset (Row Number that ‘Starts’ on page).
$previous_page = $page_number-1;
$next_page = $page_number+1;
echo “Row Start: $offset”; echo “<br>”;
$query_2 = “SELECT 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))
{
mysqli_stmt_bind_param($stmt_2,”ss”,$keywords,$keywords);
mysqli_stmt_execute($stmt_2);
$result_2 = mysqli_stmt_get_result($stmt_2);
//Grab total number of pages to paginate.
//$total_pages = ceil($result_1/$result_per_page);
$total_pages = ceil($row_count/$result_per_page);
echo “TOTAL PAGES: $total_pages<br><br>”;
if(!$result_2)
{
die(“fetching Error”);
}
while($row = mysqli_fetch_array($result_2,MYSQLI_ASSOC))
{
//Retrieve Values.
$page_url = $row[“page_url”];
$link_anchor_text = $row[“link_anchor_text”];
$page_description = $row[“page_description”];
$keyphrases= $row[“keyphrases”];
$keywords = $row[“keywords”];
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>”;
}
$i = 1;
while($i<=$total_pages)
{
if($i<$total_pages)
{
echo “<a href=”pagination_test.php?page_limit=10&page=$i”>”;?><?php echo ” $i “;?></a><?php
}
elseif($i==$page_number)
{
echo “<a href=”pagination_test.php?page_limit=10&page=$i”>”;?><?php echo “<b> $i </b>”;?></a><?php
}
$i++;
}
if($page_number>$total_pages)
{
echo “?><a href=”pagination_test.php?page_limit=10&page=$previous_page”>”;?><?php echo “<b> Previous </b>”;?></a><?php
}
}
//Close Statement.
mysqli_stmt_close($stmt_2);
//Free Result.
mysqli_free_result($result_2);
//Close Connection.
mysqli_close($conn);
}
}
?>
<form method = “GET”>
<label for=”keywords”>Keywords:*</label>
<input type=”text” name=”keywords” id=”keywords” placeholder=”Input Keywords” required>
<br>
<button name=search value=” “>Search</button><br>
<button type=”submit” name=”search” value=”search”>Search</button>
<br>
<input type=”reset”>
<br>
</form>
Q4. When I click the “Search” button and the page reloads, the html form disappears. Why ?
Ofcourse, if I put the html form above the php code then it doesn’t disappear. But I am instructed to put the php above the html form. And so, did it.