gpt4 book ai didi

php - 动态分组查询的 AND 和 OR 部分

转载 作者:行者123 更新时间:2023-12-01 00:42:15 28 4
gpt4 key购买 nike

所以我有这个:

$query = "SELECT personaname, profileurl, avatarmedium, location, country, rank, rankurl, role, map, skype, teamspeak, microphone, esea, faceit, cevo, competitive FROM csgolistings WHERE active = 1";

if(isset($_POST['location']) && $_POST['location'] != "") {
$location = $_POST['location'];
$query .= " AND location = '$location'";
}

if(isset($_POST['country']) && $_POST['country'] != "") {
$country = $_POST['country'];
$query .= " AND country = '$country'";
}

if(isset($_POST['rank']) && $_POST['rank'] != "") {
$location = $_POST['rank'];
$query .= " AND rank = '$rank'";
}

if(isset($_POST['competitive']) && $_POST['competitive'] != "") {
$query .= " AND competitive = 1";
}

if(isset($_POST['cevo']) && $_POST['cevo'] != "") {
$query .= " AND cevo = 1";
}

if(isset($_POST['faceit']) && $_POST['faceit'] != "") {
$query .= " AND faceit = 1";
}

if(isset($_POST['esea']) && $_POST['esea'] != "") {
$query .= " AND esea = 1";
}

if(isset($_POST['microphone']) && $_POST['microphone'] != "") {
$query .= " AND microphone = 1";
}

if(isset($_POST['teamspeak']) && $_POST['teamspeak'] != "") {
$query .= " AND teamspeak = 1";
}

if(isset($_POST['skype']) && $_POST['skype'] != "") {
$query .= " AND skype = 1";
}

if(isset($_POST['role']) && $_POST['role'] != "") {
$role = $_POST['role'];
$query .= " AND role = '$role'";
}

if(isset($_POST['map']) && $_POST['map'] != "") {
$map = $_POST['map'];
$query .= " AND map = '$map'";
}

所以我最终得到的是 $query 是这样的(取决于发布的搜索过滤器:

SELECT personaname, profileurl, avatarmedium, location, country, rank, rankurl, role, 
map, skype, teamspeak, microphone, esea, faceit, cevo, competitive FROM csgolistings WHERE active = 1 AND rank = '$rank' AND cevo = 1

我的问题是,我将如何让它将位置和国家/地区分组为 AND,然后将其余部分作为 OR?我需要在 OR 周围放置方括号,以便它找到所有来自位置和国家 AND (cevo = 1, rank = '$rank') 的人。我可以在平面查询中轻松地做到这一点,但我正在处理 $_POST 数据,其中一些数据可能不会发布。这取决于用户的要求。

希望这是有道理的。任何帮助是极大的赞赏。谢谢!!

最佳答案

你可以试试这个:

<?php
$required = array ('location', 'country');
$optional = array ('microphone', 'faceit', ... );

$ands = array( '1' => '1');

foreach($required as $req)
{
if(isset($_POST[$req]) && $_POST[$req] != '')
{
$ands[$req] = "$req = '{$_POST[$req]}'";
}
}

$ors = array();

foreach($optional as $req)
{
if(isset($_POST[$req]) && $_POST[$req] != '')
{
$ors[$req] = "$req = '{$_POST[$req]}'";
}
}

$and_part = implode(" AND ", $ands);
$or_part = empty($ors) ? "1" : implode(" OR ", $ors);

$query = "select .... from ... WHERE $and_part AND ($or_part) ... ";
?>

然后,您可以通过将参数添加到顶部声明的适当数组来轻松扩展查询,而无需添加到巨大的 if 链。

给定此 POST 输入:

   $_POST = array(
'location' => 'city',
'country' => 'antarctica',
'microphone' => 'loud',
'faceit' => 'yes'
);

$query 将包含 select .... from ... WHERE 1 AND location = 'city' AND country = 'antarctica' AND (microphone = 'loud' OR faceit = '是') ...

关于php - 动态分组查询的 AND 和 OR 部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29555135/

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