php - 如何使用选择和输入根据发布数据过滤结果?

标签 php mysql sql wordpress

我有一个包含 3 个选择和 3 个输入以及提交按钮的表单。当我提交表单时,我试图通过所有这些输入来过滤搜索结果并选择 $_POST 值,但是当我没有完成所有输入并为每个选择选择一个选项时,我没有结果。我在这里做错了什么:

<div style='float: left; margin-right: 20px;'>
	<form action='' method="post">
		<select name='post_type'>
			<option selected="true" disabled="disabled">Alege post type</option>    
			<option <?php if(isset($_POST['post_type']) && $_POST['post_type'] == 'post') echo 'selected'; ?> value='post'>Post</option>
			<option <?php if(isset($_POST['post_type']) && $_POST['post_type'] == 'page') echo 'selected'; ?> value='page'>Page</option>
			<option <?php if(isset($_POST['post_type']) && $_POST['post_type'] == 'retete') echo 'selected'; ?> value='retete'>Retete</option>
		</select>
		<select name='post_status'>
			<option selected="true" disabled="disabled">Alege status</option>    
			<option <?php if(isset($_POST['post_status']) && $_POST['post_status'] == 'publish') echo 'selected'; ?> value='publish'>Publish</option>
			<option <?php if(isset($_POST['post_status']) && $_POST['post_status'] == 'trash') echo 'selected'; ?> value='trash'>Trash</option>
			<option <?php if(isset($_POST['post_status']) && $_POST['post_status'] == 'draft') echo 'selected'; ?> value='draft'>Draft</option>
		</select>
		<select name='meta_key'>
			<option selected="true" disabled="disabled">Alege meta_key</option>    
			<option <?php if(isset($_POST['meta_key']) && $_POST['meta_key'] == 'reteta_vizual') echo 'selected'; ?> value='reteta_vizual'>reteta_vizual</option>
			<option <?php if(isset($_POST['meta_key']) && $_POST['meta_key'] == 'ingrediente') echo 'selected'; ?> value='ingrediente'>ingrediente</option>
			<option <?php if(isset($_POST['meta_key']) && $_POST['meta_key'] == 'preparare') echo 'selected'; ?> value='preparare'>preparare</option>
		</select>
		<input type='text' name='date_begin' placeholder='Data inceput ("yyyy/mm/dd")' style='width: 200px;'>
		<input type='text' name='date_end' placeholder='Data sfarsit ("yyyy/mm/dd")' style='width: 200px;'>
		<input type='text' name='meta_value' placeholder='Meta value here'>
		<input type='submit' name='filter_results' value='Filtreaza rezultate'>
	</form>
</div>
	<?php
	//filter 
	echo "<table class='wp-list-table widefat fixed posts'>";
		echo "<thead>";
			echo "<tr>";
				echo "<th>ID</th>";
				echo "<th>Post title</th>";
				echo "<th>Author</th>";
			echo "</tr>";
		echo "</thead>";
	if(isset($_POST['filter_results'])){
		$tabel_1 = $wpdb->prefix . 'posts';
		$tabel_2 = $wpdb->prefix . 'postmeta';
		$post_status = $_POST['post_status'];
		$post_type = $_POST['post_type'];
		$meta_key = $_POST['meta_key'];
		$date_begin = $_POST['date_begin'];
		$date_end = $_POST['date_end'];
		var_dump($_POST);
		$records_posts = $wpdb->get_results("SELECT * FROM $tabel_1 AS posts 
											INNER JOIN $tabel_2 AS postmeta 
											ON posts.ID = postmeta.post_id 
											WHERE posts.post_status = '$post_status' 
											AND posts.post_type = '$post_type' 
											AND postmeta.meta_key = '$meta_key'
											AND posts.post_date BETWEEN '$date_begin' AND '$date_end'
											GROUP BY posts.ID");
		echo "<pre>";
		var_dump($records_posts);
		foreach($records_posts as $single_post){
			$author_id = $single_post->post_author;
			$author_name = get_the_author_meta('nicename', $author_id);
			echo "<tr>";
				echo "<td>" . $single_post->post_id . "</td>";
				echo "<td>" . $single_post->post_title . "</td>";
				echo "<td>" . $author_name . "</td>";
			echo "</tr>";
		}
	}
	echo "</table>";

最佳答案

您应该检查查询中的空白值,如下所示:

$all_conditions = '';

if($post_status != "")
    $conditions[] = "posts.post_status = '".$post_status."'";

if($post_type != "")
    $conditions[] = "posts.post_type = '".$post_type."'";

if($meta_key != "")
    $conditions[] = "postmeta.meta_key = '".$meta_key."'";

if($date_begin != "" && $date_end != "")
    $conditions[] = "( posts.post_date BETWEEN '".$date_begin."' AND '".$date_end."' )";

if(count($conditions)>0)
    $all_conditions = implode(" and ",$conditions);

if($all_conditions != "")
    $all_conditions = "where ".$all_conditions;

$sql = "SELECT * FROM $tabel_1 AS posts INNER JOIN $tabel_2 AS postmeta ON posts.ID = postmeta.post_id $all_conditions GROUP BY posts.ID"

$records_posts = $wpdb->get_results($sql);

关于php - 如何使用选择和输入根据发布数据过滤结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33799630/

相关文章:

php - 获取 mySQL 数据库列中所有值的 sum()

php - 无服务器框架,处理程序不存在

c# - 如何使用 C# 应用程序从 mysql 服务器导出 csv 文件并获取导出的 csv 文件顶部的所有列名称

php - 凹凸系统(显示最近的主题并在顶部回复)

php - 在 PHP 中将 stdClass 对象转换为数组

php - 在fpdf中显示具有存储在数据库中的样式的html标签

php - 获取时检查行是否存在的最快方法是什么?

php - 使用 AJAX、JSON 从数据库实时搜索结果

sql - 在SQL中减去前一行减去当前行

php - 在 MySQL 的 X 或 Y 列中返回不同的值