Trying to build a pagination that works in 3 phases: Start, Midway, End.
QUESTIONS:
Q1. I need to know why following line never triggers when I press the ENTER and page loads. Lines 4-11.
[code]
if(isset($_SESSION[‘form_step’]))//Q1. WHY THIS ‘IF’ NEVER TRIGGERS WHEN PRESSING ‘ENTER’ AND LOADING PAGE AND THIS LINE IS UNCOMMENTED ?
{
unset( $_SESSION[‘form_step’] );
session_unset();
session_destroy();
echo “Form Step:”; echo $_SESSION[‘form_step’];
echo “Line: 10”;
}
Q2. I need to know why the following IF gets triggered when I click any numbered links (eg page 2, page 3, Next Page, Previous Page) since session id already exists. It should only trigger if session id doesn’t exist. NOTE: This still gets triggered even if I delete/comment-out the lines 4-11 mentioned above on Q1.
[code]
if(!session_id()) //Q2. WHY THIS IF GETS TRIGGERED WHEN CLICKING ANY NUMBERED PAGE LINKS (ON PAGINATION SECTION (EG PAGE 1 2 3 ETC.)) SINCE SESSION ID ALREADY EXISTS DUE TO [‘form_step’] = ‘end’ ?
{
session_start();
$_SESSION[‘form_step’] = ‘start’;
echo session_id();
echo “Line: 20<br>”;
}
Q3. When numbered links on pagination section (eg. Page 2, Next Page, Previous Page, etc.) are clicked, should I re-count the row matches using $sql_query_1 from result_count() or should I leave the code as it currently is ?
Trying to build a pagination that works in 3 phases: Start, Midway, End.
**NOTE: Am beginner. Still on: Procedural style prepared statements mysqli. Not yet into: Oop or Pdo.
Would appreciate sample code demonstrations in them formats only.
I got a my questions written on the code comments where I am stuck and puzzled.
**SCRIPT:
This is a one/single page script. And so, different params in the url trigger different parts (functions) of the script.
**HOW SCRIPT WORKS
1.
At “Start” phase, page loads displaying webform. When Form is submitted, “midway” phase counts mysql matching rows using $sql_query_1 from result_count().
Then rows are fetched using $sql_query_2 in pagination().
If you click any numbered links on pagination section (eg. page 2, page 3, next page, previous page, etc.) then again only the matching rows are fetched using $sql_query_2 from pagination().
INITIAL URL FORMAT:
PAGINATION URL SAMPLES:
1.
2.
NOTE: Note the “form_step=” in the urls.
The Single page Script:
[code]
<?php
error_reporting(E_ALL);
if(isset($_SESSION[‘form_step’]))//Q1. WHY THIS ‘IF’ NEVER TRIGGERS WHEN PRESSING ‘ENTER’ AND LOADING PAGE AND THIS LINE IS UNCOMMENTED ?
{
unset( $_SESSION[‘form_step’] );
session_unset();
session_destroy();
echo “Form Step:”; echo $_SESSION[‘form_step’];
echo “Line: 11”;
}
if(!session_id()) //Q2. WHY THIS IF GETS TRIGGERED WHEN CLICKING ANY NUMBERED PAGE LINKS (ON PAGINATION SECTION (EG PAGE 1 2 3 ETC.)) SINCE SESSION ID ALREADY EXISTS DUE TO [‘form_step’] = ‘end’ ?
{
session_start();
$_SESSION[‘form_step’] = ‘start’;
echo session_id();
echo “Line: 20<br>”;
}
//$_SESSION[‘form_step’] = ‘start’;//DELETE THIS
if(isset($_SESSION[‘form_step’]) && $_SESSION[‘form_step’] == ‘start’)//Only run this IF or one of the upcoming ELSEIFs when page is loaded due to page refresh or ENTER pressed or numbered page links clicked on pagination section (eg. page 1 2 3 etc.).
{
echo “Line: 26<br>”;//DELETE THIS LINE
if(!isset($_GET[‘form_type’]) && empty($_GET[‘form_type’]))
{
die(“Invalid Form!”);
}
else
{
$_SESSION[‘form_type’] = $_GET[‘form_type’];
echo “Line: 34<br>”;//DELETE THIS LINE
if(!isset($_GET[‘query_type’]) && empty($_GET[‘query_type’]))
{
die(“Invalid Query!”);
}
else
{
$_SESSION[‘query_type’] = $_GET[‘query_type’];
echo “Line: 43<br>”;//DELETE THIS LINE
if(!function_exists($_SESSION[‘form_type’]))//eg. $_SESSION[‘search’]. This should trigger the search() function.
{
echo “Line: 46<br>”;//DELETE THIS LINE
die(“Invalid Form!”);
}
else
{
echo “Line: 51<br>”;//DELETE THIS LINE
$_SESSION[‘form_type’]();
}
}
}
}
elseif(isset($_SESSION[‘form_step’]) && $_SESSION[‘form_step’] == ‘midway’)//Only run this ELSEIF or the next one or the previous one when page is loaded due to page refresh or ENTER pressed or numbered page links clicked on pagination section (eg. page 1 2 3 etc.).
{
echo “Line: 59<br>”;
die(“invalid Query!”);
}
elseif(isset($_SESSION[‘form_step’]) && $_SESSION[‘form_step’] == ‘end’)//Only run this ELSEIF or the previous one or the IF before that, when page is loaded due to page refresh or ENTER pressed or numbered page links clicked on pagination section (eg. page 1 2 3 etc.).
{
echo “Line: 64<br>”;
pagination();
}
?>
<!DOCTYPE HTML”>
<html>
<head>
<meta name=”viewport” content=”width-device=width, initial-scale=1″>
</head>
<body>
<?php
function search()
{echo “Line: 79<br>”;//DELETE THIS LINE
?>
<form action=”<?php echo $_SERVER[‘PHP_SELF’];?>?form_type=<?php echo $_SESSION[‘form_type’];?>&query_type=<?php echo $_SESSION[‘query_type’];?>&form_step=midway&page_limit=2&page=1″ method=’post’ enctype=’plain/text’>
<?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>
<?php
function result_count()
{
echo “result_count function started<br>”;//DELETE THIS LINE
if(isset($_SESSION[‘form_step’]) && $_SESSION[‘form_step’] == ‘midway’)
{echo “Line: 102<br>”;//DELETE THIS LINE
//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());
}
$sql_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,$sql_query_1))
{
mysqli_stmt_bind_param($stmt_1,”ss”,$_POST[“first_name”],$_POST[“marital_status”]);
mysqli_stmt_execute($stmt_1);
mysqli_stmt_bind_result($stmt_1,$row_count);
mysqli_stmt_fetch($stmt_1);
$_SESSION[“row_count”] = $row_count;
mysqli_stmt_close($stmt_1);
mysqli_close($conn);
}
$_SESSION[‘form_step’] = ‘end’;
echo $_SESSION[‘form_step’];
echo “Line: 128<br>”;//DELETE THIS LINE
pagination();
}
}
function pagination()
{
echo “pagination function started<br>”;//DELETE THIS LINE
if(isset($_SESSION[‘form_step’]) && $_SESSION[‘form_step’] == ‘end’)
{echo “Line: 137<br>”;//DELETE THIS LINE
echo $_SESSION[‘form_step’];//DELETE THIS LINE
$page_number = $_GET[‘page’];
$result_per_page = $_GET[‘page_limit’];
$row_start = (($page_number * $result_per_page) – $result_per_page); //Offset (Row Number that ‘Starts’ on page).
$row_end = ($page_number * $result_per_page); //Row Number that ‘Ends’ on page.
$previous_page = $page_number-1;
$next_page = $page_number+1;
echo “Row Start: $row_start<br>”;
echo “Row End: $row_end<br>”;
//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());
}
$sql_query_2 = “SELECT * FROM users WHERE first_name = ? AND marital_status = ? LIMIT $row_start,$row_end”;
//$_SESSION[‘sql_query_2’] = $sql_query_2;
$stmt_2 = mysqli_stmt_init($conn);
if(mysqli_stmt_prepare($stmt_2,$sql_query_2))
{
mysqli_stmt_bind_param($stmt_2,”ss”,$_POST[“first_name”],$_POST[“marital_status”]);
mysqli_stmt_execute($stmt_2);
$result_2 = mysqli_stmt_get_result($stmt_2);
$row_count = $_SESSION[“row_count”];
$total_pages = ceil($row_count/$result_per_page);
echo “TOTAL PAGES: $total_pages<br><br>”;
echo “Line: 171<br>”;//DELETE THIS LINE
while($row = mysqli_fetch_array($result_2,MYSQLI_ASSOC))
{
//Retrieve Values.
$id = $row[“id”];
$first_name = $row[“first_name”];
$middle_name = $row[“middle_name”];
$surname = $row[“surname”];
$gender = $row[“gender”];
$marital_status = $row[“marital_status”];
$working_status = $row[“working_status”];
echo “Id: $id<br>”;
echo “First Name: $first_name<br>”;
echo “Middle Name: $middle_name<br>”;
echo “Surname: $surname<br>”;
echo “Gender: $gender<br>”;
echo “Marital Status: $marital_status<br>”;
echo “Working Status: $working_status<br>”;
}
mysqli_stmt_close($stmt_2);
mysqli_close($conn);
$i = 1;
if($page_number>$total_pages)
{
echo “<a href=’http://localhost/power.page/pagination_test_simple.php?form_type=”;?><?php echo $_SESSION[‘form_type’];?>&query_type=<?php echo $_SESSION[‘query_type’];?>&form_step=end&page_limit=2&page=<?php echo $total_pages;?>’><?php echo “<b> ->Final Page<- </b>”;?></a><?php
}
else
{
if($i<$total_pages)
{
echo “<a href=’http://localhost/power.page/pagination_test_simple.php?form_type=”;?><?php echo $_SESSION[‘form_type’];?>&query_type=<?php echo $_SESSION[‘query_type’];?>&form_step=end&page_limit=2&page=<?php echo $next_page;?>’><?php echo ” Next Page-> “;?></a><?php
}
while($i<=$total_pages)
{
if($i<$total_pages)
{
echo “<a href=’http://localhost/power.page/pagination_test_simple.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
}
elseif($i==$page_number)
{
echo “<a href=’http://localhost/power.page/pagination_test_simple.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++;
}
if($i<$total_pages)
{
echo “<a href=’http://localhost/power.page/pagination_test_simple.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 ” <-Previous Page “;?></a><?php
}
}
}
//die();
}
}
if($_SERVER[‘REQUEST_METHOD’] === ‘POST’)
{
if(isset($_POST[‘search’]))
{
$_SESSION[‘form_step’] = ‘midway’;
echo $_SESSION[‘form_step’];//DELETE THIS LINE
echo “Line: 237<br>”;//DELETE THIS LINE
result_count();
}
}
}
?>