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.
Related
I have a question which I don't know the answer of. I've been thinking about it for a while.
The following code:
$i = 1;
while($i < 10)
if(($i++) % 2 == 0)
echo $i;
It correctly outputs 3579, but why isnt 1 also included in the output?
I'm a beginner with PHP and am looking forward for someone to help me.
Thank you very much! :D
Two modifications:
$i = 0; // Make it 0 from 1
while($i < 10)
if(($i++) % 2 == 0)
echo "<br/>".$i; // Make $i instead of $1
Output:
1
3
5
7
9
Program hand run:
1) Set $i to 0.
2) If it is greater than 10, go ahead.
3) Increment it by 1
4) So, for $i => 0->1, 1->2
4) if new $i is even, print it. (So for first loop iteration, you are have $i -> 1 instead of 0 because of ++$i
Hi guys I tried to echo this using php
even odd odd even odd odd
and so on .
My code :
For( $i = 0 ; $i <=11; $i++ ){
If( $i % 2 == 0 ) {
echo " even " ;
}
If( $i % 2 != 0 ) {
echo " odd " ;
$i = $i+1 ;
}
}
And sure the result is :
even odd odd odd ood
and so on .
I wanted to repeat ( even ) one time and ( odd ) twice and so on ..
Can someone help me in that ? Thanks
You can use% operator.
Where any counter that is exactly divisible by 3 is even, other case is odd.
Answer:
for ($i=0 ; $i<=11 ; ++$i) {
if ($i%3==0) {
echo "Even";
}
else {
echo "Odd";
}
}
Hand Run:
$i - $i%3 - Output
--------------------
0 - 0 - Even
1 - 1 - Odd
2 - 2 - Odd
3 - 0 - Even
4 - 1 - Odd
5 - 2 - Odd
6 - 0 - Even
7 - 1 - Odd
8 - 2 - Odd
9 - 0 - Even
10 - 1 - Odd
11 - 2 - Odd
EDIT: Using ternary operator, you can even reduce the lines of code.
Reduced Code:
for ($i=0 ; $i<=11 ; ++$i) {
echo ($i%3==0) ? 'Even' : 'Odd';
}
In this case, you could simply use modulo 3 instead of 2 with a if-else statement
for ($i = 0; $i < 11; $i++) {
if ($i % 3 == 0) {
echo 'even ';
} else {
echo 'odd ';
}
}
I'm not sure if you want the desired behaviour of using % 3 as suggested by the others (even though it gets you the result you want) but i'm not sure you'd get the same 'count'. So I'm just going to simply say, why don't you just write 'odd' in your echo statement twice?
for( $i = 0 ; $i <=11; $i++ ){
if( $i % 2 == 0 ) {
echo " even " ;
}else {
echo " odd odd ";
}
}
If you could help us understand why you want to print odd out twice on the odd numbers that would help? Or whether it doesn't actually matter if i is even or odd itself but just the output you're after (as suggested by using % 3).
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.
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.
Using a php like so..
for($i = 0; $i < 30; $i++) ...
I have this html element that is rendered several times. I want to, each time we arrive at the sixth element, it adds a "style:margin-right: 0px;" for example.
My question is:
How can we find always the 6th element ?
Update: So that can mark the 6th element, then the 12th element, then the 18th element then the 24th and, at least, the 30th.
Thanks in advance,
MEM
You can use the modulo operator, %:
for ($i = 0; $i < 30; $i++) {
if ($i % 6 == 5) {
# Add what you want---I don't use PHP much
}
}
The modulo operator, %, divides the left hand side by the right hand side, and then reports the remainder of the result. So, for instance, 15 % 6 == 3, because 15 == 6*2 + 3. In the expression a % b == c, c will range from 0 to b-1. If you had $i % 6 == 0 in the above test, it would style the first element, the seventh element, etc.; this way, it'll style the sixth element, the twelfth element, etc. This is because when you're on the sixth element, $i == 5, and 5 % 6 is of course 6. For more information, check out what Wikipedia has to say about the modulo operation.
Check that the mod of $i and 6 is 0 (means that $i is evenly divisible by 6).
for($i = 0; $i < 30; $i++) {
if($i % 6 == 0) {
// this is a sixth element
}
...
}
If you don't want this to happen on the first iteration ($i == 0), you'll also need to add that check to the if statement:
if($i > 0 && $i % 6 == 0){
}
you can try using modulus (%)
if(!($i % 6)) {
// add style
}
or
if(($i % 6) == 0) {
// add style
}
EDIT: Kaleb beats me to it =/