While loop ommitting every fourth result - php

I'm try to dynamically generate an HTML table containing data from a database. The output looks perfect, but upon closer inspection I realized it was omitting every fourth result. I know it has something to do with the structure of my while loop and the if/else statement within, but I'm not sure what it is exactly.
$i=0;
while ($person = $pull_person->fetch()){
if ($i <= 2){
echo "<td valign='top'>";
echo "<h3>" . $person['person_name'] . " - " . $person['person_id'] . "</h3>";
echo "<label style='background-image:url(" . $person['person_pic'] . ");'><input type='checkbox' name='person[]' value='" . $person['person_id'] . "''></label>";
echo "</td>";
$i++;
}
else{
echo "</tr>";
echo "<tr>";
$i=0;
}
}
It's gotta be something simple/obvious, but it's not registering with me. Thanks!

The loop is not hitting the fourth result because of the loop limiting logic.
$i=0;
while ($person = $pull_person->fetch()){
if ($i <= 2){
echo "<p>item: $i</p>";
$i++;
}
}
Iteration 1: $i = 0
Iteration 2: $i = 1
Iteration 3: $i = 2
Iteration 4: $i = 3
Iteration 4 is never hit because it checks and sees that $i must be less than or equal to 2. If you change this to be less than or equal to 3 it will work as you want.
if ($i <= 3)

Evidently... you only increment the variable $i when the condition is met:
$i=0;
while ($person = $pull_person->fetch())
{
if ($i <= 2)
{
//output
$i++;
}
else
{
//no output
$i=0;
}
}
So this happens:
iteration old $i new $i output
1 0 1 yes
2 1 2 yes
3 2 3 yes
4 3 0 no //condition not met
5 0 1 yes //loops...
...
What you observe here is that the code will skip the output of the iteration given by the number in the conditional plust 2. So, for example, if you use the condtion $i <= 3, the results are:
iteration old $i new $i output
1 0 1 yes
2 1 2 yes
3 2 3 yes
4 3 4 yes
5 4 0 no //condition not met
6 0 1 yes //loops...
...
If you want to insert something each n iterations, do as follows:
$n = 3; //number of items per row
$i = 0;
while ($person = $pull_person->fetch())
{
//output item
$i++;
if ($i == $n)
{
//something each $n iterations
$i=0;
}
}
The effect is the following (assuming $n = 3):
iteration old $i new $i new row
1 0 1 no
2 1 2 no
3 2 0 yes //condition is met, $i reset to 0
4 0 1 no
5 1 2 no
6 2 0 yes //condition is met, $i reset to 0
...
Note 1: every iteration outputs an item.
Note 2: you can adjust the initial value of i to have an offset.

Misread question but will leave the answer as it may provide use anyway.
Best way is to look at the array as a 'module' (mathematically) -- i.e. use Modular Arithmetic:
if ((i % 4) == 0)
That is to say, indices 0, 4, 8, 12, 16, ... will be targeted, only. And, actually, this is how most setInterval functions work under the hood.

Related

(PHP) Why does this generator-script adds the starting value + the step-value to the starting value?

