Increment int var every 10 loop iterations

I am building a long query of dummy data. I have a for loop that runs several thousand times, in each loop it adds a new line to the query, with the increment variable used for dummy file names, 1.png, 2.png, etc. I also need to increment a separate foreign key identifier, but this should only be every 10 iterations of the loop. can anyone help? Thank.

$var = '';
for($i=0;$i<100;$i++){
    $path = '/test' . $i . '.pdf';
    $mess = 'Test message '. substr(md5($i), 0, 10);
    $did = 0;//How to increment this var by 1 every 10 iterations?
    $var .= "INSERT INTO `message` (`design_id`, `path`, `message`) VALUES ('$did', '$path', '$mess');"."<br />";
}
echo $var;
+3
source share
4 answers

You can use the module operator > :

$did = 0;
for($i=0;$i<100;$i++){
    $did += $i % 10 == 9;
}

The expression $i % 10begins with 0, increases to 9 (10 - 1) and then "wraps" endlessly. $didincreases every time it is evaluated to 9, i.e. every 10 iterations.

, [0, 9] , $did ; , . 9 , 10-, 20- .. . 0 , 1, 11, 21 ..

, , ,

$did += ($i % 10 == 9);

if.

+6

$ + = 10

:

for ($i=0; $i<100; $i+=10) {
}
+13

Divide and gender.

$ did = floor (i / 10);

0
source
$var = '';
$did = 0;
for($i=1;$i<=100;$i++){
    $path = '/test' . $i . '.pdf';
    $mess = 'Test message '. substr(md5($i), 0, 10);
    if((i % 10) == 0){
          $did++;
     }
    $var .= "INSERT INTO `message` (`design_id`, `path`, `message`) VALUES ('$did', '$path', '$mess');"."<br />";
}
echo $var;
0
source

All Articles