当我刷新页面或按页面底部的链接移动到下一页或上一页时,我想保留$orderby
和$orderSort
(在查询中)。
我已尝试将orderby
和orderSort
附加到会话但由于查询而无效 - 它总是从else语句恢复为Name和ASC,这似乎是{的一个问题{1}}但是我试过没有它也会出现同样的问题。
当我转到下一页或上一页时,如何保留我的查询(只要选择没有更改)?
热门PHP部分:
isset($_POST['submit'])
提交表格:
//Next Page
if (!isset($_GET['startrow']) or !is_numeric($_GET['startrow'])) {
$startrow = 0;
} else {
$startrow = (int)$_GET['startrow'];
}
//when options are selected/submitted
if(isset($_POST['submit']))
{
if($_POST['select'] == "EventType")
{
$orderby = $_POST['select'];
if($_POST['otherType'] != "Select an Event") {
$EventType = 'WHERE `' . $orderby . '`="' . $_POST["otherType"] . '"';
}
}
else
{
$orderby = $_POST['select'];
}
$orderSort = $_POST['agree'];
}
else
{
$orderby = "Name";
$orderSort = "ASC";
}
if ($stmt = $local->prepare("SELECT * FROM importdata $EventType ORDER BY `$orderby` $orderSort LIMIT ?,10")) {
$stmt->bind_param('i', $startrow);
$stmt->execute();
$Recordset1 = $stmt->get_result();
$row_Recordset1 = $Recordset1->fetch_assoc();
$stmt->close();
}
下一页/上一页的底部PHP
<form id="form1" name="form1" method="post" enctype="multipart/form-data">
<label for="select">Order By:</label>
<select name="select" id="selectType">
<option value="EventType">Event</option>
<option value=" Name"selected="selected">Race Name</option>
</select>
<label><input type="radio" name="agree" value="ASC" checked>Ascend</label>
<label><input type="radio" name="agree" value="DESC">Descend</label>
<input type="submit" name="submit" value="Submit">
</form>
答案 0 :(得分:0)
您必须将参数(orderBy
等)写入每个链接,因为HTTP是无状态协议。这非常繁琐,所以我建议寻找一个适合你的框架。
会话也应该起作用,对于一个简单的应用程序,它可能是一个更容易的灵魂。你究竟是如何存储会话变量的?
答案 1 :(得分:0)
我建议这样的事情:
// Get our defaults for these variables.
$orderBy = isset($_SESSION['orderBy']) ? $_SESSION['orderBy'] : 'Name';
$orderSort = isset($_SESSION['orderSort']) ? $_SESSION['orderSort'] : 'ASC';
// When options are selected/submitted
if (isset($_POST['submit'])) {
if ($_POST['select'] == "EventType") {
$orderby = $_POST['select'];
if ($_POST['otherType'] != "Select an Event") {
$EventType = 'WHERE `' . $orderby . '`="' . $_POST["otherType"] . '"';
}
} else {
$orderby = $_POST['select'];
}
$orderSort = $_POST['agree'];
// Set our session variables for next time when we set our defaults.
$_SESSION['orderBy'] = $orderBy;
$_SESSION['orderSort'] = $orderSort;
}