the title may be pretty confusing. Here's a script which requires a start-end and step value to add numbers to the start value:
function addieren($start, $ende, $schritt = 1) {
if ($start < $ende) {
$erg = 0;
for ($i = $start; $i <= $ende; $i += $schritt) {
$erg += $i;
yield $erg;
}
}
}
foreach (addieren(2, 10, 2) as $erg) {
echo $erg . "<br>";
}
So the start value is 2, the end value is 10 (but it's not ending at 10 as the result shows), and it should add 2 to the $i every step.
Here's the output:
2
6
12
20
30
The first output is clear to me, since the $erg was 0 and it added the 2 because of 2 steps.
But the next output is 6 and I don't get why. In the second loop, $i is 2 and and the script says: $i += $schritt so when $i is 2 and the $schritt value is also 2, why doesn't that output 4 as the second output? Hope you get what I mean.. I guess it's a pure logic error in my head.

why does this php code return me 13 and not 3? I'm really confused

Can someone tell me why the output of echo of this Code is 13?
$a=10;
$b=2;
$j=$a/2;
for ($i=0;$i<$j;$i++){
if ($i % $b == 1)
echo "$i";
}
Try this
<?php
$a=10;
$b=2;
$j=$a/2;
echo $j;
echo "<br>";
for ($i=0;$i<$j;$i++){
if ($i % $b == 1)
echo "$i";
echo "<br>";
}
?>
because $a have 10 value , $b have 2 value and $j have 5 value
when start loop then $i start from 0 loop have max 5 loops from 0 to 4
so
then loop start first then $i have 0 value so $i % will be equal
1 so nothing display
when start second loop then $i have 1 value then $i % will be
equal 1 so display 1 because now $i have 1 value
when loop run third time then $i have 2 value then $i % will be
equal 0 so nothing display
when loop run fourth time then $i have 3 value then $i % will be
equal 1 so display 3 because now $i have 3 value
when loop run fifth time then $i have 4 value then $i % will
be equal 0 so nothing display
$a=10;
$b=2;
$j=$a/2; //which will be 5
for ($i=0;$i<$j;$i++){ //the loop executes 5 times
if ($i % $b == 1) // this condition satisfies when $i becomes 1 && 3
echo "$i"; //1 and 3 will be printed.
}
Check The comments written in your code
You code is like this,
echo 1; echo 3;
output 13
I hope that will help you understand how your code works.
$a=10;
$b=2;
$j=$a/2;
for ($i=0;$i<$j;$i++){
if ($i % $b == 1)
echo "output";
echo "$i";
}

PHP + Echo Data Routinely

I have the following PHP code working 'successfully' to display URL's:
<?php
for ($i = 0; $i <= $json->domaincount; $i++) {
echo '<td class="domainList">'.$json->$i.'</td>';
}
?>
Every forth echo I want to also
echo </tr><tr>
to start a new line in the table.
Is there an easy way to know which is every forth count?
I have $i which increments from 0 up so when it gets to 3, 7, 11 etc I need to change table line.
thx
Try this:
if ( ( $i + 1 ) % 4 == 0 ) { echo '</tr><tr>'; }
This is using the modulus operator. It divides a number and returns the remainder, so 7 % 4 = 3 (because 4 fits in once, and three is left over) and 8 % 4 = 0 (because 4 fits in evenly and there are no left overs)
Look into modulo operands (% in PHP). So the check that i % 4 == 0 would give you every fourth row.
<?php
for ($i = 0; $i <= $json->domaincount;) {
echo '<td class="domainList">'.$json->$i.'</td>';
if (!(++$i & 3))
echo '</tr><tr>';
}
?>
!($i & 3) is just a quick way of writing $i % 4 === 0 without having to use modulus. You can only use that trick for modding by powers of 2.

PHP echo in rows of three?

I am attempting to show data in rows of three like this (notice the number of items will not always be even):
abcd defg hijk
lmno pqrs tuvw
xyz1 2345 6789
1011 1213
I am struggling to get the logic right to do this (this is in a foreach() loop).
I know I have to have some if($i %3 == 0) logic in there.. But I'm a bit stuck.
Can anyone help me out?
$a = array('abcd','defg','hijk','lmno');
for ($i = 0; $i < count($a); $i++) {
if ($i && $i % 3 == 0)
echo '<br />';
echo $a[$i].' ';
}
It's better to use a for loop as:
// run $i for each index in the array.
for($i=0 ; $i<count($arr) ; $i++) {
// if $i is non-zero and is divisible by 3 print a line break.
if ($i && $i % 3 == 0) {
echo "<br />";
}
// print the element at index $i.
echo $arr[$i].' ';
}
Code in action
Pseudo-code since I don't know PHP (and you asked for the logic which tends to be the same across all procedural languages):
perline = 3
i = 0
foreach item in list:
if i > 0 and (i % perline) == 0:
print newline
if (i % perline) != 0:
print space
print item
i = i + 1
This will both output a line separator before elements 3, 6, 9 and so on (first element being 0) and place whatever desired spacing you want before the second and third elements on each line. You can just use a different value for perline to change the number output on each line.

how to do this.. backwards?

I currently have:
$i = 1;
while {
echo $i;
$i++;
}
And it shows:
1
2
3
4 etc..
How would I make it display backwards?
For example
4
3
2
1 etc..
I basically want to do the exact same thing but flip it around.
$i = 10;
while($i>0) {
echo $i;
$i--;
}
Example - Print number through 0 to 5 with PHP For Loop
for($i=0; $i<=5; $i=$i+1)
{
echo $i." ";
}
In the above example, we set a counter variable $i to 0. In the second statement of our for loop, we set the condition value to our counter variable $i to 5, i.e. the loop will execute until $i reaches 5. In the third statement, we set $i to increment by 1.
The above code will output numbers through 0 to 5 as 0 1 2 3 4 5.
Note: The third increment statement can be set to increment by any number. In our above example, we can set $i to increment by 2, i.e., $i=$i+2. In this case the code will produce 0 2 4.
Example - Print number through 5 to 0 with PHP For Loop
What if we want to go backwards, that is, print number though 0 to 5 in reverse order? We simple initialize the counter variable $i to 5, set its condition to 0 and decrement $i by 1.
for($i=5; $i>=0; $i=$i-1)
{
echo $i." ";
}
The above code will output number from 5 to 0 as 5 4 3 2 1 0 looping backwards.
Good luck! :)
If you want to back-word sr number as per your count of rows in result then use this.
$num_rows = mysqli_num_rows($query);
$x = $num_rows;
$x--;
$i = 4;
while($i > 0) {
echo $i--;
}

Categories