
本文档旨在提供一种使用PHP和Ajax对通过POST方法获取的搜索结果进行A-Z排序的解决方案。我们将创建一个表单,保存POST数据,并利用PHP函数对医生列表进行排序,最终通过Ajax实现无需刷新页面的排序功能。
首先,我们需要在 search.php 文件中添加一个表单,用于触发排序操作。同时,我们需要隐藏域来保存之前的POST数据,以便在排序后仍然能保持之前的搜索条件。
<?php
include 'models/doctors.class.php';
// error_reporting(0);
$search = new doctors();
$s = []; // Initialize $s
$post_data = [];
if(isset($_POST['submit'])){
$post_data = $_POST; // Store the POST data
$s= $search->filterDoctors($_POST);
}
?>
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.5.0/font/bootstrap-icons.css">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<link rel="stylesheet" href="assets/css/search.css">
<link rel="stylesheet" href="assets/css/sanascout-font.css">
<link rel="icon" type="image/png" href="assets/images/logo-ssc1.png">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<title>Healthcare</title>
</head>
<body>
<!-- ... (之前的HTML代码) ... -->
<section>
<section class="searched-area mt-4">
<div class="container">
<div class="header66">
<div style="display: flex; justify-content: space-between;">
<p class="fs-6 popins-font fw-bold" id="text-color">Available Doctors</p>
</div>
</div>
</div>
</section>
<form id="sortForm" method="post" action="">
<?php
foreach ($post_data as $key => $value) {
echo '<input type="hidden" name="' . htmlspecialchars($key) . '" value="' . htmlspecialchars($value) . '">';
}
?>
<button type="button" id="sortAZ" class="btn btn-primary">Filter A-Z</button>
</form>
<div>
<?php
if(isset($_SESSION['msg'])){
echo $_SESSION['msg'];
unset($_SESSION['msg']);
}
?>
</div>
<section id="doctorList">
<div class="container">
<?php
if(isset($s) && is_array($s)){
foreach($s as $row1){
?>
<a href="therapist.php?id=<?php echo $row1['User_ID']; ?>" class="text-decoration-none">
<div class="therapistCardOne mx-2 popins-font my-2">
<div class="row py-2">
<div class="col-3 g-0">
<div class="imgW text-center g-0 ps-2">
<img src="assets/images/006.png" class="img-fluid ms-2" alt="" width="70px" height="80px">
</div>
</div>
<div class="col-8 g-0 ps-2">
<span class="span1"><?php echo $row1['full_name'];?></span>
<span class="ps-2">
<i class="bi bi-star-fill icon-ccc"></i>
<i class="bi bi-star-fill icon-ccc"></i>
<i class="bi bi-star-fill icon-ccc"></i>
<i class="bi bi-star-fill icon-ccc"></i>
<i class="bi bi-star icon-ccc"></i>
</span><br>
<span class="span2">Location : <?php echo $row1['location'];?></span> <br>
<span class="span3"><i class="bi bi-clock icon-cc"></i> 12:00pm - 16:00pm</span>
<span class="span4 ps-2"><i class="bi bi-geo-alt icon-cc"></i> Zurich New Clinic</span>
</div>
<div class="col-1 g-0 pe-2">
<i class="bi bi-three-dots-vertical"></i>
</div>
</div>
</div>
</a>
<?php
}
} else {
echo "<p>No doctors found.</p>";
}
?>
</div>
</section>
</section>
<!-- ... (剩余的HTML代码) ... -->
<script>
$(document).ready(function() {
$("#sortAZ").click(function(e) {
e.preventDefault(); // Prevent the default form submission
$.ajax({
type: "POST",
url: "sort_doctors.php",
data: $("#sortForm").serialize() + "&sort=az", // Serialize the form data and add a sort parameter
success: function(data) {
$("#doctorList").html(data); // Update the doctor list with the sorted data
},
error: function(xhr, status, error) {
console.error("AJAX Error:", status, error);
}
});
});
});
</script>
</body>
</html>关键修改说明:
接下来,创建 sort_doctors.php 文件来处理排序逻辑。
立即学习“PHP免费学习笔记(深入)”;
<?php
include 'models/doctors.class.php';
$search = new doctors();
// Retrieve the POST data
$post_data = $_POST;
// Check if sorting is requested
if (isset($post_data['sort']) && $post_data['sort'] == 'az') {
// Filter the doctors based on the POST data
$doctors = $search->filterDoctors($post_data);
// Sort the doctors array alphabetically by full_name
usort($doctors, function($a, $b) {
return strcmp($a['full_name'], $b['full_name']);
});
// Generate the HTML for the sorted doctor list
$output = '<div class="container">';
foreach ($doctors as $row1) {
$output .= '<a href="therapist.php?id=' . $row1['User_ID'] . '" class="text-decoration-none">';
$output .= '<div class="therapistCardOne mx-2 popins-font my-2">';
$output .= '<div class="row py-2">';
$output .= '<div class="col-3 g-0">';
$output .= '<div class="imgW text-center g-0 ps-2">';
$output .= '<img src="assets/images/006.png" class="img-fluid ms-2" alt="" width="70px" height="80px">';
$output .= '</div>';
$output .= '</div>';
$output .= '<div class="col-8 g-0 ps-2">';
$output .= '<span class="span1">' . $row1['full_name'] . '</span>';
$output .= '<span class="ps-2">';
$output .= '<i class="bi bi-star-fill icon-ccc"></i>';
$output .= '<i class="bi bi-star-fill icon-ccc"></i>';
$output .= '<i class="bi bi-star-fill icon-ccc"></i>';
$output .= '<i class="bi bi-star-fill icon-ccc"></i>';
$output .= '<i class="bi bi-star icon-ccc"></i>';
$output .= '</span><br>';
$output .= '<span class="span2">Location : ' . $row1['location'] . '</span> <br>';
$output .= '<span class="span3"><i class="bi bi-clock icon-cc"></i> 12:00pm - 16:00pm</span>';
$output .= '<span class="span4 ps-2"><i class="bi bi-geo-alt icon-cc"></i> Zurich New Clinic</span>';
$output .= '</div>';
$output .= '<div class="col-1 g-0 pe-2">';
$output .= '<i class="bi bi-three-dots-vertical"></i>';
$output .= '</div>';
$output .= '</div>';
$output .= '</div>';
$output .= '</a>';
}
$output .= '</div>';
echo $output;
} else {
echo "<p>Invalid request.</p>";
}
?>关键说明:
这是一个示例 doctors.class.php 文件的内容,你需要根据你的实际情况进行修改。
<?php
class doctors {
// 示例数据,替换成你的数据库查询逻辑
private $doctors_data = [
['User_ID' => 1, 'full_name' => 'Dr. Charlie', 'location' => 'Zurich'],
['User_ID' => 2, 'full_name' => 'Dr. Alice', 'location' => 'Geneva'],
['User_ID' => 3, 'full_name' => 'Dr. Bob', 'location' => 'Bern'],
];
public function filterDoctors($post_data) {
// 这里应该根据 $post_data 中的条件从数据库查询医生信息
// 为了示例,我们简单返回所有医生
return $this->doctors_data;
}
}
?>重要说明:
通过以上步骤,你就可以实现使用PHP和Ajax对搜索结果进行A-Z排序的功能。这种方法可以在不刷新页面的情况下,动态更新医生列表,提升用户体验。记住,代码示例只是一个起点,你需要根据你的实际情况进行修改和完善。
以上就是实现搜索结果按字母排序:PHP结合Ajax的专业教程的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号