What makes this work? - php

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

Related

Get previous one item and next two item of current item in PHP. with max limit and current index only using for loop

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);
}
}

Pascals Diagonal Triangle in PHP

I'm trying to use php code to print out pascals triangle (in the diagonal style like this- http://www.cut-the-knot.org/arithmetic/combinatorics/PascalTriangle.gif)
I tried this code:
<?php
$f = 10;
for ($x = 0; $x <= $f; $x++) {
echo "1"." ";
$previous_line[$x]=1;
}
echo "<br>";
for ($x = 0; $x < $f; $x++) {
echo "1"." ";
for ($y = 1; $y <= $f-$x-1; $y++) {
$sum = 0;
for ($z = 0; $z <= $y; $z++) {
$sum = $sum + $previous_line[$z];
}
echo $sum." ";
}
echo "<br>";
}
But I get this output:
1 1 1 1 1 1 1 1 1 1 1
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7
1 2 3 4 5 6
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
What am I doing wrong?
I think you use same $previous_line[$] value for every line, so the the iteration value of $sum will increased constantly. (increased by 1)
You should update $previous_line[$] value on every line:
$previous_line[$y] = $sum;
and you don't need to use this iteration:
for ($z = 0; $z <= $y; $z++) {....}
This is full code:
<?php
$f = 10;
for ($x = 0; $x <= $f; $x++) {
echo "1"." ";
$previous_line[$x]=1;
}
echo "<br>";
for ($x = 0; $x < $f; $x++) {
$sum = 1;
echo $sum." ";
for ($y = 1; $y <= $f-$x-1; $y++) {
$sum = $sum + $previous_line[$y];
echo $sum." ";
$previous_line[$y] = $sum;
}
echo "<br>";
}
Just try it
Since Justin beat me to the punch, I though I would post an improved version.
Please note that there is probably better ways to do this.
I removed your first loop since it wasn't need, then I moved $previous_line inside the second loop and checked to make sure that it is being set. Last I update $currentSum and assign
$totalToLoop = 10;
for ($x = 0; $x <= $totalToLoop; $x++) {
$currentSum = 1;
echo '1 ';
for ($y = 1; $y <= ($totalToLoop - $x); $y++) {
if (!isset($previous_line[$y])) {
$previous_line[$y] = 0;
}
printf('%d ', $currentSum = ($currentSum + $previous_line[$y]));
$previous_line[$y] = $currentSum;
}
echo '<br>';
}
results.
1 1 1 1 1 1 1 1 1 1 1
1 2 3 4 5 6 7 8 9 10
1 3 6 10 15 21 28 36 45
1 4 10 20 35 56 84 120
1 5 15 35 70 126 210
1 6 21 56 126 252
1 7 28 84 210
1 8 36 120
1 9 45
1 10
1

How to use nested for() to get results intermittently and decreased?

I want this output:
1 1 1 1 1 1
2 2 2 3 3 3
4 4 5 5 6 6
7 8 9 10 11 12
I think I need to three nested For(), But I don't know how should I print the above result. Here is my code, How to complete it? (though I don't know, maybe my code is completely wrong)
for ($i=1; $i<=4; $i++) // row
{
for ($j=1; $j<=6; $j++) // column
{
for($z=1; $z<=12; $z++) // number
{
// what should be in here?
}
}
}
Edit: I want something like these examples: (Although these examples are very simple, what I want is a little more harder)
for ($i=1; $i<=4; $i++)
{
for ($j=1; $j<=6; $j++)
{
echo $i.' ';
}
echo '<br>';
}
1 1 1 1 1 1
2 2 2 2 2 2
3 3 3 3 3 3
4 4 4 4 4 4
Or this: echo $j;
1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6
Edit2:
Note: I need to a code that be able to print this either: (its logic is the same with first output)
4 4 4 4 4 4
5 5 5 6 6 6
7 7 8 8 9 9
10 11 12 13 14 15
You can try something like this.
var $c = 1;
for ($i=1; $i<=4; $i++)
{
var $noOfChanges = 6/$i;
for ($j=1; $j<=6; $j++)
{
echo $c.' ';
if($j%$noOfChanges==0){
$c = $c + 1;
}
}
echo '<br>';
}
Not tested.
You can intialize the var $c = 4; to get the next pattern.
Tested and working:
$length = 6;
$row = 0;
$number = 1;
$total = 0;
$n = $length;
while(true) {
$n = floor($length/($row+1));
for($i = 0; $i<$n; $i++) {
echo $number;
echo "\t";
}
$total+=$n;
if($total >= $length) {
$row++;
$total = 0;
echo "\n";
if($n == 1 ) break;
}
$number++;
}

Php for loop issue

Hi I'm Learning php and trying to do a for loop look like this wtih this code:
for($x=1; $x<=20; $x++){
echo $x;
$x = $x + 3; //5
echo "<br/>";
}
It's produce
1
5
9
13
14
But I want it should be...
1
5
10
15
20
for ($x = 0; $x <= 20; $x += 5) {
echo ($x == 0 ? 1 : $x), '<br>';
}
Or:
foreach (range(0, 20, 5) as $x) {
echo ($x == 0 ? 1 : $x), '<br>';
}
You can't produce the sequence without 1 extra condition because the delta differs in the first step:
1 + 4 ...
5 + 5
10 + 5
15 + 5
20 + 5
there are more than one solution.
one is:
for($x=1; $x<=20; $x++){
if(!($x % 5) || $x==1)
echo $x . "<br />";
}
Explination
% is the modulo operator. It returns the devision rest.
lets say $x is 3 than 3 % 5 would return 3 because the result 3/5 = 0 rest 3
if its $x is 10, it return 0. 10/5 = 2 rest 0
In the if-statement I use !-not operator. This turns around the result.
Because if takes 1+ (one and more) as true and 0- (zero and less) as false
So rest of 3 would be positiv (true) but in this case i want it to be false. So I turn arount the true/false with !
% - Modulo
R - Rest
1 % 5 = 0 R 1 // would say true to if
2 % 5 = 0 R 2 // would say true to if
3 % 5 = 0 R 3 // would say true to if
4 % 5 = 0 R 4 // would say true to if
5 % 5 = 1 R 0 // would say false to if
6 % 5 = 1 R 1 // would say true to if
and so on...
try this.
$x = 1;
for ($j = 1; $j <= 20; $j++) {
echo $x, "<br/>";
if ($x == 1) {
$x = $x + 4;
} else {
$x = $x + 5;
if($x > 20){break;}
}
}
if this answer is work for you please mark the answer.
Try this:
for($x=1; $x<=20; $x++) {
if($x%5==0 || $x==1) {
echo $x;
echo "<br>";
}
}

Alternating Designs For Loop Results

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.

Categories