Can anyone explain me the working of multiple nested loops with some suitable example in php?
Actually, i know that how the loop inside another loop works but i don't know about how the loop inside the loop of another loop works.
For example i am trying to understand the code given below but unable to understand it's working.
<?php
for($i=5;$i>=1;$i--){
for($k=6;$k>=$i;$k--){
echo " ";
}
for($j=1;$j<=$i;$j++){
echo "* ";
}
echo "<br>";
}
Try to debug the flow yourself:
The main loop $i will looping for 5 times (5 to 1). For each $i the inner first loop $k will looping for various times depending on the condition and same for the next loop. Check the Result and debug yourself.
for($i = 5; $i >= 1; $i--){
echo '$i with $k Started:<br/>';
for($k = 6; $k >= $i; $k--){
echo $i." - ".$k."<br/>";
}
echo '<br/><br/>';
echo '$i with $j Started:<br/>';
for($j = 1; $j <= $i; $j++){
echo $i." - ".$j."<br/>";
}
echo "<br/>";
}
Result:
$i with $k Started:
5 - 6
5 - 5
$i with $j Started:
5 - 1
5 - 2
5 - 3
5 - 4
5 - 5
$i with $k Started:
4 - 6
4 - 5
4 - 4
$i with $j Started:
4 - 1
4 - 2
4 - 3
4 - 4
$i with $k Started:
3 - 6
3 - 5
3 - 4
3 - 3
$i with $j Started:
3 - 1
3 - 2
3 - 3
$i with $k Started:
2 - 6
2 - 5
2 - 4
2 - 3
2 - 2
$i with $j Started:
2 - 1
2 - 2
$i with $k Started:
1 - 6
1 - 5
1 - 4
1 - 3
1 - 2
1 - 1
$i with $j Started:
1 - 1
for($i=5;$i>=1;$i--){ // means this loop will start from i=5 and run till i >= 1
for($k=6;$k>=$i;$k--){ // now this loop will starts from 6 and run till k > i ex. in first loop k will run for k=6 and k=5
echo " ";
}
for($j=1;$j<=$i;$j++){ // this loop will starts from 1 and run till j <= i value
echo "* ";
}
echo "<br>";
}
Output:
i = 5
k=6 " ";
k=5 " ";
k = 4 // now terminate as k >= i false
j = 1 "* ";
j = 2 "* ";
j = 3 "* ";
j = 4 "* ";
j = 5 "* ";
j = 6 // now terminates as j <= i false
i = 4
so on .....
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'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
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++;
}
Output:
3
3 4
3 4 5
3 4 5 6
3 4 5 6 7
3 4 5 6 7 8
function Triangle ($begin, $end) {
if ($begin < 0 || $end < 0) {
return;
}
if ($begin == $end) {
return $a;
}
else {
// non recursive
for ($i = 1; $i <= $end; $i++) {
for ($j = $begin; $j <= $i; $j++) {
echo $j . " ";
}
echo "<br>";
}
}
}
This is what I made so far.
Here's one way:
function triangle ($begin, $end, $row = 1) {
//stop when we've printed up to end
if($end - $begin + 1 < $row) return;
//let's start at the beginning :)
for($i = 0; $i < $row; $i++){
//the row number increments each time so we can keep adding.
echo ($begin + $i)." ";
}
echo "<br>";
//now recurse...
triangle($begin, $end, $row + 1);
}
Usage:
triangle(3,9);
Output:
3
3 4
3 4 5
3 4 5 6
3 4 5 6 7
3 4 5 6 7 8
3 4 5 6 7 8 9
This should work for you:
(Here I just added the variable step which defines how many steps you make from $begin to $end and if $begin + $step == $end the function is done. If not It starts from $begin and makes X steps and as long as it doesn't reach the end I call the function again with a step more)
<?php
function Triangle($begin, $end, $step = 0) {
for($count = $begin; $count <= ($begin+$step); $count++)
echo "$count ";
echo "<br />";
if(($begin + $step) == $end)
return;
else
Triangle($begin, $end, ++$step);
}
Triangle(3, 8);
?>
output:
3
3 4
3 4 5
3 4 5 6
3 4 5 6 7
3 4 5 6 7 8
.
//Prev
.
for($number = 1; $number <= $num_pages; $number++)
{
if($page == $number)
{
$navigator .= "<b>[$number]</b> ";
}
else
{
$navigator .= "<a href='?c=".$_SESSION['cID']".&rows=".$per_page."&page=$number'>$number</a> ";
}
}
.
//Next
.
This is the snippet that prints number of pages.
Sample output:
Previous 1 2 3 4 [5] 6 7 8 9 10 Next
5 is the current page.
Problem: page numbers are shown in sequence with no restrictions. If i have 100 pages, all numbers show up.
Question: I need my paging numbers appear as the following...
Assume we only have 7 ($num_pages) pages:
Previous 1 2 [3] 4 5 6 7 Next
Assume we have 90 pages:
[1] 2 3 4 5 6 7 ... 90 Next
Assume user clicked the 7th page:
Previous 1 ... 5 6 [7] 8 9 10 11 ... 90 Next
Assume user clicked 11th page:
Previous 1 ... 9 10 [11] 12 13 14 15 ... 90 Next
Assume user clicked 15th page:
Previous 1 ... 13 14 [15] 16 17 18 19 ... 90 Next
Assume user clicked 90th page:
Previous 1 ... 84 85 86 87 88 89 [90]
Any help will be appreciated.
$radius = 3;
for($i = 1; $i <= $total; $i++){
if(($i >= 1 && $i <= $radius) || ($i > $current - $radius && $i < $current + $radius) || ($i <= $total && $i > $total - $radius)){
if($i == $current) echo "<b>".$i."</b>";
}
elseif($i == $current - $radius || $i == $current + $radius) {
echo "... ";
}
}
This should be more than enough to get you started at least
$count = 7; // number to show
// start at half threshold down from the current location.
$number = $current - round($count/2);
if( $number > 1 ) echo '...';
else $ // increase to have number start at 1.
for( $number; $number < $number + $count; $number++)
{
// your for loop as normal
}
if( $number < $total ) echo '...';
An elegant solution for this kind of thing is to use "logarithmic page navigation". See my answer to this question (PHP code included):
How to do page navigation for many, many pages? Logarithmic page navigation