简体   繁体   中英

Total count inside an if of while loop

I'm trying to figure out how to do the totalCount inside an if of a while loop but so far are not successfull in doing so

How can i reach my goal with this code

    $rowCount = 1;
    while ($clientrows = $statement->fetch()) {
    // should return 1000 rows 
        if ($clientrows['company'] != $current_client) {
            echo $rowCount++;
            $rowCount++;
                    // return clientX (one time)        
        }
        if ($clientrows['time'] != NULL  ) {
                    // returns X rows of clientX(from the previews count 
                    // could be any number but lets say here it should be 100 rows


            echo $rowCount++;
            $rowCount++;
            // i need the total counts inside this IF($clientrows['time'] != NULL  )

        }
                    echo $rowCount++; // doesn't show anything 
        // create here the IF total count of previews IF function
        // if($totalCounts) {
        //      echo ' Do something here ';
        // }
    }
    echo $rowCount++; //shows the total rows of while loop while ($clientrows = $statement->fetch())

if i put the count++ right after the IF function it doesn't show anything if i do it at the end then it counts the total of everything

EDIT Number of counts returns all the rows of my while loop of while ($clientrows = $statement->fetch() (that is about 1000) what i need to achieve is to get the total rows of this IF statment
if ($clientrows['time'] != NULL ) { } Which could be 100 or 101 or 200 of the 1000 rows

Help?

"++" is incrementation. It will add + 1 into your variable. It's equal as $a = $a + 1;

Intead of

echo $rowCount++;
$rowCount++;

write

echo $rowCount;
$rowCount++;

Edit. I just read your comment in script

//shows the total rows of while loop while ($clientrows = $statement->fetch())

Can not you just simply use $statment->num_rows()?

When you do echo $rowCount++; you increment the $rowCount even if you only echo it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM