squirrelworks

PHP Pagination

NameUnitPrice
Canta, Canta Mais0.99
Enter Sandman0.99
Master Of Puppets0.99
Harvester Of Sorrow0.99
The Unforgiven0.99
Sad But True0.99
Creeping Death0.99
Wherever I May Roam0.99
Welcome Home (Sanitarium)0.99
Cochise0.99
Show Me How to Live0.99
Gasoline0.99
What You Are0.99
Like a Stone0.99
Set It Off0.99
Shadow on the Sun0.99
I am the Highway0.99
Exploder0.99
Hypnotize0.99
Bring'em Back Alive0.99
Light My Way0.99
Getaway Car0.99
The Last Remaining Light0.99
Your Time Has Come0.99
Out Of Exile0.99

<?php
$conn = createConn();
/*  
- offset value is how many results we skip before printing....
- check to see if there is an offset in the querystring. If not, set offset to 0.
- set to 0 if we have selected a new genre..
- aka: prev or next was not clicked; dropdown was submitted instead
- $_GET['offset'] will result from PREV or NEXT buttons..
- depending on the button it was sent from, the value will either be contents of $prev or $next..
*/
if (isset($_GET['offset'])){
    $offset=$_GET['offset'];} 
else{
    $offset=0;}


/*
- calculate the offset for the previous link. 
- If < 0, set to 0... result is that you cannot go back from first page of any genre...
-  this subtraction decrements the $prev contents
- first run on a new genre will always result in $prev=0
*/
$prev = $offset - 25;
if ($prev<0) {
$prev=0;
}

// calculate the offset for the NEXT link
$next = $offset + 25;

// see how many records are in the table
$query = "select count(*) as quantityvar from Track";//count contains number of records
$result=mysqli_query($conn,$query);
$row = mysqli_fetch_assoc($result);
$quantity=$row['quantityvar']; //$quantity now contains total number of records in Track table


// if the next offset is greater than the number of records,
// set it to 0 to loop back to the beginning of the table
if ($next > $quantity) {
$next = 0;
}
// check for user input
// initialize the genre to 0 in case no genre was selected.
$gen = 0;




/*
-first check to see if the form DROPDOWN was submitted. 
- If so, get the genre from the form DROPDOWN SELECTION
- data will be sent via POST in this case
*/
if (isset($_POST['genre'])){
    $gen= $_POST['genre'];} 
elseif 
/* 
- if the form was not submitted, check the querystring to see if the genre was passed as part of the paging link
- paging link is PREVIOUS or NEXT.. should stay on the same genre in the dropdown
- data will be sent via GET in this case
*/
(isset($_GET['genre'])) {
    $gen=$_GET['genre'];}
?>




<form method="post" action="php-pagination.php">
<p><label for="genre">Genre:</label>
<!--FORM LINE 1/////////////////DROP DOWN SELECTION/////////////////////////////----->
<select name="genre" id="genre">
<?php
// create a select option list from the Genre table
$query = "select GenreId, Name from Genre";
$result = mysqli_query($conn,$query);
// check for errors
if (!$result) {
die(mysqli_error($conn));
}
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
echo "<option value='".$row['GenreId']."'";
// if a genre has been selected, show that genre as the preselected option
// NOTE that on first run the first item in genre list will show as preselected by default
if ($gen==$row['GenreId']) {echo " selected";}
echo ">".$row['Name']."</option>";
}   
}
?>
</select>
<input type="submit" value="Select Genre">
</p>
</form>



<table id="tableStyles">
<tr><th>Name</th><th>UnitPrice</th></tr>
<?php
// create the query. Use the offset as the starting row and limit the rows to 20.
$query = "Select Name, UnitPrice from Track";

// if a genre is found from the form or querystring, add the where clause for the selected rep
if ($gen>0) {
$query=$query." where GenreId =$gen";
}
// add the limit clause (must be after the where clause)
$query = $query." Limit $offset, 25"; //skip amt of $offset and only produce 25 records max
//if you are not coming from paging link, but have clicked 'select genre' instead...
//your offset is going to be 0 so you'll start from beginning of results

// run the query
$result = mysqli_query($conn,$query);

