Php Folks,
Why on earth does the following variable’s value “5” auto switch to “0” when I reload the page ?
$_SESSION[‘row_count’] = $row_count;
You see, when we on PAGE 1, the value is 5. Then when I reload the page with different param in url this time from “page_1” to “page_2” (pagination) then the $_SESSION[‘row_count’] = 0. While on PAGE 1, it was $
Why the God Forsaken value switch ?
[code]
function search()
{
function rows_count()
{
//Connect to Database. (DB_SERVER, BD_USERNAME, DB_PASSWORD, DB_NAME).
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$conn = mysqli_connect(“localhost”,”root”,””,”powerpage”);
$conn->set_charset(‘utf8mb4’); //Always set Charset.
if($conn === false)
{
die(“ERROR: Connection Error!. ” . mysqli_connect_error());
}
$query_1 = “SELECT COUNT(id) FROM users WHERE first_name = ? AND marital_status = ?”;
$stmt_1 = mysqli_stmt_init($conn);
if(mysqli_stmt_prepare($stmt_1,$query_1))
{
mysqli_stmt_bind_param($stmt_1,”ss”,$_POST[“first_name”],$_POST[“marital_status”]);
mysqli_stmt_execute($stmt_1);
$result_1 = mysqli_stmt_bind_result($stmt_1,$row_count);
mysqli_stmt_fetch($stmt_1);
$_SESSION[‘row_count’] = $row_count;
$_SESSION[‘form_step’] = ‘end’;
}
//Close Statement.
mysqli_stmt_close($stmt_1);
//Close Connection.
mysqli_close($conn);
}
$_POST
rows_count()
search()
<i>
</i>$query_1 = "SELECT COUNT(id) FROM users WHERE first_name = ? AND marital_status = ?";
$stmt_1 = mysqli_stmt_init($conn);
if(mysqli_stmt_prepare($stmt_1,$query_1))
{
mysqli_stmt_bind_param($stmt_1,"ss",$_POST["first_name"],$_POST["marital_status"]);
mysqli_stmt_execute($stmt_1);
$result_1 = mysqli_stmt_bind_result($stmt_1,$row_count);
mysqli_stmt_fetch($stmt_1);
$_SESSION['row_count'] = $row_count;
<i>
</i>$result_1 = mysqli_stmt_bind_result($stmt_1,$row_count);
<i>
</i>function search()
{echo __LINE__; echo "<br>";//DELETE
function rows_count()
{
//Connect to Database. (DB_SERVER, BD_USERNAME, DB_PASSWORD, DB_NAME).
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$conn = mysqli_connect("localhost","root","","powerpage");
$conn->set_charset('utf8mb4'); //Always set Charset.
<i> </i> if($conn === false)
<i> </i> {
<i> </i> die("ERROR: Connection Error!. " . mysqli_connect_error());
<i> </i> }
<i> </i>
<i> </i> $query_1 = "SELECT COUNT(id) FROM users WHERE first_name = ? AND marital_status = ?";
<i> </i> $stmt_1 = mysqli_stmt_init($conn);
<i> </i> if(mysqli_stmt_prepare($stmt_1,$query_1))
<i> </i> {
<i> </i> mysqli_stmt_bind_param($stmt_1,"ss",$_POST["first_name"],$_POST["marital_status"]);
<i> </i> mysqli_stmt_execute($stmt_1);
<i> </i> $result_1 = mysqli_stmt_bind_result($stmt_1,$row_count);
<i> </i> mysqli_stmt_fetch($stmt_1);
<i> </i> $_SESSION['row_count'] = $row_count;
<i> </i> GLOBAL $row_count;
<i> </i> echo __LINE__; echo "<br>";//DELETE
<i> </i> $_SESSION['form_step'] = 'end'; //$form_step = 'end'; WRONG
<i> </i> //fetch_rows();
<i> </i> }
<i> </i> //Close Statement.
<i> </i> mysqli_stmt_close($stmt_1);
<i> </i> //Close Connection.
<i> </i> mysqli_close($conn);
<i> </i>}
<i> </i>function fetch_rows()
<i> </i>{ echo __LINE__; echo "<br>";//DELETE
<i> </i> $form_step = $_GET['form_step'];
<i> </i>
<i> </i> $page_number = $_GET['page'];
<i> </i> $result_per_page = $_GET['page_limit'];
<i> </i> $offset = (($page_number * $result_per_page) - $result_per_page); //Offset (Row Number that 'Starts' on page).
<i> </i> $last_row_on_page = ($page_number * $result_per_page); //Max Result (Row Number that 'Ends' on page).
<i> </i> $previous_page = $page_number-1;
<i> </i> $next_page = $page_number+1;
<i> </i>
<i> </i> echo "Row Start: $offset";echo "<br>";
<i> </i> echo "Row End: $last_row_on_page";echo "<br>";
<i> </i>
<i> </i> //Connect to Database. (DB_SERVER, BD_USERNAME, DB_PASSWORD, DB_NAME).
<i> </i> mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
<i> </i> $conn = mysqli_connect("localhost","root","","powerpage");
<i> </i> $conn->set_charset('utf8mb4'); //Always set Charset.
<i> </i> if($conn === false)
<i> </i> {
<i> </i> die("ERROR: Connection Error!. " . mysqli_connect_error());
<i> </i> }
<i> </i> $query_2 = "SELECT * FROM users WHERE first_name = ? AND marital_status = ? ORDER BY id LIMIT $offset,$last_row_on_page";
<i> </i> $stmt_2 = mysqli_stmt_init($conn);
<i> </i> if(mysqli_stmt_prepare($stmt_2,$query_2))
<i> </i> {echo __LINE__; echo "<br>";//On PAGINATION PAGE 2, THIS GETS ECHOED. IT IS LINE: 111.
<i> </i> mysqli_stmt_bind_param($stmt_2,"ss",$_POST["first_name"],$_POST["marital_status"]);
<i> </i> mysqli_stmt_execute($stmt_2);
<i> </i> $result_2 = mysqli_stmt_get_result($stmt_2);
<i> </i> echo __LINE__; echo "<br>";//On PAGINATION PAGE 2, THIS GETS ECHOED. IT IS LINE: 114.
<i> </i> //Grab total number of pages to paginate.
<i> </i> $row_count = $_SESSION['row_count'];
<i> </i> //$total_pages = ceil($result_1/$result_per_page);
<i> </i> $total_pages = ceil($row_count/$result_per_page);
<i> </i>
<i> </i> echo "TOTAL PAGES: $total_pages<br><br>";
<i> </i>
<i> </i> while($row = mysqli_fetch_array($result_2,MYSQLI_ASSOC))
<i> </i> {echo __LINE__; echo "<br>";
<i> </i> //Retrieve Values.
<i> </i> $id = $row["id"];
<i> </i> $first_name = $row["first_name"];
<i> </i> $middle_name = $row["middle_name"];
<i> </i> $surname = $row["surname"];
<i> </i> $gender = $row["gender"];
<i> </i> $marital_status = $row["marital_status"];
<i> </i> $working_status = $row["working_status"];
<i> </i>
<i> </i> echo "Id: $id<br>";
<i> </i> echo "First Name: $first_name<br>";
<i> </i> echo "Middle Name: $middle_name<br>";
<i> </i> echo "Surname: $surname<br>";
<i> </i> echo "Gender: $gender<br>";
<i> </i> echo "Marital Status: $marital_status<br>";
<i> </i> echo "Working Status: $working_status<br>";
<i> </i> echo "<br>";
<i> </i> echo "<br>";
<i> </i> }
<i> </i> $i = 1;
<i> </i> while($i<=$total_pages)
<i> </i> {
<i> </i> if($i<$total_pages)
<i> </i> {
<i> </i> echo "<a href='http://localhost/power.page/pagination_test_simple_WORKING_ON_NOW_1.php?form_type=";?><?php echo $_SESSION['form_type'];?>&query_type=<?php echo $_SESSION['query_type'];?>&form_step=end&page_limit=2&page=<?php echo $i;?>'><?php echo " $i ";?></a><?php
<i> </i> }
<i> </i> elseif($i==$page_number)
<i> </i> {
<i> </i> echo "<a href='http://localhost/power.page/pagination_test_simple_WORKING_ON_NOW_1.php?form_type=";?><?php echo $_SESSION['form_type'];?>&query_type=<?php echo $_SESSION['query_type'];?>&form_step=end&page_limit=2&page=<?php echo $i;?>'><?php echo "<b> $i </b>";?></a><?php
<i> </i> }
<i> </i>
<i> </i> $i++;
<i> </i> }
<i> </i> if($page_number>$total_pages)
<i> </i> {
<i> </i> echo "<a href='http://localhost/power.page/pagination_test_simple_WORKING_ON_NOW_1.php?form_type=";?><?php echo $_SESSION['form_type'];?>&query_type=<?php echo $_SESSION['query_type'];?>&form_step=end&page_limit=2&page=<?php echo $previous_page;?>'><?php echo "<b> Previous </b>";?></a><?php
<i> </i> }
<i> </i> }
<i> </i> //Close Statement.
<i> </i> mysqli_stmt_close($stmt_2);
<i> </i> //Close Connection.
<i> </i> mysqli_close($conn);
<i> </i>
<i> </i> $_SESSION['form_step'] = 'end';
<i> </i>}
search()
$_SESSION['row_count']
$_POST
<i>
</i><?php
session_start();
echo "<pre>SESSION:n".print_r($_SESSION, 1)."</pre>";
echo "<pre>POST:n".print_r($_POST, 1)."</pre>";
<i>
</i>$_SESSION["first_name"] = $_POST["first_name"];//ADDED THIS NEW LINE.
$_SESSION["marital_status"] = $_POST["marital_status"];//ADDED THIS NEW LINE.
<i>
</i>//mysqli_stmt_bind_param($stmt_1,"ss",$_POST["first_name"],$_POST["marital_status"]);//SUBSTITUTED THIS LINE FOR THE LINE BELOW.
mysqli_stmt_bind_param($stmt_1,"ss",$_SESSION["first_name"],$_SESSION["marital_status"]);//SUBSTITUTED TO THIS LINE FROM THE LINE ABOVE.
<i>
</i><?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
session_start();
//I WAS TOLD $_POST VALUES ARE LIKELY TO BE EMPTY ON PAGINATION PAGE 1,2,3,4,5. WAS ADVISED TO ADD THIS FOLLOWING CODE TO CHECK.
echo "<pre>SESSION:n".print_r($_SESSION, 1)."</pre>";
echo "<pre>POST:n".print_r($_POST, 1)."</pre>";
?>
<!DOCTYPE HTML">
<html>
<head>
<meta name="viewport" content="width-device=width, initial-scale=1">
</head>
<body>
<?php
if(!isset($_GET['query_type']) && empty($_GET['query_type']))//'query_type' tells the script whether it is an INSERT or SELECT or UPDATE sql query. From this the script determines which type of form to display and what prepared statements lines to use.
{
die("Invalid Query!");
}
else
{
$_SESSION['query_type'] = $_GET['query_type'];
echo __LINE__; echo "<br>";//DELETE
}
echo __LINE__; echo "<br>";//DELETE
if(!isset($_GET['form_type']) && empty($_GET['form_type']))//'form_type' tells the script which form to display. (Login, Reg, Submit Personal Details, Submit Link).
{
die("Invalid Form!");
}
else
{
$_SESSION['form_type'] = $_GET['form_type'];
echo __LINE__; echo "<br>";//DELETE
<br/>
<i> </i>if(!function_exists($_SESSION['form_type']))
<i> </i>{
<i> </i> die("Invalid Form!");
<i> </i>}
<i> </i>else
<i> </i>{echo __LINE__; echo "<br>";//DELETE
<i> </i> if(!isset($_SESSION['form_step']))// || $_SESSION['form_step'] != 'end')
<i> </i> {
<i> </i> $_SESSION['form_step'] = 'start'; echo $_SESSION['form_step'];
<i> </i> echo __LINE__; echo "<br>";//DELETE
<i> </i> $_SESSION['form_type']();
<i> </i> }
<i> </i> else
<i> </i> {
<i> </i> $_SESSION['form_step'] = $_GET['form_step'];
<i> </i> echo __LINE__; echo "<br>"; echo $_SESSION['form_step'];//DELETE
<i> </i> $_SESSION['form_type']();
<i> </i> }
<i> </i>}
}
function search()
{
echo __LINE__; echo "<br>";//DELETE
//Do following if "Search" button clicked.
if($_SERVER['REQUEST_METHOD'] === 'POST') //Doing following means "Search" button clicked AND pagination numbered links are NOT clicked. Eg page 2, 3, etc.
{echo __LINE__; echo "<br>";//DELETE
//Do following if "Search" button clicked.
if(isset($_POST['search']))
{echo __LINE__; echo "<br>";//DELETE
rows_count();
fetch_rows();
echo __LINE__; echo "<br>";
die;
}
}
else //Doing following means "Search" button not clicked but pagination numbered links are clicked. Eg page 2, 3, etc.
{
echo __LINE__; echo "<br>";//DELETE
//Do following if "Search" button not clicked but pagination numbered links are clicked. Eg Page 2, 3, etc..
//rows_count(); //SHOULD I RUN THIS FUNCTION AGAIN FROM PAGE 2,3,4, ETC OR NOT ?
fetch_rows();
echo __LINE__; echo "<br>";//DELETE
}
}
function rows_count()
{
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$conn = mysqli_connect("localhost","root","","powerpage");
$conn->set_charset('utf8mb4'); //Always set Charset.
<i> </i>if($conn === false)
<i> </i>{
<i> </i> die("ERROR: Connection Error!. " . mysqli_connect_error());
<i> </i>}
<i> </i>$query_1 = "SELECT COUNT(id) FROM users WHERE first_name = ? AND marital_status = ?";
<i> </i>$_SESSION["first_name"] = $_POST["first_name"];//ADDED THIS NEW LINE.
<i> </i>$_SESSION["marital_status"] = $_POST["marital_status"];//ADDED THIS NEW LINE.
<i> </i>$stmt_1 = mysqli_stmt_init($conn);
<i> </i>if(mysqli_stmt_prepare($stmt_1,$query_1))
<i> </i>{
<i> </i> //mysqli_stmt_bind_param($stmt_1,"ss",$_POST["first_name"],$_POST["marital_status"]);//SUBSTITUTED THIS LINE FOR THE LINE BELOW.
<i> </i> mysqli_stmt_bind_param($stmt_1,"ss",$_SESSION["first_name"],$_SESSION["marital_status"]);//SUBSTITUTED TO THIS LINE FROM THE LINE ABOVE.
<i> </i> mysqli_stmt_execute($stmt_1);
<i> </i> $result_1 = mysqli_stmt_bind_result($stmt_1,$row_count);
<i> </i> mysqli_stmt_fetch($stmt_1);
<i> </i> $_SESSION['row_count'] = $row_count;
<i> </i> echo __LINE__; echo "<br>";//DELETE
<i> </i> $_SESSION['form_step'] = 'end';
<i> </i> //fetch_rows();
<i> </i>}
<i> </i>//Close Statement.
<i> </i>mysqli_stmt_close($stmt_1);
<i> </i>//Close Connection.
<i> </i>mysqli_close($conn);
}
function fetch_rows()
{ echo __LINE__; echo "<br>";//DELETE
$form_step = $_GET['form_step'];
<br/>
<i> </i>$page_number = $_GET['page'];
<i> </i>$result_per_page = $_GET['page_limit'];
<i> </i>$offset = (($page_number * $result_per_page) - $result_per_page); //Offset (Row Number that 'Starts' on page).
<i> </i>$last_row_on_page = ($page_number * $result_per_page); //Max Result (Row Number that 'Ends' on page).
<i> </i>$previous_page = $page_number-1;
<i> </i>$next_page = $page_number+1;
<i> </i>
<i> </i>echo "Row Start: $offset";echo "<br>";
<i> </i>echo "Row End: $last_row_on_page";echo "<br>";
<i> </i>
<i> </i>//Connect to Database. (DB_SERVER, BD_USERNAME, DB_PASSWORD, DB_NAME).
<i> </i>mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
<i> </i>$conn = mysqli_connect("localhost","root","","powerpage");
<i> </i>$conn->set_charset('utf8mb4'); //Always set Charset.
<i> </i>if($conn === false)
<i> </i>{
<i> </i> die("ERROR: Connection Error!. " . mysqli_connect_error());
<i> </i>}
<i> </i>$query_2 = "SELECT * FROM users WHERE first_name = ? AND marital_status = ? ORDER BY id LIMIT $offset,$last_row_on_page";
<i> </i>$stmt_2 = mysqli_stmt_init($conn);
<i> </i>if(mysqli_stmt_prepare($stmt_2,$query_2))
<i> </i>{echo __LINE__; echo "<br>";
<i> </i> //mysqli_stmt_bind_param($stmt_2,"ss",$_POST["first_name"],$_POST["marital_status"]);//SUBSTITUTED THIS LINE FOR THE LINE BELOW.
<i> </i> mysqli_stmt_bind_param($stmt_2,"ss",$_SESSION["first_name"],$_SESSION["marital_status"]);//SUBSTITUTED TO THIS LINE FROM THE LINE ABOVE.
<i> </i> mysqli_stmt_execute($stmt_2);
<i> </i> $result_2 = mysqli_stmt_get_result($stmt_2);
<i> </i> echo __LINE__; echo "<br>";
<i> </i> //Grab total number of pages to paginate.
<i> </i> $row_count = $_SESSION['row_count'];
<i> </i> //$total_pages = ceil($result_1/$result_per_page);
<i> </i> $total_pages = ceil($row_count/$result_per_page);
<i> </i>
<i> </i> echo "TOTAL PAGES: $total_pages<br><br>";
<i> </i>
<i> </i> while($row = mysqli_fetch_array($result_2,MYSQLI_ASSOC))
<i> </i> {echo __LINE__; echo "<br>";
<i> </i> //Retrieve Values.
<i> </i> $id = $row["id"];
<i> </i> $first_name = $row["first_name"];
<i> </i> $middle_name = $row["middle_name"];
<i> </i> $surname = $row["surname"];
<i> </i> $gender = $row["gender"];
<i> </i> $marital_status = $row["marital_status"];
<i> </i> $working_status = $row["working_status"];
<i> </i>
<i> </i> echo "Id: $id<br>";
<i> </i> echo "First Name: $first_name<br>";
<i> </i> echo "Middle Name: $middle_name<br>";
<i> </i> echo "Surname: $surname<br>";
<i> </i> echo "Gender: $gender<br>";
<i> </i> echo "Marital Status: $marital_status<br>";
<i> </i> echo "Working Status: $working_status<br>";
<i> </i> echo "<br>";
<i> </i> echo "<br>";
<i> </i> }
<i> </i> $i = 1;
<i> </i> while($i<=$total_pages)
<i> </i> {
<i> </i> if($i<$total_pages)
<i> </i> {
<i> </i> echo "<a href='http://localhost/power.page/stack.php?form_type=";?><?php echo $_SESSION['form_type'];?>&query_type=<?php echo $_SESSION['query_type'];?>&form_step=end&page_limit=2&page=<?php echo $i;?>'><?php echo " $i ";?></a><?php
<i> </i> }
<i> </i> elseif($i==$page_number)
<i> </i> {
<i> </i> echo "<a href='http://localhost/power.page/stack.php?form_type=";?><?php echo $_SESSION['form_type'];?>&query_type=<?php echo $_SESSION['query_type'];?>&form_step=end&page_limit=2&page=<?php echo $i;?>'><?php echo "<b> $i </b>";?></a><?php
<i> </i> }
<i> </i>
<i> </i> $i++;
<i> </i> }
<i> </i> if($page_number>$total_pages)
<i> </i> {
<i> </i> echo "<a href='http://localhost/power.page/stack.php?form_type=";?><?php echo $_SESSION['form_type'];?>&query_type=<?php echo $_SESSION['query_type'];?>&form_step=end&page_limit=2&page=<?php echo $previous_page;?>'><?php echo "<b> Previous </b>";?></a><?php
<i> </i> }
<i> </i>}
<i> </i>//Close Statement.
<i> </i>mysqli_stmt_close($stmt_2);
<i> </i>//Close Connection.
<i> </i>mysqli_close($conn);
<i> </i>
<i> </i>$_SESSION['form_step'] = 'end';
<i> </i>echo $_POST["first_name"];// THESE ARE FAILING TO ECHO!
<i> </i>echo $_POST["marital_status"];// THESE ARE FAILING TO ECHO!
<i> </i>//die();
}
?>
<form action="<?php echo $_SERVER['PHP_SELF'];?>?form_type=<?php echo $_SESSION['form_type'];?>&query_type=<?php echo $_SESSION['query_type'];?>&form_step=end&page_limit=2&page=1" method='POST' enctype='text/plain'>
<?php
//Added '*' (asterisk) to indicate the 'Text Field' is a 'required' one.
echo "<label for="first_name">First Name *:</label>
<input type="text" name="first_name" placeholder="First Name" value = "">";?>
<br>
<?php
echo "<label for="marital_status">Marital Status *:</label>";
echo "<select name="marital_status">";
echo "<option value="single">Single</option>";
echo "<option value="married">Married</option>";
echo "</select>";
echo "<br>";
?>
<input type="submit" name="search" value="Search">
</form>
<i>
</i>
$offset = (($page_number * $result_per_page) - $result_per_page); //Offset (Row Number that 'Starts' on page).
$last_row_on_page = ($page_number * $result_per_page); //(Row Number that 'Ends' on page).
<i>
</i>$offset = (($page_number * $result_per_page) - $result_per_page); //Offset (Row Number that 'Starts' on page).
$last_row_on_page = ($page_number * $result_per_page); //(Row Number that 'Ends' on page).
$query_2 = "SELECT * FROM users WHERE first_name = ? AND marital_status = ? ORDER BY id LIMIT $offset,$last_row_on_page";
<i>
</i>$offset = (($page_number * $result_per_page) - $result_per_page); //Offset (Row Number that 'Starts' on page).
$max_rows_on_page = 2;
$query_2 = "SELECT * FROM users WHERE first_name = ? AND marital_status = ? ORDER BY id LIMIT $offset,$max_rows_on_page ";
<i>
</i><?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
session_start();
//I WAS TOLD $_POST VALUES ARE LIKELY TO BE EMPTY ON PAGINATION PAGE 1,2,3,4,5. WAS ADVISED TO ADD THIS FOLLOWING CODE TO CHECK.
echo "<pre>SESSION:n".print_r($_SESSION, 1)."</pre>";
echo "<pre>POST:n".print_r($_POST, 1)."</pre>";
?>
<!DOCTYPE HTML">
<html>
<head>
<meta name="viewport" content="width-device=width, initial-scale=1">
</head>
<body>
<?php
if(!isset($_GET['query_type']) && empty($_GET['query_type']))//'query_type' tells the script whether it is an INSERT or SELECT or UPDATE sql query. From this the script determines which type of form to display and what prepared statements lines to use.
{
die("Invalid Query!");
}
else
{
$_SESSION['query_type'] = $_GET['query_type'];
echo __LINE__; echo "<br>";//DELETE
}
echo __LINE__; echo "<br>";//DELETE
if(!isset($_GET['form_type']) && empty($_GET['form_type']))//'form_type' tells the script which form to display. (Login, Reg, Submit Personal Details, Submit Link).
{
die("Invalid Form!");
}
else
{
$_SESSION['form_type'] = $_GET['form_type'];
echo __LINE__; echo "<br>";//DELETE
<br/>
<i> </i>if(!function_exists($_SESSION['form_type']))
<i> </i>{
<i> </i> die("Invalid Form!");
<i> </i>}
<i> </i>else
<i> </i>{echo __LINE__; echo "<br>";//DELETE
<i> </i> if(!isset($_SESSION['form_step']))// || $_SESSION['form_step'] != 'end')
<i> </i> {
<i> </i> $_SESSION['form_step'] = 'start'; echo $_SESSION['form_step'];
<i> </i> echo __LINE__; echo "<br>";//DELETE
<i> </i> $_SESSION['form_type']();
<i> </i> }
<i> </i> else
<i> </i> {
<i> </i> $_SESSION['form_step'] = $_GET['form_step'];
<i> </i> echo __LINE__; echo "<br>"; echo $_SESSION['form_step'];//DELETE
<i> </i> $_SESSION['form_type']();
<i> </i> }
<i> </i>}
}
function search()
{
echo __LINE__; echo "<br>";//DELETE
//Do following if "Search" button clicked.
if($_SERVER['REQUEST_METHOD'] === 'POST') //Doing following means "Search" button clicked AND pagination numbered links are NOT clicked. Eg page 2, 3, etc.
{echo __LINE__; echo "<br>";//DELETE
//Do following if "Search" button clicked.
if(isset($_POST['search']))
{echo __LINE__; echo "<br>";//DELETE
$_SESSION["first_name"] = $_POST["first_name"];//ADDED THIS NEW LINE.
$_SESSION["marital_status"] = $_POST["marital_status"];//ADDED THIS NEW LINE.
<br/>
<i> </i> rows_count();
<i> </i> fetch_rows();
<i> </i> echo __LINE__; echo "<br>";
<i> </i> die;
<i> </i> }
<i> </i>}
<i> </i>else //Doing following means "Search" button not clicked but pagination numbered links are clicked. Eg page 2, 3, etc.
<i> </i>{
<i> </i> echo __LINE__; echo "<br>";//DELETE
<i> </i> //Do following if "Search" button not clicked but pagination numbered links are clicked. Eg Page 2, 3, etc..
<i> </i> //rows_count(); //SHOULD I RUN THIS FUNCTION AGAIN FROM PAGE 2,3,4, ETC OR NOT ?
<i> </i> fetch_rows();
<i> </i> echo __LINE__; echo "<br>";//DELETE
<i> </i>}
}
function rows_count()
{
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$conn = mysqli_connect("localhost","root","","powerpage");
$conn->set_charset('utf8mb4'); //Always set Charset.
<br/>
<i> </i>if($conn === false)
<i> </i>{
<i> </i> die("ERROR: Connection Error!. " . mysqli_connect_error());
<i> </i>}
<i> </i>$query_1 = "SELECT COUNT(id) FROM users WHERE first_name = ? AND marital_status = ?";
<i> </i>//$_SESSION["first_name"] = $_POST["first_name"];//ADDED THIS NEW LINE.
<i> </i>//$_SESSION["marital_status"] = $_POST["marital_status"];//ADDED THIS NEW LINE.
<i> </i>$stmt_1 = mysqli_stmt_init($conn);
<i> </i>if(mysqli_stmt_prepare($stmt_1,$query_1))
<i> </i>{
<i> </i> //mysqli_stmt_bind_param($stmt_1,"ss",$_POST["first_name"],$_POST["marital_status"]);//SUBSTITUTED THIS LINE FOR THE LINE BELOW.
<i> </i> mysqli_stmt_bind_param($stmt_1,"ss",$_SESSION["first_name"],$_SESSION["marital_status"]);//SUBSTITUTED TO THIS LINE FROM THE LINE ABOVE.
<i> </i> mysqli_stmt_execute($stmt_1);
<i> </i> $result_1 = mysqli_stmt_bind_result($stmt_1,$row_count);
<i> </i> mysqli_stmt_fetch($stmt_1);
<i> </i> $_SESSION['row_count'] = $row_count;
<i> </i> echo __LINE__; echo "<br>";//DELETE
<i> </i> $_SESSION['form_step'] = 'end';
<i> </i> //fetch_rows();
<i> </i>}
<i> </i>//Close Statement.
<i> </i>mysqli_stmt_close($stmt_1);
<i> </i>//Close Connection.
<i> </i>mysqli_close($conn);
}
function fetch_rows()
{ echo __LINE__; echo "<br>";//DELETE
$form_step = $_GET['form_step'];
<br/>
<i> </i>$page_number = $_GET['page'];
<i> </i>$result_per_page = $_GET['page_limit'];
<i> </i>$offset = (($page_number * $result_per_page) - $result_per_page); //Offset (Row Number that 'Starts' on page).
<i> </i>$last_row_on_page = ($page_number * $result_per_page); //Max Result (Row Number that 'Ends' on page).
<i> </i>$previous_page = $page_number-1;
<i> </i>$next_page = $page_number+1;
<i> </i>
<i> </i>echo "Row Start: $offset";echo "<br>";
<i> </i>echo "Row End: $last_row_on_page";echo "<br>";
<i> </i>
<i> </i>//Connect to Database. (DB_SERVER, BD_USERNAME, DB_PASSWORD, DB_NAME).
<i> </i>mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
<i> </i>$conn = mysqli_connect("localhost","root","","powerpage");
<i> </i>$conn->set_charset('utf8mb4'); //Always set Charset.
<i> </i>if($conn === false)
<i> </i>{
<i> </i> die("ERROR: Connection Error!. " . mysqli_connect_error());
<i> </i>}
<i> </i>$query_2 = "SELECT * FROM users WHERE first_name = ? AND marital_status = ? ORDER BY id LIMIT $offset,$last_row_on_page";
<i> </i>$stmt_2 = mysqli_stmt_init($conn);
<i> </i>if(mysqli_stmt_prepare($stmt_2,$query_2))
<i> </i>{echo __LINE__; echo "<br>";
<i> </i> //mysqli_stmt_bind_param($stmt_2,"ss",$_POST["first_name"],$_POST["marital_status"]);//SUBSTITUTED THIS LINE FOR THE LINE BELOW.
<i> </i> mysqli_stmt_bind_param($stmt_2,"ss",$_SESSION["first_name"],$_SESSION["marital_status"]);//SUBSTITUTED TO THIS LINE FROM THE LINE ABOVE.
<i> </i> mysqli_stmt_execute($stmt_2);
<i> </i> $result_2 = mysqli_stmt_get_result($stmt_2);
<i> </i> echo __LINE__; echo "<br>";
<i> </i> //Grab total number of pages to paginate.
<i> </i> $row_count = $_SESSION['row_count'];
<i> </i> //$total_pages = ceil($result_1/$result_per_page);
<i> </i> $total_pages = ceil($row_count/$result_per_page);
<i> </i>
<i> </i> echo "TOTAL PAGES: $total_pages<br><br>";
<i> </i>
<i> </i> while($row = mysqli_fetch_array($result_2,MYSQLI_ASSOC))
<i> </i> {echo __LINE__; echo "<br>";
<i> </i> //Retrieve Values.
<i> </i> $id = $row["id"];
<i> </i> $first_name = $row["first_name"];
<i> </i> $middle_name = $row["middle_name"];
<i> </i> $surname = $row["surname"];
<i> </i> $gender = $row["gender"];
<i> </i> $marital_status = $row["marital_status"];
<i> </i> $working_status = $row["working_status"];
<i> </i>
<i> </i> echo "Id: $id<br>";
<i> </i> echo "First Name: $first_name<br>";
<i> </i> echo "Middle Name: $middle_name<br>";
<i> </i> echo "Surname: $surname<br>";
<i> </i> echo "Gender: $gender<br>";
<i> </i> echo "Marital Status: $marital_status<br>";
<i> </i> echo "Working Status: $working_status<br>";
<i> </i> echo "<br>";
<i> </i> echo "<br>";
<i> </i> }
<i> </i> $i = 1;
<i> </i> while($i<=$total_pages)
<i> </i> {
<i> </i> if($i<$total_pages)
<i> </i> {
<i> </i> echo "<a href='http://localhost/power.page/stack.php?form_type=";?><?php echo $_SESSION['form_type'];?>&query_type=<?php echo $_SESSION['query_type'];?>&form_step=end&page_limit=2&page=<?php echo $i;?>'><?php echo " $i ";?></a><?php
<i> </i> }
<i> </i> elseif($i==$page_number)
<i> </i> {
<i> </i> echo "<a href='http://localhost/power.page/stack.php?form_type=";?><?php echo $_SESSION['form_type'];?>&query_type=<?php echo $_SESSION['query_type'];?>&form_step=end&page_limit=2&page=<?php echo $i;?>'><?php echo "<b> $i </b>";?></a><?php
<i> </i> }
<i> </i>
<i> </i> $i++;
<i> </i> }
<i> </i> if($page_number>$total_pages)
<i> </i> {
<i> </i> echo "<a href='http://localhost/power.page/stack.php?form_type=";?><?php echo $_SESSION['form_type'];?>&query_type=<?php echo $_SESSION['query_type'];?>&form_step=end&page_limit=2&page=<?php echo $previous_page;?>'><?php echo "<b> Previous </b>";?></a><?php
<i> </i> }
<i> </i>}
<i> </i>//Close Statement.
<i> </i>mysqli_stmt_close($stmt_2);
<i> </i>//Close Connection.
<i> </i>mysqli_close($conn);
<i> </i>
<i> </i>$_SESSION['form_step'] = 'end';
}
?>
<form action="<?php echo $_SERVER['PHP_SELF'];?>?form_type=<?php echo $_SESSION['form_type'];?>&query_type=<?php echo $_SESSION['query_type'];?>&form_step=end&page_limit=2&page=1" method='POST'>
<?php
//Added '*' (asterisk) to indicate the 'Text Field' is a 'required' one.
echo "<label for="first_name">First Name *:</label>
<input type="text" name="first_name" placeholder="First Name" value = "">";?>
<br>
<?php
echo "<label for="marital_status">Marital Status *:</label>";
echo "<select name="marital_status">";
echo "<option value="single">Single</option>";
echo "<option value="married">Married</option>";
echo "</select>";
echo "<br>";
?>
<input type="submit" name="search" value="Search">
</form>
<i>
</i>$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).
$last_row_on_page = ($page_number * $result_per_page); //Max Result (Row Number that 'Ends' on page).
<i>
</i>$last_row_on_page = ($page_number * $result_per_page); //Max Result (Row Number that 'Ends' on page).
0.1.9 — BETA 5.22