FOR Loop link generating by limits - php

How to generate links? For example I want to create 10 links starting from page/5 and generate 10 link dynamically - it seems skip first 5 and generate from link 5 to 15 e.g.
page/5, page/6, page/7, to page/15
$numOfPages = 10;
$startfrom = 5;
$pages = [];
for ($i = $startfrom; $i <= $numOfPages; $i++) {
$pages[] = 'page/' . $i;
}
Not working if work if $startfrom grater than $numOfPages

$startfrom is an offset, so you need to loop from that offset to the offset+the number of pages you want to have:
for ($i = $startfrom; $i < $startfrom+$numOfPages; $i++) {
Of you can do the addition inside the loop:
for ($i = 1; $i <= $numOfPages; $i++) {
$pages[] = 'page/' . ($i + $startfrom)
}
But since you probably have a maximum number of pages, which may not be a multiple of 5 of 10, I'd introduce an extra variable and determine the end of the loop before actually looping (using min()). That way, the loop itself and the code in it is nice and simple:
$totalPages = 28;
$pagesToShow = 10;
$startFrom = 5;
$loopTo = min($startFrom + $pagesToShow - 1, $totalpages);
for ($i = $startFrom; $i <= $loopTo; $i++) {
$pages[] = 'page/' . $i;
}

Related

For loop (php) that results in this: 12345678910987654321

My niece is trying to create one for-loop (php), that results in this:
* 12345678910987654321
example for loop she tried:
for ($i = 1; $i <= 10; $i++ , $i = 10; $i <= 1; $i--) {
echo $i . ' ';
}
She can only use if's and elseif's. I'm not a programmer and can't really help her. Any ideas how this could be achieved in php?
Any information would be greatly appreciated.
The key is to add a variable instead of a number, then reverse that number when $i hits 10.
for($i = 1, $j = 1; $i> 0; $i+=$j) // Start i at 1, and j at 1
{
echo $i;
if($i == 10)
$j = -1; // i has hit 10, so use -1 to start subtracting
}
Another possibility is to loop up to 20, printing $i for the ascending part and 20 - $i for the descending.
for ($i = 1; $i < 20; $i++) {
if ($i <= 10) {
echo $i;
} else {
echo 20 - $i;
}
}

Loop through array to assign value for every nth item

I have an array containing a range of numbers 1-100:
$range = range(1, 100);
I want to loop through and assign each a value of 1-24. So 1=1, 2=2, 24=24, but then also 25=1, 26=2, 27=3, etc...
How can I loop through $range and apply said values to each number?
Note: I would preferably like to use a forloop, but will take any valid answer.
The modulo operator (%) is the answer
$range = range(1, 100);
$rangeValues = array();
for ($i = 0; $i < count($range); $i++){
// using modulo 25 returns values from 0-24, but you want 1-25 so I use ($i % 24) +1 instead which gives 1-24
$rangeValues[$range[$i]] = ($i % 24) +1;
}
Try : php modulo operator (%).
//example loop
$range = range(1, 100);
$yourIndex = array();
for ($i = 0; $i < count($range); $i++){
//$yourIndex will reset to 1 after each 25 counts in $range
$yourIndex[$range[$i]] = ($i + 1) % 25;
}
$range = range(1, 100);
$offset = 1;
$limit = 24;
for($i = 0; $i < count($range); $i++)
{
$range[$i] = $offset+($i%$limit);
}
var_dump($range);
For getting your required solution, you can also use the below code -
$range = range(1, 100);
for($i=0; $i<100; $i++){
if($i < 24){
echo $range[$i].' = '.($i+1);echo "<br>";
}else if($i < 48){
echo $range[$i].' = '.($i-23);echo "<br>";
}else if($i < 72){
echo $range[$i].' = '.($i-47);echo "<br>";
}else if($i < 96){
echo $range[$i].' = '.($i-71);echo "<br>";
}else{
echo $range[$i].' = '.($i-95);echo "<br>";
}
}

PHP for loop within for loop with different incrementing values

I am new to PHP and for loops but I am having trouble racking my brain around the math for this. I am trying to write a loop that will create an array with 49 items. The items have two incrementing values within them. The 49 items are below:
M1s1t1url
M1s1t2url
M1s1t3url
M1s1t4url
M1s1t5url
M1s1t6url
M1s1t7url
M1s2t1url
M1s2t2url
M1s2t3url
M1s2t4url
M1s2t5url
M1s2t6url
M1s2t7url
M1s3t1url
M1s3t2url
M1s3t3url
M1s3t4url
M1s3t5url
M1s3t6url
M1s3t7url
M1s4t1url
M1s4t2url
M1s4t3url
M1s4t4url
M1s4t5url
M1s4t6url
M1s4t7url
M1s5t1url
M1s5t2url
M1s5t3url
M1s5t4url
M1s5t5url
M1s5t6url
M1s5t7url
M1s6t1url
M1s6t2url
M1s6t3url
M1s6t4url
M1s6t5url
M1s6t6url
M1s6t7url
M1s7t1url
M1s7t2url
M1s7t3url
M1s7t4url
M1s7t5url
M1s7t6url
M1s7t7url
As you can see there are three numbers in each item. The first number is a constant. The second number counts up to 7 then resets back to 1. The third number adds 1 every time the second number resets back to 1. Here is what I have below but I know I am off on the calculations.
for ($i = 1; $i < 8; $i = $i + 1) {
for ($u = 1; $u < 8; $u = $u + 1) {
$urln[] = 'M1s'.[$u].'t'.[$i].'url';
}
}
I am getting an array to string error.
<?php
for ($i = 1; $i < 8; $i++) {
for ($u = 1; $u < 8; $u++) {
$urln[] = 'M1s' . $u . 't' . $i . 'url';
}
}
<?php
$urln = array();
for ($i = 1; $i < 8; $i++) {
for ($u = 1; $u < 8; $u++) {
$urln[] = 'M1s' .$i. 't'. $u .'url';
}
}
foreach ($urln as $i) {
echo "$i\n";
}
?>

How can i make the for loop parameters dynamic

for ($i=40; $i>=30; $i--) //code will display data for top x row
for ($i=1; $i<=9; $i++) //code will display data for left y column
for ($i=29; $i>=21; $i--) //code will display data for bottom x row
for ($i=30; $i>=39; $i++) //code will display data for right y column
These 4 loops all do the same thing.
In my index.php im using "include" to get the 4 loops that are in 4 different files.
How can I make the for loop dynamic?
Algoritihm:
$i = (40,1,29,30) <--will be any of those 4
$maxlow = (30,9,21,39)
$check =(>,<) <--value depends on whether $i > or < $maxlow
$icrement = (--,++) <-- if $check is > then decrease, otherwise increment
for ($i; $i($check)=$maxlow; $i($increment) <---what i am trying to do
// $step is either 1 (incrementing) or -1 (decrementing)
foreach (range($begin, $maxlow, $step) as $i) {
}
Settings for given loop:
$diff = -1;
$start = 40;
$stop = 30 + $diff;
The loop itself, always like this:
for ($i = $start; $i != $stop; $i += $diff)
$low = [40,1,29,30](rand(0,3);
$high = [30,9,21,39](rand(0,3);
$modifier = ($low > $high) ? -1 : 1;
for($i = $low; ($i * $modifier) < ($high * $modifier); $i += $modifier)
{
doStuff();
}
why not just use for with switch inside, like this:
for ($i=1; $i >=39; $i++) {
switch($i) {
case ($i>=1 && $i<=9):
break;
...
...
case ($i>40):
//do something
break;
}
}
It will be much more readable, and easier to understand/edit in future.

For loop if statement issue

I have a for loop and inside of it I have a if statement something like this:
$output = "";
$limit = 550;
for ($i = 1; $i <= $limit; $i++) {
if($i < 10){
$output .= my_function($i*1);
}elseif($i < 20){
$output .= my_function($i*2);
}elseif($i < 30){
$output .= my_function($i*3);
}
//elseif 30 => 550
}
Problem is I find it very tedious to have to continue this elseif statement down to 550. Is there any way to do this without writing 55 elseif statements.
Your thinking is exactly right. Having to repeat that much code is a sure sign that something is off.
In this case, the solution is pretty simple. You just want to use integer division to knock off the ones digit, which you don't care about. You also need to adjust by one since you're checking less than and not less than or equal.
$output = "";
$limit = 550;
for ($i = 1; $i <= $limit; $i++) {
$temp = (int) ($i / 10) + 1;
$output = my_function($i*$temp);
}
for ($i = 1;$i <= 550;$i++) {
$multiplier = (int) ($i / 10) + 1;
$output = my_function($i * $multiplier);
}
My first thought is that you could use:
$output = "";
$c = 0;
$limit = 550;
for ($i = 1; $i <= $limit; $i++) {
if (($i + 1) % 10 == 0) {
$c++;
$output = my_function($i*$c);
}
}
How about
$output = "";
$limit = 550;
for ($i = 1; $i <= $limit; $i++) {
$num = ($i / 10) + 1
$output = my_function($i*$num);
}
Try this:
$output = "";
$limit = 550;
for ($i = 1; $i <= $limit; $i++)
{
$output = my_function($i * (1 + (int)($i / 10)));
}
The solution should be as simple as the following:
$output = "";
$limit = 550;
for ($i = 1; $i <= $limit; $i++)
{
$num = ceil($i/10);
$output = my_function($num, $num-1);
}
hmm the original code seems to have changed since I started typing this. The my_function function now only has 1 input when it originally had 2.
for ($i = 1; $i <= $limit; $i++) {
$output = my_function(i*floor($i/10+1));
}
Point is not really about this question:
You reassign output on every loop iteration, but only last will be vailable after loop? Need you $output[]= or $output.= ?

Categories