// check for errors
if (!$result) {
    die(mysqli_error($conn));
}
// check for results
if (mysqli_num_rows($result)> 0) {
    // loop through results and display
    while ($row = mysqli_fetch_assoc($result)) {
        echo "<tr><td>".$row['Name']."</td>";
        echo "<td>".$row['UnitPrice']."</td></tr>";
    }

} 
/*
- IF OFFSET EXCEEDS AMOUNT OF RECORDS FOR ANY GIVEN GENRE, THE TABLE OUTPUT WILL BE BLANK
- NEED CLOSING BCKET HERE SO THAT THE PRVS AND NEXT BUTTONS ARE NOT A CONDITION OF QUERY OUTPUT
- BECAUSE THE QUERY WILL PRODUCE NOTHING AFTER 2 OR 3 PAGES IF WE ARE 
SKIPPING AMOUNT OF OFFSET 25 PER PAGE BUT THE GENRE HAS LESS RESULTSS THAN THAT.. 
THIS WILL ALLOW US TO USE THE BACK BUTTON AFTER RESULTS DONT PRINT ANYMORE
- add the Previous and Next links in the last row of the table.
- include the offset and the genre in the querystring
- send data via GET... will show in url
*/
;?>

<tr id="links">
<td><a href='php-pagination.php?offset=<?php echo $prev?>&genre=<?php echo $gen?>'>PREVIOUS</a></td>
<td><a href='php-pagination.php?offset=<?php echo $next?>&genre=<?php echo $gen?>'>NEXT</a></td>
</tr>


</table>       
   



Accessibility
 --overview

API
 --REST best practices
 --REST demo
 --REST vs RPC
 --Wikipedia API

Blockchain
 --overview

Blog
 --The 'Brute Force' Mistake
 --The Bezosian Protocol: Eliminating Learned Helplessness
 --The Humility Protocol: Reality Over Reputation
 --The Jobsian Protocol: Systems Analysis as a War on Entropy
 --The Jordan Framework: Engineering a Competitive Edge

Cloud
 --AWS overview

CSS/HTML
 --Admissions Portal Simulation Lab
 --Bootstrap carousel
 --Grid demo
 --markdown demo

DevOps
 --Agile Principles
 --DevOps overview
 --Drupal, containerized
 --Prometheus & Grafana
 --RKE2: Deploying the Rancher Kubernetes Engine

Encoding
 --Overview

Ergonomics
 --Desk configuration
 --Device fleet
 --Input device array
 --keystroke mechanics
 --Phones & RSI

ERP
 --Anthology overview
 --Ellucian Banner
 --Higher Ed ERP Simulation Lab
 --PeopleSoft Campus Solutions
 --PESC standards
 --Slate data model

Git
 --syntax overview
 --troubleshooting libcrypto

Hardware
 --Device fleet
 --Electricity fundamentals
 --Homelab diagram

Identity & Access
 --Foundations
 --OIDC Integration
 --Provisioning Okta Dev Tenant

Java
 --Fundamentals

Javascript
 --Advanced Interaction: jQuery & UI Frameworks
 --input prompt demo
 --misc demo
 --Time and Date functions
 --Vue demo

Linux
 --Auditing the live interface state using ethtool
 --grep demo
 --HCI and Proxmox
 --Persistent Infrastructure Telemetry: TMUX
 --Proxmox install
 --xammp ftp server

Mail flow
 --DKIM, SPF, DMARC
 --MAPI

Microsoft
 --AZ-800: Administering Windows Server Hybrid Core Infrastructure
 --BAT scripting
 --Group Policy
 --IIS
 --robocopy
 --Server 2022 setup - Virtualbox

Misc
 --Applications
 --regex
 --Resources
 --Sustainable Computing
 --Terminology
 --Tribute to Computer Scientists

Networks
 --BGP Peering & Security Hardening Lab
 --CCNA Lammle Study Guide
 --Cisco 1921/K9 router
 --NGFW vs. Legacy
 --routing protocols
 --throughput calculations

PHP/SQL
 --Cookies
 --database interaction
 --demo, OSI Layers quiz
 --Foreign key constraint demo
 --fundamentals
 --MySQL and PHPmyAdmin setup
 --pagination
 --security
 --session variables
 --SQL fundamentals
 --structures
 --Tables display

Python
 --fundamentals

Security
 --Overview- GRC (Governance, Risk, and Compliance)
 --Security Blog
 --SSH fundamentals

Serialization
 --JSON demo
 --YAML demo