我会尽力解释这一点。我有两个从 MySQL 中提取数据的查询,一个是关于公园评论的数据,另一个是关于乘车评论的数据。两个查询都按 排序Review Date
。
我在网页上想要的是一份按排序的评论列表,Review Date
因此公园评论和骑行评论将在一个列表中混合在一起。
几乎就像在运行两个 foreach 循环,每个查询一个,但根据日期轮流执行。
这是两个查询:对于游乐设施
$query4= 'SELECT review_id, review, tpf_reviews_rides.user, DATE_FORMAT(date_ridden, "%d %M %Y") AS Date_Ridden, DATE_FORMAT(review_date, "%d %M %Y") AS Review_Date, tpf_rides.name AS ride_name, rating, tpf_parks.name AS park_name, country
FROM tpf_reviews_rides
INNER JOIN tpf_rides ON tpf_reviews_rides.ride_id = tpf_rides.ride_id
INNER JOIN tpf_ratings_rides ON tpf_reviews_rides.rating_link = tpf_ratings_rides.rating_id
INNER JOIN tpf_parks ON tpf_reviews_rides.park_id = tpf_parks.park_id
ORDER BY review_date DESC, review_id DESC LIMIT '.$start_from.' , '.$limit.'';
对于公园:
$query5= 'SELECT review_id, review, tpf_reviews_parks.user, DATE_FORMAT(date_visited, "%d %M %Y") AS Date_Visited, DATE_FORMAT(review_date, "%d %M %Y") AS Review_Date, rating, tpf_parks.name, country
FROM tpf_reviews_parks
INNER JOIN tpf_ratings_parks ON tpf_reviews_parks.rating_link = tpf_ratings_parks.rating_id
INNER JOIN tpf_parks ON tpf_reviews_parks.park_id = tpf_parks.park_id
ORDER BY review_date DESC, review_id DESC LIMIT '.$start_from.' , '.$limit.'';
这就是我分别显示结果的方式:对于游乐设施:
foreach ($res4 AS $row4) {
printf('
<h2 style="display:inline;">%s</h2> <h3 style="display:inline;">- %s, %s</h3>
<h3>Rating - %sStars</h3>
<h4>Submitted by %s on %s</h4>
<p>%s</p>' . PHP_EOL, $row4['ride_name'], $row4['park_name'], $row4['country'], $row4['rating'], (htmlspecialchars($row4['user'], ENT_QUOTES, 'UTF-8')), $row4['Review_Date'], nl2br(htmlspecialchars($row4['review'], ENT_QUOTES, 'UTF-8')));
if(!empty($row4['Date_Ridden'])){
printf('<p>Date ridden: %s</p>' . PHP_EOL, $row4['Date_Ridden']);
}
printf('<a class="bloglink" href="#top">Back to Top</a>
<hr>');
}
For PArks:
foreach ($res5 AS $row5) {
printf('
<h2 style="display:inline;">%s</h2> <h3 style="display:inline;">- %s</h3>
<h3>Rating - %sStars</h3>
<h4>Submitted by %s on %s</h4>
<p>%s</p>' . PHP_EOL, $row5['name'], $row5['country'], $row5['rating'], (htmlspecialchars($row5['user'], ENT_QUOTES, 'UTF-8')), $row5['Review_Date'], nl2br(htmlspecialchars($row5['review'], ENT_QUOTES, 'UTF-8')));
if(!empty($row4['Date_Ridden'])){
printf('<p>Date Visited: %s</p>' . PHP_EOL, $row4['Date_Visited']);
}
printf('<a class="bloglink" href="#top">Back to Top</a>
<hr>');
}
为了使它更棘手,这个页面是分页的。看看它,因为这两个查询非常相似,如果有一种方法可以在查询级别组合,那将是最好的,并有助于保持分页代码的完整。有人对我如何实现这一目标有任何建议吗?
谢谢