使用数组

时间:2015-08-01 04:47:27

标签: php arrays mysqli

我正在学习编码,但我想为当地青少年营制作一个互动名册,在登记新营员的个人信息(name | birth_year | hobbies | favorite_subject | spirit_animal_explanation | contact_information)后,露营者可以选择一个"基我"按钮,显示其他拥有类似爱好和精神动物的露营者的个人资料以及属于同一年龄组(11-13岁,14-16岁等)

为了实现这一点,当露营者输入他/她的信息时,它首先被保存到数据库中。然后,我查询SELECT语法,该语法从“爱好”中获取信息。和' spirit_animal_explanation'露营者的列和过滤掉不需要的单词并将它们放在一个数组中(因此,如果露营者说"我喜欢跑步和跳跃"过滤器将只有阵列(print_r Array([0]=>running[1]=>jumping)。

然后我array_merge将两个已过滤的数组合并为一个,这样我就可以轻松管理搜索(有些营员将业余爱好混合到spirit_animal_explanation中,因此为两列提供了一个参考数组搜索可以更容易)给我留下一个包含两个合并数组的$compiled变量。

现在正在努力解决的部分:我可以为搜索准备参考数组,但我不知道查询语法以开始搜索或扫描使用引用数组的每个用户的hobbiesspirit_animal_explanation列。根据之前的建议,我已经阅读了JOIN查询,但是我没有看到制作两个表的重点,因为参考数组的元素对于每个执行搜索的露营者来说都不是固定/常量的。此外,我不知道我应该如何/何时添加年龄条件。最后,改进延迟我希望系统在第一个搜索会话中查找前10个匹配用户,然后在第二个搜索会话中查找接下来的10个用户,等等。我该怎样才能这样做?

编辑:使用REGEXP,内爆工作正常,但由于某些原因我没有得到结果。这是我的代码:

<?php
    error_reporting(E_ALL ^ E_NOTICE);
    ini_set('display_errors',1);
    ini_set('display_startup_errors',1);

    if(isset($_GET['group-me'])) {

    // run query, get $result
    $query = "SELECT `hobbies`, `spirit_animal_explanation` FROM `users`";
    $result = $con->query($query);

    if ($result) {
      while ($row = $result->fetch_assoc()) {
    // this contains an associative array with the keys, 'hobbies'and 'spirit_animal_explanation'
           $compile = $row;
        }

    $tags1 = explode(" ",strtolower(preg_replace("/[^a-zA-Z]+/", " ", $compile['hobbies'])));
    $tags2 = explode(" ",strtolower(preg_replace("/[^a-zA-Z]+/", " ", $compile['spirit_animal_explanation'])));

//example of word filtering
    $black_list_1 = array_diff($tags1, array('i','a','am','do','really','the','then');
    $black_list_2 = array_diff($tags2, array('i','them','is','at','an','what','because');

//further cleaning
    $clean_1 = array_filter($black_list_1);
    $clean_2 = array_filter($black_list_2);

//combining both arrays into a single array
    $compressed = array_merge($clean_1, $clean_2);

//alter array to be used against string values
    $keywords = implode("|", $compressed);

        $result->close();
    }
    $searchCD = "SELECT * FROM users WHERE field REGEXP '".$keywords."'";
    $searchST = $con->query($searchCD);

    if($searchST) {
      while ($record = $searchST->fetch_assoc()) {

       echo $record['first_name'] . "<br/>" . $record['hobbies'] . "<br/>" . $record['spirit_animal_explanation'] . "<br/>" . $record['favorite_subject'] . "<br/>" . $record['contact_information'];

      }
}

}

   ?>

2 个答案:

答案 0 :(得分:0)

如果没有看到桌面结构,很难知道,但是你所寻找的可能就是&#39;喜欢&#39; mysql中的子句。

http://www.tutorialspoint.com/mysql/mysql-like-clause.htm

例如,如果业余爱好是“跳跃”。你可能在寻找:

SELECT * FROM sometable where hobby like "%jumping%";

这将匹配记录,其中爱好列包含单词&#34; jump&#34;在它的任何地方。即&#34;跳跃和跑步&#34;和&#34;喝酒和跳跃&#34;两者都匹配。

答案 1 :(得分:0)

REGEXP可能对您更有效,例如

{% for host in salt['publish.publish']('roles:backend', 'network.ip_addrs', 'eth0', 'grain') %}
  server {{ host.ip }}; # {{ host.fqdn }}
{% endfor %}

这将输出如下内容:

$array = array('keyword1', 'keyword2', 'keyword3', 'keyword4');
$keywords = implode("|", $array);

"SELECT * FROM tbl_name WHERE field REGEXP '".$keywords."'";

希望这对你有所帮助。

相关问题