如何基于现有PHP代码实现出发与返程航班的分页及Div展示?
Hey there! Let's work through your problem step by step. First, I spot a couple of critical issues in your current code that we need to fix before building out the Div-based display and pagination:
Key Fixes & Improvements First
- SQL Logic Error: Your original
WHEREclause usesORwithout grouping, which means the origin/destination filters only apply to the departure date condition—not the return date. This would pull unintended flights. - SQL Injection Risk: Directly concatenating
$_REQUESTvalues into your query is a huge security hole. We'll switch to prepared statements to fix this. - Data Separation: We need to split results into departure and return flights explicitly, either via separate queries or post-query grouping.
- Pagination: We'll add separate pagination controls for departure and return flights since they're distinct datasets.
Full Working Code
Here's a complete implementation that addresses all your requirements:
<?php include 'connection.php'; session_start(); // Pagination settings (adjust per your needs) $per_page = 5; // Flights per page // Get pagination parameters for departure/return flights $page_dep = isset($_REQUEST['page_dep']) ? (int)$_REQUEST['page_dep'] : 1; $offset_dep = ($page_dep - 1) * $per_page; $page_ret = isset($_REQUEST['page_ret']) ? (int)$_REQUEST['page_ret'] : 1; $offset_ret = ($page_ret - 1) * $per_page; // Sanitize input values (for safe use in count queries) $origin = htmlspecialchars($_REQUEST['origin']); $destination = htmlspecialchars($_REQUEST['destination']); $departure_date = htmlspecialchars($_REQUEST['departure']); $return_date = htmlspecialchars($_REQUEST['return']); // -------------------------- // Query Departure Flights // -------------------------- $stmt_dep = $conn->prepare(" SELECT origin, destination, departure, arrival, craftoperator, date, returndte, operatorlogo, Price FROM test WHERE origin = ? AND destination = ? AND date = ? LIMIT ? OFFSET ? "); $stmt_dep->bind_param("sssii", $origin, $destination, $departure_date, $per_page, $offset_dep); $stmt_dep->execute(); $result_dep = $stmt_dep->get_result(); $departure_flights = []; while ($row = $result_dep->fetch_assoc()) { $departure_flights[] = $row; } $stmt_dep->close(); // Get total departure flights for pagination links $count_dep = $conn->query(" SELECT COUNT(*) FROM test WHERE origin = '$origin' AND destination = '$destination' AND date = '$departure_date' ")->fetch_row()[0]; $total_pages_dep = ceil($count_dep / $per_page); // -------------------------- // Query Return Flights // -------------------------- // Note: Swap origin/destination if your return flights store reverse routes $stmt_ret = $conn->prepare(" SELECT origin, destination, departure, arrival, craftoperator, date, returndte, operatorlogo, Price FROM test WHERE origin = ? AND destination = ? AND returndte = ? LIMIT ? OFFSET ? "); $stmt_ret->bind_param("sssii", $destination, $origin, $return_date, $per_page, $offset_ret); $stmt_ret->execute(); $result_ret = $stmt_ret->get_result(); $return_flights = []; while ($row = $result_ret->fetch_assoc()) { $return_flights[] = $row; } $stmt_ret->close(); // Get total return flights for pagination links $count_ret = $conn->query(" SELECT COUNT(*) FROM test WHERE origin = '$destination' AND destination = '$origin' AND returndte = '$return_date' ")->fetch_row()[0]; $total_pages_ret = ceil($count_ret / $per_page); ?> <!DOCTYPE html> <html> <head> <title>Flight Results</title> <style> .flight-section { max-width: 1200px; margin: 2rem auto; padding: 0 1rem; } .flight-card { display: flex; justify-content: space-between; align-items: center; padding: 1.5rem; margin: 1rem 0; border: 1px solid #e0e0e0; border-radius: 8px; box-shadow: 0 2px 4px rgba(0,0,0,0.05); } .flight-details { flex: 1; } .flight-meta { text-align: right; margin-left: 2rem; } .pagination { display: flex; gap: 0.5rem; margin-top: 1rem; justify-content: center; } .pagination a { padding: 0.5rem 1rem; border: 1px solid #ddd; border-radius: 4px; text-decoration: none; color: #333; } .pagination a.active { background-color: #007bff; color: white; border-color: #007bff; } .no-flights { text-align: center; padding: 2rem; color: #666; } </style> </head> <body> <div class="flight-section"> <h2>Departure Flights</h2> <?php if (!empty($departure_flights)): ?> <?php foreach ($departure_flights as $flight): ?> <div class="flight-card"> <div class="flight-details"> <p><strong>Route:</strong> <?php echo htmlspecialchars($flight['origin']); ?> → <?php echo htmlspecialchars($flight['destination']); ?></p> <p><strong>Departure:</strong> <?php echo htmlspecialchars($flight['date'] . ' ' . $flight['departure']); ?></p> <p><strong>Arrival:</strong> <?php echo htmlspecialchars($flight['date'] . ' ' . $flight['arrival']); ?></p> </div> <div class="flight-meta"> <img src="<?php echo htmlspecialchars($flight['operatorlogo']); ?>" alt="Operator Logo" width="60"> <p><strong>Operator:</strong> <?php echo htmlspecialchars($flight['craftoperator']); ?></p> <p><strong>Price:</strong> $<?php echo htmlspecialchars($flight['Price']); ?></p> </div> </div> <?php endforeach; ?> <!-- Departure Pagination --> <div class="pagination"> <?php for ($i = 1; $i <= $total_pages_dep; $i++): ?> <a href="?origin=<?php echo urlencode($origin); ?>&destination=<?php echo urlencode($destination); ?>&departure=<?php echo urlencode($departure_date); ?>&return=<?php echo urlencode($return_date); ?>&page_dep=<?php echo $i; ?>&page_ret=<?php echo $page_ret; ?>" <?php if ($i == $page_dep) echo 'class="active"'; ?> > <?php echo $i; ?> </a> <?php endfor; ?> </div> <?php else: ?> <div class="no-flights">No departure flights found for your selected dates.</div> <?php endif; ?> </div> <div class="flight-section"> <h2>Return Flights</h2> <?php if (!empty($return_flights)): ?> <?php foreach ($return_flights as $flight): ?> <div class="flight-card"> <div class="flight-details"> <p><strong>Route:</strong> <?php echo htmlspecialchars($flight['origin']); ?> → <?php echo htmlspecialchars($flight['destination']); ?></p> <p><strong>Departure:</strong> <?php echo htmlspecialchars($flight['returndte'] . ' ' . $flight['departure']); ?></p> <p><strong>Arrival:</strong> <?php echo htmlspecialchars($flight['returndte'] . ' ' . $flight['arrival']); ?></p> </div> <div class="flight-meta"> <img src="<?php echo htmlspecialchars($flight['operatorlogo']); ?>" alt="Operator Logo" width="60"> <p><strong>Operator:</strong> <?php echo htmlspecialchars($flight['craftoperator']); ?></p> <p><strong>Price:</strong> $<?php echo htmlspecialchars($flight['Price']); ?></p> </div> </div> <?php endforeach; ?> <!-- Return Pagination --> <div class="pagination"> <?php for ($i = 1; $i <= $total_pages_ret; $i++): ?> <a href="?origin=<?php echo urlencode($origin); ?>&destination=<?php echo urlencode($destination); ?>&departure=<?php echo urlencode($departure_date); ?>&return=<?php echo urlencode($return_date); ?>&page_dep=<?php echo $page_dep; ?>&page_ret=<?php echo $i; ?>" <?php if ($i == $page_ret) echo 'class="active"'; ?> > <?php echo $i; ?> </a> <?php endfor; ?> </div> <?php else: ?> <div class="no-flights">No return flights found for your selected dates.</div> <?php endif; ?> </div> </body> </html>
Important Notes
- Security: We use prepared statements to prevent SQL injection, and
htmlspecialchars()to avoid XSS attacks when rendering user-supplied data. - Route Swap: For return flights, we swapped
originanddestinationin the query—adjust this if yourtesttable stores return routes differently. - Styling: The included CSS gives you a clean, responsive card layout. Tweak it to match your desired design.
- Pagination: Each flight type has independent pagination controls that preserve your original search parameters.
内容的提问来源于stack exchange,提问作者Joe Kasedde
相关产品推荐
相关产品推荐

