0

In my php code I have the results of the DB displaying and all is good. Now I have added a single line of text that shows the number of rows returned. What I don't like about it is that it shows "There are 1 results" and "There are 5 results", etc. I have made an if statement "if $queryResult = 1" then echo "There is 1 result", and also "if $queryResult > 1" echo "There are xx results", and after that "if $queryResult > 0" echo out all the rows of results.

Right now, this is the code and it works except for the incorrect usage "are" and "results" when there is only 1 result:

$queryResult = mysqli_num_rows($result);
echo "There are ".$queryResult." results.";
if ($queryResult > 0) 
{ // output data of each row
while($row = mysqli_fetch_assoc($result)) 
{ // Display each field of the records.

So, I tried this code:

$queryResult = mysqli_num_rows($result);
//echo "There are ".$queryResult." results.";
if ($queryResult = 1) 
{
echo "There was ".$queryResult." lesson found.";
}
elseif ($queryResult > 1) 
{
echo "There were ".$queryResult." lessons found.";
}
else ($queryResult > 0) 
{ // output data of each row
while($row = mysqli_fetch_assoc($result)) 
{ // Display each field of the records.

and it breaks the page with an error of: syntax error, unexpected 'while' (T_WHILE) I'm sure there's more wrong that just that, I'm sure I'm missing something regarding the first if and elseif statements, but what is it? What am I doing wrong?

I've also enclosed the first if and elseif statements within curly braces but that doesn't work either.

I know that the difference between using "is" and "are" is trivial, but this is being used on a website for people who are learning English as a second language, which means it should be using English correctly. And "There are 1 results" is not good English for new learners to be seeing.

Dharman
  • 30,962
  • 25
  • 85
  • 135
ChipW
  • 3
  • 1

1 Answers1

0

You have some syntax errors in your code, one of them being that you were setting your $queryResult to 1 here:

if ($queryResult = 1) 

You want:

if ($queryResult == 1) 

However it's easier to condense your code like this:

$rsltCount = mysqli_num_rows($result);
echo  $rsltCount === 1 ? "There is 1 result." : "There are ".$rsltCount." results.";

while($row = mysqli_fetch_assoc($result)) 
{ 
    // Display each field of the records.
}
symlink
  • 11,984
  • 7
  • 29
  • 50