帮我写一个mvc模糊查询和多条件查询的分页显示的完整代码
时间: 2024-05-16 10:14:33 浏览: 159
好的,以下是一个简单的MVC模糊查询和多条件查询的分页显示的完整代码示例:
模型层代码:
```php
class Model {
private $dbhost = 'localhost';
private $dbname = 'database_name';
private $dbuser = 'database_username';
private $dbpass = 'database_password';
private $conn;
public function __construct() {
try {
$this->conn = new PDO("mysql:host=$this->dbhost;dbname=$this->dbname", $this->dbuser, $this->dbpass);
$this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
die("Connection failed: " . $e->getMessage());
}
}
public function search($keyword, $category, $sort, $start, $limit) {
$sql = "SELECT * FROM table_name WHERE 1=1";
if(!empty($keyword)) {
$sql .= " AND (field1 LIKE '%$keyword%' OR field2 LIKE '%$keyword%')";
}
if(!empty($category)) {
$sql .= " AND category = '$category'";
}
if(!empty($sort)) {
$sql .= " ORDER BY $sort";
}
$sql .= " LIMIT $start, $limit";
$stmt = $this->conn->prepare($sql);
$stmt->execute();
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
public function count($keyword, $category) {
$sql = "SELECT COUNT(*) FROM table_name WHERE 1=1";
if(!empty($keyword)) {
$sql .= " AND (field1 LIKE '%$keyword%' OR field2 LIKE '%$keyword%')";
}
if(!empty($category)) {
$sql .= " AND category = '$category'";
}
$stmt = $this->conn->prepare($sql);
$stmt->execute();
return $stmt->fetchColumn();
}
}
```
控制器层代码:
```php
class Controller {
private $model;
public function __construct() {
$this->model = new Model();
}
public function search() {
$keyword = isset($_GET['keyword']) ? $_GET['keyword'] : '';
$category = isset($_GET['category']) ? $_GET['category'] : '';
$sort = isset($_GET['sort']) ? $_GET['sort'] : '';
$page = isset($_GET['page']) ? $_GET['page'] : 1;
$limit = 10;
$start = ($page - 1) * $limit;
$total = $this->model->count($keyword, $category);
$pages = ceil($total / $limit);
$results = $this->model->search($keyword, $category, $sort, $start, $limit);
include 'view.php';
}
}
```
视图层代码:
```php
<!DOCTYPE html>
<html>
<head>
<title>Search Results</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form method="get" action="">
<input type="text" name="keyword" placeholder="Search...">
<select name="category">
<option value="">All</option>
<option value="category1">Category 1</option>
<option value="category2">Category 2</option>
<option value="category3">Category 3</option>
</select>
<select name="sort">
<option value="">Sort by</option>
<option value="field1 ASC">Field 1 ASC</option>
<option value="field1 DESC">Field 1 DESC</option>
<option value="field2 ASC">Field 2 ASC</option>
<option value="field2 DESC">Field 2 DESC</option>
</select>
<button type="submit">Search</button>
</form>
<?php if(!empty($results)): ?>
<?php foreach($results as $result): ?>
<div>
<h3><?php echo $result['title']; ?></h3>
<p><?php echo $result['description']; ?></p>
</div>
<?php endforeach; ?>
<?php else: ?>
<p>No results found.</p>
<?php endif; ?>
<?php if($pages > 1): ?>
<ul>
<?php for($i = 1; $i <= $pages; $i++): ?>
<li><a href="?keyword=<?php echo $keyword; ?>&category=<?php echo $category; ?>&sort=<?php echo $sort; ?>&page=<?php echo $i; ?>"><?php echo $i; ?></a></li>
<?php endfor; ?>
</ul>
<?php endif; ?>
</body>
</html>
```
这个示例代码可以完成一个简单的MVC模糊查询和多条件查询的分页显示,你可以根据自己的需求修改和扩展它。
阅读全文