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 =/
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
I have for loop from 1 to 6000 and I would like to execute a function every 100 times. I have only such an idea:
for($i=0;$i<=6000;$i++)
{
if($i==100 && $i==200 ... $i==6000) do something;
}
How can I solve this problem differently?
From http://php.net/manual/en/language.operators.arithmetic.php
if ($i % 100 == 0) {
// $i can be fully divided by 100
}
The modulo operator (%) tells you if a number divided by another number has a remainder. If the remainder is 0, you know the first number is a multiple of the second (since it divides evenly).
Just check if i is a multiple of 100:
for($i=0;$i<=6000;$i++)
{
if($i % 100 == 0) {
}
}
I agree with the answers this question has received already. However, you might want to omit the case when $i is 0. So you can check it in your for loop if you are starting from 0.
for($i=0; $i<=6000; $i++)
{
if($i != 0 && $i % 100 == 0){
// do something
}
}
If you have a simple counter loop, how do you detect special patterns, for example, every 10 increments but at 6/16/26/36. $i needs to start at 0 too.
The only approach I can think of is this one, but obviously it doesn't work for large loops:
for ($i=0; $i < 1000; $i++) {
// if ( $i == 6 || $i == 16 || $i == 26...... etc ) { do something }
}
There's not going to be one single answer for all types of patterns, but so long as there is a pattern, you can figure it out:
for ($i=0; $i<1000; $i++) {
if (($i-6)%10 == 0) {
// every time $i minus 6 is evenly divisible by 10
}
}
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.
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.