php array and for loop - add br after 5 resultss
Use the Modulus operator.
<?php
$sm2 = array( "angry", "cool", "cry", "happy", "heart", "kiss", "mute", "sad", "smile");
for($j=0;$j<count($sm2); $j++) {
if(!empty($j) && $j % 5 == 0) {
echo '<br>';
}
echo $sm2[$j];
}
Output:
angrycoolcryhappyheart<br>kissmutesadsmile
Demo: https://eval.in/614166
Or with your actual code:
for($j=0;$j<count($sm2); $j++) {
if(!empty($j) && $j % 5 == 0) {
$data .= '<br>';
}
$data=$data . "<img id='". $sm2[$j] ."' src='images/emotions/" . $sm2[$j] . ".png' data-toggle='tooltip' title=". $sm2[$j] ." width='32' height='32' style='margin:5px;'
onclick='insertEmoticons(this.id);'/>";
}
Also note $data=$data .
is the same as $data .= ...
.
Execute statement every N iterations in Python
How about keeping a counter and resetting it to zero when you reach the wanted number? Adding and checking equality is faster than modulo.
printcounter = 0
# Whatever a while loop is in Python
while (...):
...
if (printcounter == 1000000):
print('Progress report...')
printcounter = 0
...
printcounter += 1
Although it's quite possible that the compiler is doing some sort of optimization like this for you already... but this may give you some peace of mind.
How to run a function multiple times and write the results to a list?
Combination of above answer, comment, and my own answer. Naturally, I like mine better. Also, I think there is a mistake in the above answer for base
R.
n <- 10
# give 1 to gen_mat n-times
lapply(rep(1, n), gen_mat)
# replicate n-times
replicate(n, gen_mat(1), simplify=FALSE)
# lapply returns error if FUN is not function or
# the function is not taking an argument. Hence a dummy function.
lapply(seq_len(n), function(x) gen_mat(1))
microbenchmarking the three methods
I used a larger value for n
, but the results are similar in my desktop with smaller n
as well. For this, replicate
takes longer than the other two methods.
set.seed(1)
gen_mat <- function(x) matrix(c(1, 1, 1, x + rnorm(1)), nrow = 2)
n <- 1000
library(microbenchmark)
library(ggplot2)
mb <- microbenchmark(
lap1 = {lapply(rep(1, n), gen_mat)},
repl = {replicate(n, gen_mat(1), simplify=FALSE)},
lap2 = {lapply(seq_len(n), function(x) gen_mat(1))},
times = 10000L
)
mb
# Unit: milliseconds
# expr min lq mean median uq max neval cld
# lap1 2.839435 3.494157 4.806954 3.854269 5.611413 103.0111 10000 a
# repl 3.091829 3.777199 5.140789 4.165856 6.013591 101.4318 10000 b
# lap2 3.131491 3.761274 5.089170 4.140316 5.939075 101.1983 10000 b
autoplot(mb)
flutter run function every x amount of seconds
build()
can and usually will be called more than once and every time a new Timer.periodic
is created.
You need to move that code out of build()
like
Timer? timer;
@override
void initState() {
super.initState();
timer = Timer.periodic(Duration(seconds: 15), (Timer t) => checkForNewSharedLists());
}
@override
void dispose() {
timer?.cancel();
super.dispose();
}
Even better would be to move out such code from widgets entirely in an API layer or similar and use a StreamBuilder
to have the view updated in case of updated data.
Run certain code every n seconds
import threading
def printit():
threading.Timer(5.0, printit).start()
print "Hello, World!"
printit()
# continue with the rest of your code
https://docs.python.org/3/library/threading.html#timer-objects
PHP: How do you determine every Nth iteration of a loop?
The easiest way is to use the modulus division operator.
if ($counter % 3 == 0) {
echo 'image file';
}
How this works:
Modulus division returns the remainder. The remainder is always equal to 0 when you are at an even multiple.
There is one catch: 0 % 3
is equal to 0. This could result in unexpected results if your counter starts at 0.
Related Topics
How to Serve Documents from Outside the Web Root Using PHP
Jquery Mobile: Sending Data from One Page to the Another
Use Curly Brackets to Structure Code in PHP
Do I Really Need to Do MySQL_Close()
How to Pass Multiple Variables Across Multiple Pages
How to Bind an Arbitrary Number of Values to a Prepared Statement in MySQLi
How to Retrieve Comments from Within an Xml Document in PHP
Accessing Files Relative to Bundle in Symfony2
Convert Lat/Longs to X/Y Co-Ordinates
Oauth Implementation in Netsuite Using PHP
In PHP, How to Check If a Function Exists
Pdo Error: SQLstate[Hy000]: General Error: 2031
Saving Images in Database MySQL
How to Display PHP & HTML Source Code on a Page