gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 03:28:01 25 4
gpt4 key购买 nike

我有一个包含 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com