【问题标题】:Simple User Matching Logic in PHP [closed]PHP中的简单用户匹配逻辑[关闭]
【发布时间】:2015-08-11 13:25:54
【问题描述】:

我有一个 html 页面,其中有 1 个用于用户名的文本框字段和 5 个用于该用户的复选框,以指示他们喜欢的音乐类型(环境、摇滚、说唱、爵士、流行)。我将表单字段重复了两次,以便获得以下数据值:

汤姆
环境
摇滚
说唱


摇滚
说唱
爵士乐

贝蒂
摇滚
说唱
爵士乐

在 PHP 中执行用户匹配算法的最佳方法是什么?从关于 Sue 和 Betty 的值来看,将匹配 100%,而 Tom 将与其他两个匹配不到 100%。

有什么建议吗?

下面是代码

<p>Put in your name and pick the music genres you like:</p>
<form id="form1" name="form1" method="post" action="">
  <table width="100%" border="0" cellspacing="0" cellpadding="0">
    <tr>
      <td width="12%"><label for="name"></label>
      <input type="text" name="name1" id="name1" vale="name1" placeholder="name 1"/></td>
      <td width="12%"><label for="name"></label>
        <input type="text" name="name2" id="name2" value="name2" placeholder="name 2"/></td>
      <td width="13%"><label for="name3"></label>
        <input type="text" name="name3" id="name3" value="name3" placeholder="name 3"/></td>
    </tr>
    <tr>
      <td><input type="checkbox" name="checkbox" id="checkbox" />
      <label for="checkbox">Ambient</label></td>
      <td><input type="checkbox" name="checkbox" id="checkbox" />
        <label for="checkbox">Ambient</label></td>
      <td><input type="checkbox" name="checkbox" id="checkbox" />
        <label for="checkbox">Ambient</label></td>
    </tr>
    <tr>
      <td><input type="checkbox" name="Rock" id="Rock" value="Rock"/>Rock</td>
      <td><input type="checkbox" name="Rock" id="Rock" value="Rock"/>
        Rock</td>
      <td><input type="checkbox" name="Rock" id="Rock" value="Rock"/>
        Rock</td>
    </tr>
    <tr>
      <td><input type="checkbox" name="Rap" id="Rap" value="Rap"/>Rap</td>
      <td><input type="checkbox" name="Rap" id="Rap" value="Rap"/>
        Rap</td>
      <td><input type="checkbox" name="Rap" id="Rap" value="Rap"/>
        Rap</td>
    </tr>
    <tr>
      <td><input type="checkbox" name="Jazz" id="JAzz" value="Jazz" />Jazz</td>
      <td><input type="checkbox" name="Jazz" id="JAzz" value="Jazz" />
        Jazz</td>
      <td><input type="checkbox" name="Jazz" id="JAzz" value="Jazz" />
        Jazz</td>
    </tr>
    <tr>
      <td><input type="checkbox" name="Pop" id="Pop" value="Pop" />Pop</td>
      <td><input type="checkbox" name="Pop" id="Pop" value="Pop" />
        Pop</td>
      <td><input type="checkbox" name="Pop" id="Pop" value="Pop" />
        Pop</td>
    </tr>
  </table>

  <br />
  <input type="submit" name="button" id="button" value="Submit" />
</form>

【问题讨论】:

  • 您能更详细地解释一下“用户匹配算法”是什么意思吗?另外,不要列出您的项目,而是尝试添加您的 HTML 代码,否则 Stackoverflow 将删除您的原始帖子。
  • 你应该自己做作业。
  • 感谢@CodeGodie 的提示。如果它让人们感到困惑,我将其更改为标题中的逻辑。

标签: php html forms logic matching


【解决方案1】:

如果你有数组的选项,你可以用 array_diff 函数做一些事情。即

<?php
$tom = array("Ambient", "Rock", "Rap");
$sue = array("Rock", "Rap", "Jazz");
$betty = array("Rock", "Rap", "Jazz");

//compare tom to sue
$total = count($tom);
$diff = count(array_diff($tom, $betty));
$percent = 100 - ($diff / $total * 100);

echo round($percent, 2)."% match between Tom and Sue";

【讨论】:

    【解决方案2】:

    使用关联数组将流派列表添加到每个用户。然后循环比较流派:

    $user_genre['Tom']=array('Ambient', 'Rock', 'Rap');
    $user_genre['Sue']=array('Rock', 'Rap', 'Jazz');
    $user_genre['Betty'] =array('Rap', 'Rock', 'Jazz');
    $match=0;
    foreach ($user_genre['Tom'] as $tom_genre){
        foreach ($user_genre['Sue'] as $sue_genre){
            if ($tom_genre == $sue_genre){
                $match++;
            }
        }
    }
    

    不是一个好的代码,只是你应该思考的方式。

    【讨论】:

      【解决方案3】:

      假设您将用户的选择存储到可以使用array_intersect的数组中

      这是一个例子:

      <?php
      $array1 = array("a" => "green", "red", "blue");
      $array2 = array("b" => "green", "yellow", "red");
      $result = array_intersect($array1, $array2);
      print_r($result);
      ?>
      

      输出:#

      Array 
      (
         [a] => green
         [0] => red 
      )
      

      这里是 php manual 与所有数组函数。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-10-31
        • 1970-01-01
        • 1970-01-01
        • 2010-10-10
        • 2012-04-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多