PHP:foreach循环结果打印manay时间

时间:2016-03-27 09:16:23

标签: php foreach

我有一个名为singles.txt的txt文件。每行包含有关用户的信息(名称,性别...用逗号分隔)从输入的名称keyborad我要检查此用户是否存在 我跟进:跟一个对象当前用户然后将它与txt文件中的所有对象进行比较。这是myfunction.php文件

<?php
  class User
  {
    public $name;
    public $gender;
    public $age;
    public $personality;
    public $FAVos;
    public $min;
    public $max;
  }
  function countLine() {
      $linecount = 0;
      $file = fopen("singles.txt", "r");
  while(!feof($file)){
    $line = fgets($file);
    $linecount++;
    }
    fclose($file);
    return $linecount;
  }
  function makeCurrentUser($n) {
    $file = fopen("singles.txt", "r");
    while(!feof($file)){
      $line = fgets($file);
      $word = explode(",", $line);
      if ($word[0] == $n) {
        break;  
        fclose($file);
        }
    }
    $currUser = new User();
    $currUser-> name=$word[0];
    $currUser-> gender=$word[1];
    $currUser-> age=$word[2];
    $currUser-> personality=$word[3];
    $currUser-> FAVos=$word[4];
    $currUser-> min=$word[5];
    $currUser-> max=$word[6];
    return $currUser;
    }
    function getAllUser() {
      $file = fopen("singles.txt", "r");
      $collectionUser = array();
      $linecount= countLine();
      while(!feof($file)){
          $line = fgets($file);
          $word = explode(",", $line);
          for ($i=0; $i<= $linecount; $i++) {
             $nameUsr = $i.' user';
             $nameUsr= new User();
             $nameUsr-> name=$word[0];
             $nameUsr-> gender=$word[1];
             $nameUsr-> age=$word[2];
             $nameUsr-> personality=$word[3];
             $nameUsr-> FAVos=$word[4];
             $nameUsr-> min=$word[5];
             $nameUsr-> max=$word[6];
      array_push($collectionUser,$nameUsr);
        }   
     }
     return $collectionUser; 
    }

?>

在anothorr文件中(从keyborad处理数据“name”),我调用我的函数并进行比较

<?
    $name = $_GET['name'];
        $currUser = makeCurrentUser($name);
        $collectionUser= getAllUser();
        foreach ($collectionUser as $user) {
            $common = similar_text($currUser->personality, $user->personality);
            if($currUser->gender!=$user->gender && $currUser->min <= $user->age && $currUser->max >= $user->age
                && $currUser->FAVos== $user->FAVos && $common>=2) {
            echo '<div class="match">';
                echo '<img src="files/user.jpg">';
                echo '<span>';
                echo '<ul>';
                echo '<li>'.$user->name.'</li>';
                echo '<li>'.$user->gender.'</li>';
                echo '<li>'.$user->age.'</li>'; 
                echo '<li>'.$user->personality.'</li>'; 
                echo '<li>'.$user->FAVos.'</li>';   
                echo '</span>';
                echo '</div>';
            }
?>

我找到了结果。它可以,但每次打印多次。你能帮我解决我做错的事吗

0 个答案:

没有答案