坚持阵列和爆炸 - 空间

时间:2013-09-22 12:05:33

标签: php mysql arrays explode

我有一个名为mystique item的应用程序,用户必须猜测水印背后的项目。水印时不时地显露出来,一切都很完美,但是我猜测一个“小”的问题。我在数组中添加了单词,用逗号分隔,我在我的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

1 个答案:

答案 0 :(得分:1)

您需要修剪if条件的空白才能正常工作:

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

如果元素在开头或结尾包含空格,则以下条件将计算为FALSE,从而触发else块中的语句:

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

如果删除了空格,in_array应该有效,代码应该按预期工作。

相关问题