Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 按匹配百分比顺序将用户首选项与对象属性匹配_Php_Mysql_Algorithm_Matching - Fatal编程技术网

Php 按匹配百分比顺序将用户首选项与对象属性匹配

Php 按匹配百分比顺序将用户首选项与对象属性匹配,php,mysql,algorithm,matching,Php,Mysql,Algorithm,Matching,我试图计算项目与用户定义的首选项的匹配程度。下面是我的想法。但我并没有太多的经验,我想知道是否有更好的方法 以汽车为例。我们将把范围缩小到汽车(汽车、厢式货车等)的颜色和风格。 第一部分 用户以HTML形式选择以下内容: Color: ( )White, (*)Black, ( )Red Style: ( )Car, ( )Van, (*)Suv, (*)Truck 现在,如果我将上述内容转换为一个二进制数,其中第一个数字=第一个属性(白色),并继续 属性代码=0100011(黑色、suv、

我试图计算项目与用户定义的首选项的匹配程度。下面是我的想法。但我并没有太多的经验,我想知道是否有更好的方法

以汽车为例。我们将把范围缩小到汽车(汽车、厢式货车等)的颜色和风格。
第一部分

用户以HTML形式选择以下内容:

Color: ( )White, (*)Black, ( )Red
Style: ( )Car, ( )Van, (*)Suv, (*)Truck
现在,如果我将上述内容转换为一个二进制数,其中第一个数字=第一个属性(白色),并继续

属性代码=0100011(黑色、suv、卡车)
第二部分

现在使用MySQL

Select item_id, attribute_code FROM items

items table = [item_id][attribute_code]
接下来,使用PHP计算每个items属性代码与用户首选项的匹配程度

// Set users attribute code to var
$user_pref = $_POST['user_att_code'];

while($row=mysql_fetch_array($result))
{
    // Pull attribute_code from database and put into var
    $item_code = $row['attribute_code'];

    // Set counters
    $count_digit = 0;
    $count_match = 0;

    // Length of attribute code
    $length = 7;

    // Start calculating match
    while($count_digit <= $length)
    {
        // Does first digit of users code = 1?
        // Does first digit of items code = 1?
        if($user_pref{$count_digit} != 0 && $user_pref{$count_digit} == $item_code{$count_digit})
        {
            // Add a positive match point to counter
            $count_match++;
        }

        // Next digit in code
        $count_digit++;
    }

    if($count_match > 0)
    {
    // Make array of item_id and match amount
    $item_search [$row['item_id']] = $count_match;
    }   
}

// Sort array by most similar
arsort($item_search);
现在我知道这是可行的。然而,这似乎不是一个好办法性能最明智。假设超过150000项,属性代码长度约为200。一次搜索至少需要30000000次计算(基于上述)


也许还有别的办法吗?这有什么大不了的吗?

为了获得更好的性能,你应该重新组织你的系统

对颜色和样式使用单独的表格。也可以使用表来组织数据之间的关系(项-颜色、项-样式)

您将能够仅从数据库中选择用户选择的参数,而不是迭代每个请求的所有项

user: 0100011
it_1: 0100011 = 100% match
it_2: 0100100 = 50% match
it_3: 0011000 = 0% match
// If you notice the 50% does not make sense ignore it.
// I left something out for simplification.