From the course: PHP for Web Designers
Unlock the full course today
Join today to access over 22,600 courses taught by industry experts or purchase this course individually.
Repeating output at specific intervals in a loop - PHP Tutorial
From the course: PHP for Web Designers
Repeating output at specific intervals in a loop
So now we know how modulo division can be used to identify every fourth item in a series. We can fix the loop, in mixed.php. To keep track of the number of times the loop has run, we need to initialize a counter variable, outside the loop, and set its value to zero. So, the loop begins on line 57. We need to add our counter variable before that loop. So, let's just add in a few lines. And counter variables in PHP, by tradition, are usually $i. So, we'll set that to zero. And then, when the loop begins, we need to have an if decision making block. So if, and then the condition will be with a i, is equally divisible by four. So using modulo division, and to make sure that it is actually zero and not a false result we need three equal signs, followed by zero, and then we need an opening curly brace for our block. Then these two tags will be displayed, and we need the closing curly brace for that conditional block. That goes there. So the first time that the loop runs, i will be zero…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
-
-
-
(Locked)
Loading data into MySQL5m 37s
-
(Locked)
Connecting to the database7m 47s
-
Querying the database6m 15s
-
(Locked)
Displaying the results of the query3m 50s
-
(Locked)
Using modulo division to establish a repeating series4m 20s
-
(Locked)
Repeating output at specific intervals in a loop4m 56s
-
(Locked)
Linking to a details page2m 40s
-
(Locked)
Embedding a variable in a query securely6m 5s
-
(Locked)
Handling database errors gracefully--and securely8m 40s
-
(Locked)
-
-