I have a growing amount of code in my page. Individually, I have an okay understanding of how all the various pieces work, but I don't understand how they function TOGETHER. The first bit of code I have pulls a name from an SQL database, then passes it into another table, sorting that table by the name I've chosen:
<form method="post" enctype="multipart/form-data">
<table id="" summary="PAGE HEADER" style='width:20%'>
<tr>
<th>
<?php
include('./db.php');
$PM = mysqli_query($con, "SELECT DISTINCT PMName FROM report ORDER BY PMName ASC");
?>
<b style="font-family: Candara, Calibri, Segoe, Optima, Arial, sans-serif;">Choose PM: </b>
<br>
<span class="custom-dropdown custom-dropdown--red">
<Select class="custom-dropdown__select custom-dropdown__select--red" name="PMName" onChange="submit(this.form)"<>
<?php
while ($row = mysqli_fetch_row($PM)) {
$selected = array_key_exists('PMName', $_POST) && $_POST['PMName'] == $row[0] ? ' selected' : '';
$sel = ($table === $row[0]) ? "id='sel' selected" : "";
printf(" <option value='%s' %s>%s</option>
", $row[0], $selected, $row[0]);
}
?>
</th>
</span>
</tr>
</select>
</form>
Because the results are huge, I've built in a sort of "previous page, next page" deal so the user can display only 10 results at a time.
<?php
if (isset($_POST['next'])) {
$postedLimit = (isset($_POST['next']) ? (int) $_POST['next'] : 0);
$nextLimit = $postedLimit + 10;
session_start();
$_SESSION['page'] = ((int) $nextLimit / 10)+1;
$result = mysqli_query($con, "SELECT * FROM report WHERE PMName = '$PMSelection' AND REGNSB <> 0.000 ORDER BY RegNSB DESC Limit $nextLimit,$LimitItems");
}
?>
<table id="box-table-a" stlye="widgth:20%">
<form method="POST" action="">
<div class="container">
<div class="right">Page <?= $_SESSION['page'] ?> of <?= $totalPages ?>
<input type="submit" name="next" value="next" onclick="this.value=<?php echo $nextLimit; ?>">
</div></form></div></table>
My issue is that when I click this "next" button, this query is run:
$result = mysqli_query($con, "SELECT * FROM report WHERE PMName = '$PMSelection' AND REGNSB <> 0.000 ORDER BY RegNSB DESC Limit $nextLimit,$LimitItems");
but, (forgive my non-understanding of how this whole thing works...) $PMSelection is now empty, and is not passing any value to the SQL query, so whatever name I had the list filtered by goes away. I come from the world of VBA, where once I define something, I can use it forever... why $PMSelection HAS a value at one point and then DOESN'T have a value later on is very confusing to me....
My questions are these:
- Why is $PMSelection, once defined and previously used, not still available later on when I try to use it?
- How do I make it so that $PMSelection is HELD somewhere/somehow so I can access it in this second bit of code?