I want to display rows with 4 items, per row but I want to complete my rows
How can I always get : count(myarray) % 4 = 0 ?
let's say I have : count = 29 , how can I get : 3 ?
29 + 3 = 32 --> 32 % 4 = 0
$foo = 29;
$rounded_up = $foo + (4 - ($foo % 4));
29 % 4 -> 1
4 - 1 -> 3
29 + 3 -> 32
32 % 4 -> 0
This is a math question, not a programming question. But to answer it: the missing number is (4-(count % 4)) % 4. Example: count=29 , count%4=1, 4-1=3. The extra %4 is to catch edge cases like 28: 28%4=0; 4-0=4; 4%4=0.
How about
4 - count(myarray) % 4
x - count(myarray) % x
It will give you the rest.
And as pointed out by the comment below, if you want to avoid having 4 as the result of a complete row, use this :
(4 - count(myarray) % 4) % 4
(x - count(myarray) % x) % x
Try with:
$count = count($array);
$modulo = 4;
$result = $modulo - $count % $modulo;
Related
i need to get previous value and next two values of a current_selected_val with max value stating in the starting only. Currently I am using this and not getting actual results. Can someone please help me in this
<?php
$pager_max = 8;
$current = 3;
for($i = 1; $i <= $pager_max; $i++) {
if ($i > ($current - $pager_max ) + 6 && $i < $current + 3) {
echo $i . '<br>';
}
}
?>
Here are the results which I wanted
If I select $current as
1 - 1 2 3 4
2 - 1 2 3 4
3 - 2 3 4 5
4 - 3 4 5 6
5 - 4 5 6 7
6 - 5 6 7 8
7 - 5 6 7 8
8 - 5 6 7 8
If I change $pager_max to any other value, then behaviour should be same. I need to use only formulas but not any functions here. Thanks in advance
function getValues(int $pager_max, int $current) {
if ($current === 1) {
return range(1, 4);
} elseif ($current + 2 >= $pager_max) {
return range($pager_max - 3, $pager_max);
} else {
return range($current - 1, $current + 2);
}
}
I am trying to use this chunk of code as template, but Im not fully understanding of how one line works. I'll first provide the full chunk, then I'll single out the line I don't understand.
/** settings **/
$images_dir = 'preload-images/';
$thumbs_dir = 'preload-images-thumbs/';
$thumbs_width = 200;
$images_per_row = 3;
/** generate photo gallery **/
$image_files = get_files($images_dir);
if(count($image_files)) {
$index = 0;
foreach($image_files as $index=>$file) {
$index++;
$thumbnail_image = $thumbs_dir.$file;
if(!file_exists($thumbnail_image)) {
$extension = get_file_extension($thumbnail_image);
if($extension) {
make_thumb($images_dir.$file,$thumbnail_image,$thumbs_width);
}
}
echo '<img src="',$thumbnail_image,'" />';
if($index % $images_per_row == 0) { echo '<div class="clear"></div>'; }
}
echo '<div class="clear"></div>';
}
else {
echo '<p>There are no images in this gallery.</p>';
}
I understand how everything with the exception of this line works.
if($index % $images_per_row == 0) { echo '<div class="clear"></div>'; }
I know it is getting its value from this line:
$images_per_row = 3;
But what actually makes this work? Im still pretty new to php, and I would like a better understanding of the code Im about to use before I use it.
Any answers at all would be appreciative!
$index % $images_per_row == 0
The % means "mod", example 4 mod 2 = 0.
A % B = the remainder when we divide A by B.
In your script, the condition is met (valued to 'true') when the remainder of $index divided by $images_per_row equals 0, meaning the divisibility of $index by $images_per_row.
Hope it helps!
% is the modulo operator. It divides the two numbers and then returns the remainder after the division.
It is quite easy to understand if you remember your fractions and how to reduce them to their lowest terms.
So, if we make 5 % 2 into a fraction and reduce it:
5 1 (this is the remainder)
--- → 2 ---
2 2
So, 5 % 2 = 1.
If we take 8 % 3 we can do the same thing:
8 2 (this is the remainder)
--- → 2 ---
3 3
So, 8 % 3 = 2.
If there is no remainder, such as in 9 % 3 then you will get 0 back. See:
9 0 (this is the remainder)
--- → 3 ---
3 3
You can write some PHP to see what the values are when doing the modulo operations:
$perRow = 3;
for ($i = 0; $i < 10; $i++) {
echo "$i % $perRow = ", $i % $perRow, ' | ', "$i / $perRow = ", ($i / $perRow), "\n";
}
Output:
0 % 3 = 0 | 0 / 3 = 0
1 % 3 = 1 | 1 / 3 = 0.33333333333333
2 % 3 = 2 | 2 / 3 = 0.66666666666667
3 % 3 = 0 | 3 / 3 = 1
4 % 3 = 1 | 4 / 3 = 1.3333333333333
5 % 3 = 2 | 5 / 3 = 1.6666666666667
6 % 3 = 0 | 6 / 3 = 2
7 % 3 = 1 | 7 / 3 = 2.3333333333333
8 % 3 = 2 | 8 / 3 = 2.6666666666667
9 % 3 = 0 | 9 / 3 = 3
Okay i will have a number from my database. This could be 3, 15 , 138 etc. Basically any number.
Now if the number is not a multiple of 6 i want to find out how many more until it becomes a multiple of 6.
So for instance if my number is 4, i want it to say you need 2 more to reach a multiple of 6.
How can i achieve this? Also when giving an answer could you please explain how the formula works.
I have tried this which someone suggested
$number = 4;
if($number % 6 != 0) {
echo $number += 6 - ($number % 6);
}
But this just prints 6 out
Using += modifies $number by the value returned
echo $number += 6 - ($number % 6);
Results in: 4 += 6 - 4 or $number = 6
Should be
echo $number = 6 - ($number % 6);
Results in: $number = 6 - 4 or $number = 2
Your numbers are backwards:
$number = 4;
if(6 % $number != 0) {
echo (6 % $number) - 6;
}
https://3v4l.org/1V5Rb
I want to check if a number is divisible by 6 and if not I need to increase it until it becomes divisible.
how can I do that ?
if ($number % 6 != 0) {
$number += 6 - ($number % 6);
}
The modulus operator gives the remainder of the division, so $number % 6 is the amount left over when dividing by 6. This will be faster than doing a loop and continually rechecking.
If decreasing is acceptable then this is even faster:
$number -= $number % 6;
if ($variable % 6 == 0) {
echo 'This number is divisible by 6.';
}:
Make divisible by 6:
$variable += (6 - ($variable % 6)) % 6; // faster than while for large divisors
$num += (6-$num%6)%6;
no need for a while loop! Modulo (%) returns the remainder of a division. IE 20%6 = 2. 6-2 = 4. 20+4 = 24. 24 is divisible by 6.
So you want the next multiple of 6, is that it?
You can divide your number by 6, then ceil it, and multiply it again:
$answer = ceil($foo / 6) * 6;
I see some of the other answers calling the modulo twice.
My preference is not to ask php to do the same thing more than once. For this reason, I cache the remainder.
Other devs may prefer to not generate the extra global variable or have other justifications for using modulo operator twice.
Code: (Demo)
$factor = 6;
for($x = 0; $x < 10; ++$x){ // battery of 10 tests
$number = rand( 0 , 100 );
echo "Number: $number Becomes: ";
if( $remainder = $number % $factor ) { // if not zero
$number += $factor - $remainder; // use cached $remainder instead of calculating again
}
echo "$number\n";
}
Possible Output:
Number: 80 Becomes: 84
Number: 57 Becomes: 60
Number: 94 Becomes: 96
Number: 48 Becomes: 48
Number: 80 Becomes: 84
Number: 36 Becomes: 36
Number: 17 Becomes: 18
Number: 41 Becomes: 42
Number: 3 Becomes: 6
Number: 64 Becomes: 66
Use the Mod % (modulus) operator
if ($x % 6 == 0) return 1;
function nearest_multiple_of_6($x) {
if ($x % 6 == 0) return $x;
return (($x / 6) + 1) * 6;
}
Simply run a while loop that will continue to loop (and increase the number) until the number is divisible by 6.
while ($number % 6 != 0) {
$number++;
}
Assuming $foo is an integer:
$answer = (int) (floor(($foo + 5) / 6) * 6)
For micro-optimisation freaks:
if ($num % 6 != 0)
$num += 6 - $num % 6;
More evaluations of %, but less branching/looping. :-P
Why don't you use the Modulus Operator?
Try this:
while ($s % 6 != 0) $s++;
Or is this what you meant?
<?
$s= <some_number>;
$k= $s % 6;
if($k !=0) $s=$s+6-$k;
?>
result = initial number + (6 - initial number % 6)
I am looking to create alternating designs for the content of each post returned in my loop. In short I want the first post to display left align, next right align, and so on. I have not been able to find a way to do this. Any ideas?
Try something like this:
$count = 0;
foreach ($posts as $post) {
echo "<div class=\"" . (++$count % 2 ? 'left' : 'right') . "\">"
. $post['postText'] // or whatever the crazy wordpress thing is
. "</div>"
;
}
You could loop through the results and then check if an incremented counter is even or odd and display left or right depending on that.
Have a look at the modulo operator '%'
0 % 2 = 0
1 % 2 = 1
2 % 2 = 0
3 % 2 = 1
...
100 % 2 = 0
101 % 2 = 1
You can have a repeating pattern of as many as you like:
0 % 4 = 0
1 % 4 = 1
2 % 4 = 2
3 % 4 = 3
4 % 4 = 0
5 % 4 = 1
....
C.