Jogn Smit
Jogn Smit

Reputation: 87

Stucked with array and explode - spaces

I have an application called mystique item where users has to guess the item that's behind the watermark. Water mark is revealing on time to time and everything is working perfect, but i have a "small" problem with guessing words. I have added words in arrays, separated with commas, and i'm exploding that array in my php, but for some reason, it only catches the first word as correct, everything else is being incorrect. Here's what i have done.

$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 is the row where I store the words, my words has spaces in them, for example: new iphone,iphone 5s,apple ipad,etc etc).

And here's the code that checks the words:

$.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 is containing the first code I've showed you.

If you need anything else in order to help me, please let me know.

@AmalMurali What I need is next: I have in MySQL:

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

I need them as strings as:

apple ipad
apple iphone4
apple ipod
iphone4
apple

Upvotes: 0

Views: 125

Answers (1)

Amal Murali
Amal Murali

Reputation: 76656

You need to trim the whitespace for the if conditions to work as you want it:

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

If the elements contain a whitespace in the beginning or end, the following condition will evaluate to FALSE, thus triggering the statements in else block:

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

If the whitespace is removed, in_array should work and the code should work as expected.

Upvotes: 1

Related Questions