如何使用选择和输入根据发布数据过滤结果?
Posted
技术标签:
【中文标题】如何使用选择和输入根据发布数据过滤结果?【英文标题】:How can I filter results depending on post data using selects and inputs? 【发布时间】:2015-11-19 09:18:27 【问题描述】:我有一个包含 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>";
【问题讨论】:
如果你创建一个过滤器,你只需要搜索选择/填写的字段.. 【参考方案1】:您应该检查查询中的空白值,如下所示:
$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);
【讨论】:
请再次检查。我已经解决了。 它有效。你能解释一下你做了什么吗? $conditions[ ] 是什么?谢谢。 @Anonymous 太好了。 :) 为了解释,我添加了if
条件来检查发布的值是否不是blank
。如果它不是blank
,那么只有你应该在查询中包含它。
但是 $conditions[ ] 是什么意思?我对 [ ] 有点困惑。
我在array
中添加了所有conditions
,然后将array
内爆以转换为string
,以便将其传递给查询。另一种方法是创建一个字符串,然后将其传递给字符串。以上是关于如何使用选择和输入根据发布数据过滤结果?的主要内容,如果未能解决你的问题,请参考以下文章