2016-01-26 74 views
0

我已经编写了一个脚本,用于检查通过textarea接收到的名称的批量输入,并省略已经在数据库中的所有值。如果你只输入一个重复的名字,它会起作用。如果输入两个或更多,它将过滤出第一个重复名称,将其余的名称视为唯一名称并将其插入数据库。我无法弄清楚为什么。首先,这是一个内置于脚本另一部分的数组。它是从数据库查询生成的:PHP的foreach循环弄乱了我的in_array函数

Array 
(
    [0] => john 
    [1] => peter 
    [2] => max 
    [3] => jake 
) 

此数组被称为$ onlyHandles。然后这是脚本:

if((isset($_POST['extract']) && !empty($_POST['extract']))){ 
    $handles = trim($_POST['extract']); 
    $handles = explode("\n", $handles); 

     if(count($handles)>200){ 
      echo 'error'; 
      exit(1); 
     } 

     foreach($handles as $handle) { 
      $handleRep = strtolower(str_replace('@','',$handle)); 
      $handleClean = str_replace(str_split('\\/:*?&"<>=+-#%$|'), ' ', $handleRep, $count); 

       if ($count > 0) { 
        echo 'error'; 
        exit(1); 
       } 
       else{ 

        if (in_array($handleClean, $onlyHandles)){ 
         $delmessage .= "<p>".$handleClean." is already in your list.</p>"; 
        } 
        else{ 
         $sqlIns = "INSERT INTO...blah blah blah)"; 
         $resultIns = mysql_query($sqlIns); 
         $resInsArr[] = array($resultIns); 

        } 
       } 
     } 
     $countresIns = count($resInsArr); 
      if ($countresIns > 0){ 
       $delmessage .= "<p>User(s) added to list succesfully!</p>" ; 
      } 
} 

现在,如果在textarea中输入“john”,它会喊出名称已经存在。如果输入“john”和“max”,它将省略john并添加最大值。

任何帮助将不胜感激。

P.S.关于查询格式,我知道,我知道,谢谢!

+0

您好!我认为你必须修改你的'$ handle'变量,因为它可能在它周围有一些空格... 然后关于你的问题,我不理解它。你的代码看起来是“干净的”,也许你应该考虑将strict标志设置为true [见函数定义](https://secure.php.net/in_array)。 – shulard

+2

当然,您可以使用数组交集函数和多插入来实现更高效的过程。它将允许节省处理时间并简化此代码。 – shulard

+2

看一看[array_diff](https://secure.php.net/manual/en/function.array-diff.php)! – shulard

回答

0

我想要给你一些想法如何实现它:

  1. 替换第一行:

    IF((isset($ _ POST [ '提取'])& &!空($ _ POST [ '摘录']))){

通过

if((!empty($_POST['extract']))){ 

因为!empty已经guivesÜ它isset

担保
  • 我暂停播放一些特殊字符
  • ü还可以使用正则表达式的权力r E放置不需要的字符 在替换:

    第12行:$handleClean = str_replace(str_split('\\/:*?&"<>=+-#%$|'), ' ', $handleRep, $count);

    通过:

    $handleClean = preg_replace("/\[\/:\*?&\"<>=\+-#%\$\|\]*/", ' ', $handleRep, $count);

  • 在乌尔for循环,什么有关重构的以下几行:
  • 第2行:$handles = trim($_POST['extract']);

    通过(修剪是没有必要的票数)

    $handles = $_POST['extract'];

    AND

    线11:$handleRep = strtolower(str_replace('@','',$handle));

    通过

    $handleRep = trim(strtolower(str_replace('@','',$handle)));

    嘿;-),

    ü还应该添加一些的print_r(......)调试的每一步

    0

    感谢@Ulrich Tevi荷鲁斯,使我的代码有点清洁,但并没有解决神秘消失用户。 @shulard,你应该发布这个作为获得upvote的答案。 array_diff确实是最好的解决方案。 这是最终的代码。需要整理一些内容,但这足以让我的服务器进行测试。

    //this is the current contents of the list: 
    $onlyHandles = array(); 
    foreach ($organizedArray as $key2 => $val2) { 
        $onlyHandles[] = $val2['name']; 
    } 
    echo "<br>begin onlyhandles<br>"; 
    print_r($onlyHandles); 
    echo "<br>end onlyhandles<br>"; 
    //finish preparation for display  
    
    //if names submitted for the list list 
    if(!empty($_POST['extract'])){ 
        $handles = trim($_POST['extract']); 
        $handles = explode("\n", $handles); //this is now an array 
        echo "<br>begin handles<br>"; 
        print_r($handles); 
        echo "<br>end handles<br>"; 
        //$countInput = count($handles); 
    
         if($countInput>200){ 
          echo '<p style="color:red;">Please Enter fewer than 200 names!</p>'; 
          exit(1); 
         } 
         else{ 
    
          $handleFinal = array(); 
           foreach($handles as $handle) { 
            //$handleRep = strtolower(str_replace('@','',$handle)); 
            $handleRep = trim(strtolower(str_replace('@','',$handle))); 
            $handleClean = str_replace(str_split('\\/:*?&"<>=+ -#%$|'), 'p', $handleRep, $count); 
            //$handleClean = preg_replace("/\[\/:\*?&\"<>=\+-#%\$\|\s+\]*/", ' ', $handleRep, $count); 
            echo "handleClean: ".$handleClean."<br>"; 
    
             if ($count > 0) { 
              echo '<p style="color:red;">Your input contained special characters.</p>'; 
              exit(1); 
             } 
             else{ 
               $handleFinal[] = $handleClean; 
             } 
           }//end foreach 
    
         }//finish checking count input number 
    
        echo "<br>begin handleFinal<br>"; 
        print_r($handleFinal); 
        echo "<br>end handleFinal<br>"; 
    
        $countFinal = count($handleFinal); 
        echo "<br>countfinal is ".$countFinal."<br>"; 
        //check if this user is already in the list 
        $handleDiffs = array_diff($handleFinal,$onlyHandles); 
    
        echo "<br>begin handlediffs<br>"; 
        print_r($handleDiffs); 
        echo "<br>end handlediffs<br>"; 
    
         foreach($handleDiffs as $handleDiff) { 
          $sqlIns = "blah blah blah"; 
          $resultIns = mysql_query($sqlIns); 
          $resInsArr[] = array($resultIns); 
    
         } 
    
        $countresIns = count($resInsArr); 
         if ($countresIns > 0){ 
          $delmessage .= "<p>User(s) added to the list succesfully!</p>" ; 
         } 
    } 
    
    0

    我后我comment answer作为一个真正的答案:)

    你必须得修剪$handle变量,因为它可能有它周围的一些空间......

    然后你的问题,我不明白它。你的代码似乎“干净”,也许你应该考虑将strict标志设置为true请参阅function definition here