Пример #1
0
function glob_makeWhereCondition($ar)
{
    $res = "";
    foreach ($ar as $key => $val) {
        $tmp_ar = glob_makeArFromString($val);
        $tmp = "";
        $numstart = 2;
        $ss = 0;
        foreach ($tmp_ar as $key2 => $val2) {
            //    if ($ss==0) {$ss++; $numstart = (strlen($val2)>MIN_OR_STR_LEN?2:3); }
            $tmp_ar[$key2] = "'%" . trim($val2) . "%'";
            //    $tmp .= ' '.(strlen($val2)>MIN_OR_STR_LEN?'OR':'AND').' '.$key.' LIKE '.$tmp_ar[$key2];
        }
        $tmp = implode(' OR ' . $key . ' LIKE ', $tmp_ar);
        $res .= '(' . $key . ' LIKE ' . $tmp . ') AND ';
        //$tmp = substr($tmp,3,strlen($tmp));
        //$res .='('.$tmp.') AND ';
    }
    if ($res !== '') {
        $res = substr($res, 0, strlen($res) - 5);
    }
    //    echo $res; exit;
    return $res;
}
Пример #2
0
 private function relevantSort($ar, $search)
 {
     $s_search = glob_removePunctuations($search);
     $ar_search = glob_makeArFromString($s_search);
     $cnt_s = count($ar_search);
     $count_find = 0;
     $ar1 = array();
     $ar2 = array();
     $res_ar = array();
     while ($rows = $ar->fetch()) {
         $koef = 1;
         $s = $rows['name'];
         //  $s = 'Проверялочка: Математика 3 класс';
         //   $s = 'Математика. 5 класс. Методическое пособие к учебнику Г.К. Муравина, О.В. Муравиной "Математика. 5 класс". В 2-х частях. Часть 1. Вертикаль';
         $tmp_name = glob_removePunctuations($s);
         $tmp_ar = glob_makeArFromString($tmp_name);
         $cnt = count($tmp_ar);
         if ($s_search == $tmp_name) {
             $koef = 1000;
         } else {
             if (preg_match("/(^|\\s){$s_search}(\\s|\$)/i", $tmp_name)) {
                 if (strpos($tmp_name, $s_search) == 0) {
                     $koef = 500 + 1 / $cnt;
                 } else {
                     $koef = 200 + 1 / $cnt;
                 }
             } else {
                 $ar_repeat = array();
                 $schet = 0;
                 foreach ($ar_search as $val2) {
                     if ($c = preg_match_all("/(^|\\s){$val2}(\\s|\$)/i", $tmp_name, $matches)) {
                         if (isset($ar_repeat[$val2])) {
                             $cnt_schet = $ar_repeat[$val2]['cnt'];
                             if ($cnt_schet < $c) {
                                 $schet++;
                             }
                         } else {
                             $ar_repeat[$val2] = array('cnt' => 1);
                             $schet++;
                         }
                     }
                 }
                 $koef = $schet + 1 / $cnt;
                 //      echo strlen($s_search)."<br>";
                 //      echo $tmp_name."<br>";
             }
         }
         //     echo $koef; exit;
         $count_find = count($ar1);
         if ($count_find < SEARCH_PROD_NUM) {
             $ar1[$rows['id']] = $koef;
             $ar2[$rows['id']] = $rows;
         } else {
             arsort($ar1, SORT_NUMERIC);
             end($ar1);
             $cur = each($ar1);
             if ($koef > $cur['value']) {
                 unset($ar2[$cur['key']]);
                 array_pop($ar1);
                 //удаляем последние элементы
                 $ar1[$rows['id']] = $koef;
                 $ar2[$rows['id']] = $rows;
             }
         }
         $ar1[$rows['id']] = $koef;
         $ar2[$rows['id']] = $rows;
     }
     arsort($ar1, SORT_NUMERIC);
     $num = 0;
     foreach ($ar1 as $key => $val) {
         $num++;
         $res_ar[] = $ar2[$key];
     }
     return $res_ar;
 }