我有一个从数据库执行查询的页面。有5列,我希望第一列和最后一列是排序超链接。这是我的代码。我希望能够点击“价格”栏目或“课程ID”按类别对其进行排序。当我点击链接时,没有任何反应,但是?sort = price显示在网址上。我是PHP和MySQL的新手,我在网上找不到任何东西。提前谢谢。
<?php
require_once ('mysqli_connect.php');
//grab data from database
$course = "SELECT course.courseID, course.courseTitle, course.credit,
book.bookTitle, book.price, book.isbn13
FROM course
INNER JOIN coursebook ON course.courseID=coursebook.course
JOIN book ON coursebook.book=book.isbn13
ORDER BY course.courseID";
$course_response = mysqli_query($dbc, $course);
$isbn = "SELECT isbn13 FROM book";
$isbn_response = mysqli_query($dbc, $isbn);
// Close connection to the database
mysqli_close($dbc);
?>
<!DOCTYPE html>
<html>
<!-- the head section -->
<head>
<title>Book Catalog</title>
</head>
<!-- the body section -->
<body>
<main>
<div>
<h1 align='center'>Book Catalog</h1>
<?php $array = array(); // make a new array to hold data
$index = 0;
?>
<?php if($course_response){ ?>
<table align="left" cellspacing="5" cellpadding="8">
<tr>
<td align="left"><a href="index.php?sort=course"><b>Course #</b>
</td>
<td align="left"><b>Course Title</b></td>
<td align="left"><b>Book Image</b></td>
<td align="left"><b>Book Title</b></td>
<td align="left"><b><a href="index.php?sort=price">Price</a></b>
</td>
</tr>
<?php
if ($_GET['sort'] == 'price') {
$query .= " ORDER BY book.price";
}elseif ($_GET['sort'] == 'course') {
$query .= " ORDER BY Description";
}
?>
<?php while($row = mysqli_fetch_array($course_response)){ ?>
<tr>
<td align="left">
<a href="www.barnesandnoble.com">
<?php echo $row['courseID']; ?></a></td>
<td align="left"> <?php echo $row['courseTitle']; ?> (<?php echo
$row['credit']; ?>)</td>
<td align="left"> <a href='bookDetails.php'>
<?php $isbnrow = $row['isbn13']; ?><img src='images/<?php echo
$isbnrow?>.jpg' width='90px' height='100px'></a></td>
<td align="left"> <?php echo $row['bookTitle']; ?></td>
<td align="left"> <?php echo "$" . $row['price']; ?></td>
</tr>
<?php } ?>
<?php } else {
echo "Couldn't issue database query<br />";
echo mysqli_error($dbc);
} ?>
</table>
</div>
</main>
</body>
</html>
答案 0 :(得分:0)
您没有将get参数传递给mysql查询来订购结果。 通过以下代码更新您的代码,
<?php
require_once ('mysqli_connect.php');
//grab data from database
$order_by = null;
if($_GET){
switch($_GET['sort']){
case 'price' :
$order_by = 'ORDER BY book.price ASC';
break;
case 'course' ;
$order_by = 'ORDER BY course.courseID DESC';
break
}
}
$course = "SELECT course.courseID, course.courseTitle, course.credit,
book.bookTitle, book.price, book.isbn13
FROM course
INNER JOIN coursebook ON course.courseID=coursebook.course
JOIN book ON coursebook.book=book.isbn13 ";
$course .= (!is_null($order_by)) ? $order_by : "ORDER BY course.courseID ASC";
$course_response = mysqli_query($dbc, $course);
if(!$course_response){
die("Error in query: " . mysqli_error($dbc));
}
?>
<!DOCTYPE html>
<html>
<head><title>Book Catalog</title></head>
<body>
<main>
<div>
<h1 align='center'>Book Catalog</h1>
<?php if($course_response){ ?>
<table align="left" cellspacing="5" cellpadding="8">
<tr>
<td align="left"><a href="index.php?sort=course"><b>Course #</b></td>
<td align="left"><b>Course Title</b></td>
<td align="left"><b>Book Image</b></td>
<td align="left"><b>Book Title</b></td>
<td align="left"><b><a href="index.php?sort=price">Price</a></b></td>
</tr>
<?php while($row = mysqli_fetch_array($course_response)){ ?>
<tr>
<td align="left"><a href="www.barnesandnoble.com"><?php echo $row['courseID']; ?></a></td>
<td align="left"> <?php echo $row['courseTitle']; ?> (<?php echo $row['credit']; ?>)</td>
<td align="left"> <a href='bookDetails.php'><?php $isbnrow = $row['isbn13']; ?><img src='images/<?php echo $isbnrow?>.jpg' width='90px' height='100px'></a></td>
<td align="left"> <?php echo $row['bookTitle']; ?></td>
<td align="left"> <?php echo "$" . $row['price']; ?></td>
</tr>
<?php } ?>
<?php } else { echo 'results not found!'; } ?>
</table>
</div>
</main>
</body>
</html>
<?php
// Close connection to the database
mysqli_close($dbc);
?>