Curtis Stewart
Curtis Stewart

Reputation: 63

How can I loop through a table skipping every 11th row while doing something on every 10th loop?

This is my thought process of how I think it should work.

if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) && ($row % 11 != 0)) {
        $counter++;
        if ($counter % 10 == 0) {
         // Do something.
        }
    }
}

Example of how it should work:

Row 1 = Counter 1
Row 2 = Counter 2
Row 3 = Counter 3
Row 4 = Counter 4
Row 5 = Counter 5
Row 6 = Counter 6
Row 7 = Counter 7
Row 8 = Counter 8
Row 9 = Counter 9
Row 10 = Counter 10
Row 11 = Skip
Row 12 = Counter 11
Row 13 = Counter 12
Row 14 = Counter 13
Row 15 = Counter 14
Row 16 = Counter 15
Row 17 = Counter 16
Row 18 = Counter 17
Row 19 = Counter 18
Row 20 = Counter 19
Row 21 = Counter 20
Row 22 = Skip
Row 23 = Counter 21
etc.

For further reference, here is the full piece of code I've written to remind users on my web-store that if they have 10 tickets in their cart, they'll get the next one free if they add it. Thanks Jakar

$fancyCounter = 1;

if ($result->num_rows > 0) {

     // output data of each row
     while($row = $result->fetch_assoc()) {
         $rawCounter++;
        if ($rawCounter % 11 !== 0) {
            $fancyCounter++;
        }
        else {
            $fancyCounter = 1;
        }
    }
}
$result->close();

if ($fancyCounter % 11 == 0) {
    $freeTicketAvailable = 1;
} else {
    $freeTicketAvailable = 0;
}

Upvotes: 2

Views: 90

Answers (3)

i4h
i4h

Reputation: 861

This should do it. You were trying to divide an array ($row) by a number in your while condition, which is probably undefined even in PHP ;)

if ($result->num_rows > 0) {  
    $skipCounter = 0;
    $tenCounter = 0;
    $counter = 0;
    while($row = $result->fetch_assoc()) {
        ++$skipCounter;
        if ($skipCounter == 11) {
          $skipCounter = 0;
          continue;
        } 
        ++$counter
        ++$tenCounter;
        echo "Counter: ".$counter;
        if ($tenCounter  ==  10) {
           $tenCounter = 0;
           echo "Do something";
        }
    }
 }

Upvotes: 1

Reed
Reed

Reputation: 14984

Here, there is a $rawCounter which counts the rows iterated and a $fancyCounter which keeps the row count just how you posted.

while($row=$result->fetch_assoc()){
     $rawCounter++;
     if ($rawCounter%11!==0){
         $fancyCounter++;
     } 
     if ($rawCounter%10===0){
         //Do Something
     }
}

Here is an example that give your example output.

If you need the row number and the counter number separate, you could also use a $counter and a $skipCount, and always increment $counter and only increment $skipCount on $counter%11==0 and to get the counter count you want (ie Row 12 = Counter 11, Row 23 = Counter 21), you'd use echo "Row {$counter} = Counter ".($counter-$skipCount)

Upvotes: 1

Script47
Script47

Reputation: 14540

Try this,

<?php

$counter = 1;

while ($counter <= 33) {

    if ($counter % 10 == 0) {
        // On 10.
        echo "Do Something<br/>";
    } else if ($counter % 11 == 0) {
        // On 11.
        echo "Skip<br/>";
    } else {
        // Other numbers.
        echo $counter . "<br/>";
    }
    $counter++;
}

?>

You need to replace the condition inside the while(); to suit you.

Output

1 2 3 4 5 6 7 8 9 Do Something Skip 12 13 14 15 16 17 18 19 Do Something 21 Skip 23 24 25 26 27 28 29 Do Something 31 32 Skip

Upvotes: 1

Related Questions