0

I have a SQL query and an array where I put values to exclude. When I perform my query, I would like to test my variable.

If not empty ==> Display values
If empty ==> Display one time 0 result.

My SQL query gives in output these values:FGFR1e12, FGFR3e7, FGFR3e14

When I perform my php script, I manage to display values but it doesn't enter in the loop else when the variable is empty.

Does anyone know what I am doing wrong in my script ?

Here's my script:

<?php
require_once ('config.php');
$VariantNContrib = "SELECT DISTINCT Reference FROM mytable";
$PerformVariantNContrib = mysqli_query($conn, $VariantNContrib) or die(mysqli_error($conn));
// Values to exclude are stored in an array
$arrayNoContrib = array(
    'FGFR1e12',
    'FGFR3e7',
    'FGFR3e14'
);
while ($rowVarNContrib = mysqli_fetch_assoc($PerformVariantNContrib)) {
    if (!in_array($rowVarNContrib["Reference"], $arrayNoContrib)) {
        $NContribList = '' . implode(',', $rowVarNContrib) . '; ';
        if (!empty($NContribList)) {
            echo '<br/>Variants:' . $NContribList . '<br/>';
        }
        else {
            echo "0 results";
        }
    }
}
?>
2
  • do a var_dump() of $NContribList to see what it contains. That should tell you why it's not working. Alternatively, just replace if(!empty($NContribList)) with if(!count($rowVarNContrib)) to check the number of elements in the array directly rather than looking at the string. Also, off topic, but I would urge you to tidy up your code so that the intentation is correct -- the messy layout of the code you've posted makes it really hard to read. Commented Jul 4, 2016 at 8:57
  • when $arrayNoContrib = array('FGFR1e12', 'FGFR3e7','FGFR3e14'); var_dump($NContribList) displays nothing. When $arrayNoContrib = array('FGFR1e12'); var_dump($NContribList) displays string(7) "FGFR3e7" string(8) "FGFR3e14" Commented Jul 4, 2016 at 9:27

3 Answers 3

1

Your code should be:-

$arrayNoContrib=array('FGFR1e12', 'FGFR3e7', 'FGFR3e14');
while($rowVarNContrib =  mysqli_fetch_assoc($PerformVariantNContrib)) {
  if(!empty($rowVarNContrib) && !in_array($rowVarNContrib["Reference"],$arrayNoContrib)){  // check $rowVarNContrib array is not empty
    $NContribList=implode(',',$rowVarNContrib); // remove empty string      
    echo "Variants: $NContribList"."<br>"; // remove earlier <br>      
  }else{
      echo "0 results"."<br>"; // add <br>
  }   
}
Sign up to request clarification or add additional context in comments.

1 Comment

Hi @Ravi, thx for your script. I tested but unfortunatly, it also don't enter in the loop else when $NContribList is empty. I don't understand why.
0

Hi I dont know why you have used implode if i am right. You have a array in which you have some values and you are fetching some reference and if all are same then you are displaying 0 result and if not then you are displaying that refence number. Then Please once try this one.

<?php
require_once ('config.php');
    $VariantNContrib="SELECT DISTINCT Reference FROM mytable";
    $PerformVariantNContrib=mysqli_query($conn,$VariantNContrib) or die(mysqli_error($conn));
    //Values to exclude are stored in an array
   $arrayNoContrib = array(
    'FGFR1e12',
    'FGFR3e7',
    'FGFR3e14'
);
    $i = 0;
    while($rowVarNContrib =  mysqli_fetch_assoc($PerformVariantNContrib)) {
        if (!in_array($rowVarNContrib["Reference"], $arrayNoContrib)) {
            $NContribList = trim($rowVarNContrib["Reference"]);
            if (!empty($NContribList)) {
                echo '<br/>Variants:' . $NContribList . '<br/>';
                $i++;
        }}

    }
      echo $i." results";

2 Comments

Hi @mamta, thx for your help. Your script works at my end :). I actually used implode in order to display my results variants as the following: Variants: FGFR1e12;FGFR3e7; and not repeating Variants each time and having all on the same line. So I cannot use implode ?
hey you can store variants in array and then use implode after while loop. to display your desired result.
0

empty() function does not return true for string which is single space. " ".

$NContribList=''.implode(',',$rowVarNContrib).' ';

In above code you've space at the end of the string and that is the reason empty() is returning FALSE.

Either remove that space from the string or use PHP trim() to remove unnecessary spaces.

3 Comments

Hi @Alok, thx for your answer. I tried to remove undesired space by using rtrim: $NContribList=''.implode(',',$rowVarNContrib).'; '; $NContribList2=rtrim($NContribList); but it doesn't enter in else loop :(
Also remove ; in the string. Check my updated answer.
I also tried with your update answer $NContribList=''.implode(',',$rowVarNContrib).' '; it doesn't work either. If I remove a value from $arrayNoContrib it enters into the if loop but if I let the 3 values into $arrayNoContrib it never enters into the else loop :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.