Why are the data shown the opposite?

1

Queries are made through queries prepared from mysqli, variable $previa can bring two types of results yes or not

The query is made by means of data related to an example:

id  articulo  previa  id_articulo
 1    algo     yes        2
 2    algo1    yes        2

As you can see, results of the same article id are included.

And the results are as follows:

something - > not something1 - > not

But it should be this way since in the database this data as yes

something - > And it is something1 - > yes

  

Note: But if I get to change the value yes by not there if it shows the result it should show, but everything is doing the other way around.

I am using the following code:

if($previa!="yes"){
  echo $articulo;
  echo 'yes';
} else {
  echo $articulo;
  echo 'not';
}
    
asked by Oscar 17.11.2017 в 00:16
source

3 answers

2

You have to keep in mind that the operator != means other than .

In your case, you are saying that if it is different from yes , that is, no , show not per screen and vice versa.

You would have to change the operator of your if to make a comparison. To indicate if a variable is equal to something, use the double == (or triple === if you want an exact comparison).

That would be your comparison:

if($previa=="yes"){
  echo $articulo;
  echo 'yes';
} else {
  echo $articulo;
  echo 'not';
}
    
answered by 17.11.2017 / 00:41
source
1

yes yes is different from yes {echo $ echo 'yes';} if not {echo $ article;       echo 'not';}

if($previa!="yes"){
  echo $articulo;
  echo 'yes';
} else {
  echo $articulo;
  echo 'not';
}

is wrong if yes is the same as yes echo $ article; echo 'yes'; else {echo $ article;           echo 'not';}

if($previa==="yes"){
      echo $articulo;
      echo 'yes';
    } else {
      echo $articulo;
      echo 'not';
    }
    
answered by 17.11.2017 в 00:22
1

Try this evaluation with ternary operators:

$articulo='articulo-1';
$previa='yes';
$str=($previa=='yes') ? $articulo.' '.$previa : $articulo.' '.$previa;
echo $str.PHP_EOL;


$articulo='articulo-2';
$previa='no';
$str=($previa=='yes') ? $articulo.' '.$previa : $articulo.' '.$previa;
echo $str.PHP_EOL;

Output:

articulo-1 yes
articulo-2 no

Conclusion

With this single line would suffice:

$str=($previa=='yes') ? $articulo.' '.$previa : $articulo.' '.$previa;

Or, if you want a not when it's not yes, you change it for this:

$str=($previa=='yes') ? $articulo.' '.$previa : $articulo.' not';
    
answered by 17.11.2017 в 00:48