我试图用$ _GET参数查询表,但是当我添加参数时,没有按指定值排序。如何写这个if语句有什么问题吗?
if (isset($_GET['sort'])) {
$sorting = $_GET['sort'];
$result = $mysqli->query("SELECT * FROM routes ORDER BY $sorting");
}
else if (!isset($_GET['sort'])) {
$result = $mysqli->query("SELECT * FROM routes _");
}
if ($result->num_rows != 0) {
$total_results = $result->num_rows;
$total_pages = ceil($total_results / $per_page);
if (isset($_GET['page']) && is_numeric($_GET['page']) && $_GET['page'] <= $total_pages) {
$show_page = $_GET['page'];
if ($show_page > 0 && $show_page <= $total_pages) {
$start = ($show_page - 1) * $per_page;
$end = $start + $per_page;
} else {
$start = 0;
$end = $per_page;
}
} else {
$start = 0;
$end = $per_page;
}
echo "<form action='../processors/delete.php' method='post'";
echo "<br/><table class='table table-striped table-bordered' id='basic-datatable' border='0' cellpadding='10'>";
echo "<thead><tr> <th>#</th>";
echo "<th><a href='adminviewer.php?sort=route_id'>Route ID</a></th>";
echo "<th><a href='adminviewer.php?sort=route_title'>Route</a></th>";
echo "<th><a href='adminviewer.php?sort=route_price'>Price</a></th>";
echo "<th><a href='adminviewer.php?sort=route_payment'>Payment</a></th>";
echo "<th><a href='adminviewer.php?sort=route_net'>Weekly Net</a></th>";
echo "<th><a href='adminviewer.php?sort=route_city'>City</a></th>";
echo "<th><a href='adminviewer.php?sort=route_state'>State</a></th>";
echo "<th><a href='adminviewer.php?sort=route_remarks'>Remarks</a></th>";
echo "</tr></thead>";
echo "<tbody>";
// Check for Sorting Request
for ($i = $start; $i < $end; $i++) {
if ($i == $total_results) {
break;
}
$result->data_seek($i);
$row = $result->fetch_row();
echo "<tr>";
echo '<td><input class="checked-box" type="checkbox" name="selec-row[]" value="' . $row[0] . '"></td>';
echo '<td>' . $row[1] . '</td>';
echo '<td><a href="detail.php?id=' . $row[0] . '">' . $row[2] . '</a></td>';
echo '<td>' . $row[3] . '</td>';
echo '<td>' . $row[4] . '</td>';
echo '<td>' . $row[5] . '</td>';
echo '<td>' . $row[6] . '</td>';
echo '<td>' . $row[7] . '</td>';
echo '<td>' . $row[8] . '</td>';
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
echo "<input class='del-btn' type='submit' value='Delete'>";
echo "</form>";
} else {
echo "No results to display!";
}
echo '<div class="col-md-12" style="text-align: center;position:relative;top:-25px;">';
echo "Showing 1 to " . $per_page . " of " . $total_results . " routes";
echo '</div>';
/* echo '<div class="col-md-6" style="text-align:right;">';
echo '<ul class="paginated-btns">';
for ($i = 1; $i <= $total_pages; $i++)
{
if (isset($_GET['page']) && $_GET['page'] == $i)
{
echo "<li><a href='adminviewer.php?page=$i&page_sel=$per_page'>$i</a></li>";
}
else
{
echo "<li><a href='adminviewer.php?page=$i&page_sel=$per_page'>$i</a></li>";
}
}
echo '</ul>';
*/
echo "</div>";
echo '</div>';
echo '</div>';
echo '</div>';
echo '<div class="pagination">';
if (!isset($_GET['page'])) {
$_GET['page'] = 1;
}
if ($_GET['page'] > $per_page) {
$next_page = $i - 1;
echo "<div class='pag-btn'><a href='adminviewer.php?page=$next_page&page_sel=$per_page'><- Previous</a></div>";
} else {
echo "<div class='pag-btn'>Previous</div>";
}
if ($_GET['page'] < $total_pages) {
$next_page = $_GET['page'] + 1;
echo "<div class='pag-btn'><a href='adminviewer.php?page=$next_page&page_sel=$per_page'>Next -></a></div>";
} else {
echo "<div class='pag-btn'>Next</div>";
}
echo '</div>';
我正在学习PHP,所以我可以想象逻辑有问题,但我似乎无法弄明白。
$_GET['sort']
的值是表格的列;例如:route_id,route_name,route_price,...