2013-09-22 74 views
0

我有一个称为mystique项目的应用程序,用户必须猜测水印后面的项目。水印不时显露出来,一切工作都很完美,但我有一个猜测单词的“小”问题。我在数组中添加了用逗号隔开的单词,并且我在php中展开了该数组,但由于某种原因,它只捕获第一个单词正确,其他所有内容都不正确。这是我所做的。数组陷入空间和爆炸 - 空间

$gt = getVal('pics','gtext','online',1); 
$won = getVal('pics','winner','online',1); 

if($won=='no') 
{ 
    $counts = getGen(3); 
    $counts2 = getGen(4); 

    if($counts2==0) 
    { 
     $counts2 = 9999999999999; 
    } 

    $ccount = getCount2("$uid","$pid",date('Y-m-d H:i:s',$t1),date('Y-m-d H:i:s',$t2)); 
    $ccount3 = getCount3("$uid","$pid"); 

    if($ccount>=$counts || $ccount3>=$counts2) 
    { 
     echo '4'; 
    } 
    else 
    { 
     $sp = explode(",",$gt); 

     if(in_array($val, $sp)) // guess correct 
     { 
      echo '1'; 
     } 
     else// guess wrong 
     { 
      echo '2'; 
     } 
    } 
} 

gtext是我保存的话行,我的话中有空格的它们,例如:new iphone,iphone 5s,apple ipad,etc etc)

及这里的检查的话代码:

$.post('guessit.php',{from:1,val:$('#ug').val(),uid:$('#uid').val(),pid:$('#pid').val(),t1:<?php echo $time1; ?>,t2:<?php echo $time3; ?>},function(d){ 
    if(parseInt(d)==1){ 
    $.post('guessit.php',{from:2,val:$('#ug').val(),uid:$('#uid').val(),pid:$('#pid').val(),t1:<?php echo $time1; ?>,t2:<?php echo $time3; ?>},function(d1){ 
     advanced_example($('#uid').val(),'Congratulations!','You are the winner!!',1); 
     //setInterval($(location).attr('href','redirecttohome.php'),10000); 
    }); 
    }else if(parseInt(d)==2){ 
    $.post('guessit.php',{from:3,val:$('#ug').val(),uid:$('#uid').val(),pid:$('#pid').val(),t1:<?php echo $time1; ?>,t2:<?php echo $time3; ?>},function(d1){ 
     advanced_example($('#uid').val(),'Wrong!','Please try again!',1);    
     //setInterval($(location).attr('href','redirecttohome.php'),10000); 
    }); 
    }else if(parseInt(d)==3){ 
    advanced_example($('#uid').val(),'Sorry!','Someone else was faster!',1); 
    //setInterval($(location).attr('href','redirecttohome.php'),8000); 
    }else if(parseInt(d)==4){ 
    advanced_example($('#uid').val(),'Error!','You already attempted maximum times',1);    
    //setInterval($(location).attr('href','redirecttohome.php'),8000); 

} 

guessit.php是包含第一个代码中,我向您展示。

如果您需要任何其他信息以帮助我,请告诉我。

@AmalMurali我需要的是未来:我在MySQL:

apple ipad,apple iphone4,apple ipod,iphone4,apple 

我需要他们为字符串为:

apple ipad 
apple iphone4 
apple ipod 
iphone4 
apple 
+2

'array_walk($ sp,'trim');' –

回答

1

您需要修剪空白的if条件下工作如你所愿:

$sp = explode(",",$gt); 
$sp = array_map('trim', $sp); //trim all the elements in $sp 

如果元素在开始处包含空格o ř端,以下的条件将评估为FALSE,因此引发的发言中else块:

if(in_array($val, $sp)) { 

如果空白被移除,in_array应该工作和如预期的代码应工作。

+0

好的,我会尝试在我自己的代码中,让你们知道。基本上,这会识别短语中的空格,但为什么第一个空格的单词有效?还有什么没有?我只是想保持清醒。谢谢 –

+0

@JognSmit:可能是因为你的字符串有额外的空间。 –

+0

我的短语仅在单词之间有空格,而不在其开头/结尾。现在,我想我明白你在那里说什么,将马上对它进行测试:) –