gpt4 book ai didi

带范围过滤器的 PHP 搜索查询

转载 作者:行者123 更新时间:2023-11-30 00:08:58 25 4
gpt4 key购买 nike

我正在尝试进行搜索查询,但不确定如何将所有内容组合在一起。我的范围过滤器部分有问题。

我想要实现的目标:一个搜索表单1.) 如果字段 A,B(不为空)则放入搜索查询

2.) 通过(价格较低范围、价格较高范围)搜索价格列如果匹配字段 A、B(如果不为空)和价格(如果在范围内),则包含结果。(如果搜索字段 A、B 为空,则显示范围内存在的所有结果)。

感谢您的宝贵时间。

我现在拥有的代码。

    <?php
ini_set('display_errors', 1); error_reporting(E_ALL);
session_start();
include 'connect.php';
if($_POST)
{
$A = ($_POST['A']);
$B = ($_POST['B']);
$C = ($_POST['C']);
$pricelow = ($_POST['pricelow']);
$pricehigh = ($_POST['pricehigh']);
$sql = array();
if (!empty($A)) {
$sql[] = "A='$A'";
}
if (!empty($B)) {
$sql[] = "B='$B'";
}
if (!empty($C)) {
$sql[] = "C='$C'";
}
if (!empty($price)) {
for($i = pricelow; $i<pricehigh; $i++){
$price = $i;
}
$sql[] = "price='$price'";
$sql = implode(' AND ', $sql);
$sql = "SELECT * FROM Listing" . (!empty($sql)? " WHERE " . $sql: '');
$result = mysqli_query($con,$sql);
$output = array();
// fetch your results
while( $row = mysqli_fetch_assoc($result) )
{
// add result row to your output's next index
$output[] = $row;
}
// echo the json encoded object
echo json_encode( $output );
}
?>

编辑:

$sql = "SELECT * FROM Listing" . (!empty($sql)? " WHERE " . $sql: '') . ("AND" 'price' BETWEEN  "$pricelow" AND "$pricehigh");

编辑:

if (!empty($pricelow) || !empty($pricehigh)) {


$sql[] = $pricehigh>= 'price' and 'price'>=$pricelow ;

}
$sql = array();


$sql = implode(' AND ', $sql);
$sql = "SELECT * FROM Listing" . (!empty($sql)? " WHERE " . implode(" AND ", $sql): '');

最佳答案

<?php

ini_set('display_errors', 1); error_reporting(E_ALL);


session_start();

include 'connect.php';

if($_POST)
{

$A = ($_POST['A']);
$B = ($_POST['B']);
$C = ($_POST['C']);
$pricelow = ($_POST['pricelow']);
$pricehigh = ($_POST['pricehigh']);

$query = "SELECT * FROM Listing WHERE ";
$flag = 0;

if (!empty($A)) {
$query .= $flag==0?" A='$A' ":" AND A = '$A'";
$flag = 1;
}
if (!empty($B)) {
$query .= $flag==0?" B = '$B' ":" AND B = '$B'";
$flag = 1;
}
if (!empty($C)) {
$query .= $flag==0?" C='$C' ":" AND C= '$C'";
$flag = 1;
}

if ($flag == 0) {
$query .= " price > $pricelow AND price > $pricehigh"
}


$result = mysqli_query($con,$query);

$output = array();

// fetch your results
while( $row = mysqli_fetch_assoc($result) )
{
// add result row to your output's next index
$output[] = $row;
}

//your rest of the code
>?

我无法测试,所以请尝试报告结果!

关于带范围过滤器的 PHP 搜索查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24245692/

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