用于在varchar中搜索多个单词的php代码

时间:2016-11-28 10:36:25

标签: php mysql arrays search varchar

从我的应用程序中,我发送了一个用户列表,我想搜索他们订阅的群组:

$selectedUsers = ["John", "Carlos", "Anna", "Julia"]

我的数据库中有许多不同的用户组,其中包含许多不同的用户 他们每个人:

$football = ["**John**" ,"**Carlos**" ,"Daniel" ,"Rob" ,"Frank" ,"Bob"] 

$cooking = ["**John**" , "**Anna**" , "**Julia**" , "Claudia" , "Rob" , "Adriana"]

$startups = ["**John**", "**Carlos**", "**Anna**", "**Julia**", "Rob", "Adriana"]

我想要的输出是具有所选用户数量的组的排序列表:

$returnArray[0] = $startups //4 users inside group
$returnArray[1] = $cooking //3 users inside group
$returnArray[2] = $football //2 users inside group

这是我到目前为止的代码,但我使用的循环基于我已经存储的group_id,我想改变它:

<?php
    //fetch groups with users

    $returnValue = array();
    $groupUsersNumber = 0;

    $selectedUsers = htmlentities($_REQUEST["selectedUsers"]);
    $lastGroupID = htmlentities($_REQUEST["lastGroupID"]); //remove

    if($lastGroupID == ""){
        $lastGroupID = getLastGroupID(); 
        $lastGroupID = $lastGroupID + 1;
    }

    if($selectedUsers == ""){
        //return all groups ordered by ID desc

        $group = getGroupWithID($lastGroupID); 

    } else{

        $usersArray = explode(', ', $selectedUsers);
        $foundGroup = false;
        while($foundGroup == false){

            $group = getGroupWithID($lastGroupID);

            $fetchedGroupUsers = explode(', ', $group["users"]);
            for($i = 0; $i < count($usersArray); $i++){ 
                if(in_array($usersArray[$i], $fetchedGroupUsers)){
                    $foundGroup = true;
                    break;
                } else{
                    $lastGroupID = $group["group_id"];
                }
            }
        }
    }

    for($i = 0; $i < count($usersArray); $i++){
        if(in_array($usersArray[$i], $fetchedGroupUsers)){
            $groupUsersNumber = $groupUsersNumber + 1;
        }
    }

    if(empty($group))
    {
        $returnValue["status"]="403";
        $returnValue["message"]="No more groups with that users.";
        echo json_encode($returnValue);
        return;
    } else{

        $returnValue=$group;
        $returnValue["groupUsersNumber"]=$groupUsersNumber;
    }

    echo json_encode($returnValue);
?>

有没有其他方法可以更好/更有效地搜索我的数据库?理解!

2 个答案:

答案 0 :(得分:1)

您的数据库似乎未规范化。规范化数据库可能是更有效的方式。不要将用户存储在描述varchar中。而是建立多对多的关系。

除此之外,PHP的FilterIterator类适合您。它是可重用的,并且在迭代数组时效率更高。

这是一个简短的例子。

class NameFilterIterator extends FilterIterator {

    protected $filter = null;

    public function __construct(Iterator $iterator, $filter) {
        parent::__construct($iterator);
        $this->filter = $filter;
    }

    public function accept() {
        $current = $this->getInnerIterator()->current();

        if (strpos($current, $this->filter) !== false) {
            return true;
        }

        return false;
    }
}

// Usage
$aUsers = [ 'John', 'Carlos', 'Anna', 'Julia' ];
$oFootball = new ArrayIterator(["**John**" ,"**Carlos**" ,"Daniel" ,"Rob" ,"Frank" ,"Bob"]);

foreach ($aUsers as $sUser) {
    $oFilter = new NameFilterIterator($oFootball, $sUser);
    foreach ($oFilter as $sName) {
        var_dump($sName); // outputs: John, Carlos
    }
}

FilterIterator对象的内部内存使用效率更高。

答案 1 :(得分:1)

你似乎在混淆php和mysql,最好重新设计你的数据库。

然而,作为一个基本想法,你可以在MySQL中大致做你想要的。这不好,而且效率不高,但是这样的事情: -

given = raw_input()
n= list(map(int,given.split()))

A = n[0]
B = n[1]
if B % 2 == 0:
    print (B - 2)
else:
    print (B -1)