我有一個頁面,它是php查詢單擊(單個作業列表)的結果。我想使用ORDER BY
,這樣個人可以根據截止日期,作業名稱等來排列他們的結果。保留通過_GET在頁面重新加載時檢索到的php變量
當用戶單擊變量表標題時,問題似乎會起作用。這會導致頁面刷新,然後_GET
的變量顯然不見了。我嘗試將它設置爲cookie,但我相信也許我沒有正確設置它。
這是該文件的頂部:
<?php
if($_SERVER['REQUEST_METHOD']=='GET' && isset($_GET['assignee'])){
$assignee = filter_input(INPUT_GET, 'assignee', FILTER_SANITIZE_STRING);
}
$assignee_name = $assignee;
setcookie($assignee_name, time() + (86400 * 30), "/"); // 86400 = 1 day
?>
這是直接PHP
的foreach
循環之前:
<?php
$servername = "localhost";
$username = "jobs_usr1";
$password = "xxxxxxxxx";
$dbname = "jobs_users";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT * FROM `jobs_canjobs` WHERE assignee = \"$assignee\"";
$orderBy = array('job_numb', 'job_name', 'due_date', 'show_date', 'status');
$order = 'type';
if (isset($_GET['orderBy']) && in_array($_GET['orderBy'], $orderBy)) {
$order = $_GET['orderBy'];
}
if(isset($_COOKIE[$assignee_name])){
$sql = "SELECT * FROM `jobs_canjobs` WHERE assignee = \"$assignee_name\" ORDER BY $order";
}
$results = mysqli_query($conn, $sql);
?>
然後在結果表,表頭下,我有這:
<table>
<tr style="background-color: cadetblue">
<th><a href="?orderBy=job_numb"><b>Job ID#</b></a></th>
<th><a href="?orderBy=job_name"><b>Title</b></a></th>
<th><a href="?orderBy=due_date"><b>Due Date</b></a> | <a href="?orderBy=show_date"><b>Show Date</b></a></th>
<th><a href="?orderBy=status"><b>Status</b></a></th>
</tr>
<?php
foreach ($results as $result){
$job_numb = $result['job_numb'];
$assignee = $result['assignee'];
$job_name = $result['job_name'];
$due_date = $result['due_date'];
$show_date = $result['show_date'];
$status = $result['status'];
?>
<tr>
<td>
<p><?php echo $job_numb;?></p>
</td>
<td>
<p><?php echo $job_name;?></p>
</td>
<td>
<p><?php echo $due_date;?> | <?php echo $show_date;?></p>
</td>
<td>
<p><?php echo $status;?></p>
</td>
</tr>
<? php } ?>
</table>
沒有重新排序$order
,它工作得很好 - 但是一旦有人按順序點擊th
,變量就會消失,即使在頁面重新加載後,我也不知道如何保留它。
有沒有辦法讓這個變量$assignee
頁面上,所以我可以再次引用它來顯示結果?我知道這是頁面刷新,因爲點擊後的URL不包含變量。
任何幫助,將不勝感激。謝謝。
cookie或會話。 – AbraCadaver
我想使用一個cookie,但我認爲我沒有實現它的權利。 – scoopzilla