get value 1 - 2 - 1 - 2 - 1 in for loop php - php

I am trying to get value like 1-2-1-2-1-2 from for loop
I tried code like this
$c = count($response['video']); // 2
$currentPage = 2;
$k = 1;
for ($i=$k; $i <= $currentPage ; $i++) {
$b = $i > $c ? $k : $i;
$urls = $response['video'][$b - 1];
echo "$b";
}
where $currentPage = 5; $c =2;
when $c = 3; the value should be 1-2-3-1-2-3
when i echo $b or dd($b) out of loop i will got 12111 but i need answer 121212
please help me to solve this.

Is this what you want?
$k = 1;
$currentPage = 5;
$c = 3;
for ($b=$i=$k; $i <= $currentPage ; $i++, $b++) {
$b = $b <= $c ? $b : 1;
echo $b;
}

Related

php echo strings for multiple times with order

for ($i = 1; $i < 3; $i++) {
$a = 11;
$b = 22;
$c = $a.'<br/>' $b .'<br/>';
echo $c.'<br/>';
}
The result that I am having now is:
11
22
11
22
How can I make it become like the result below with the use of $c:
11
11
22
22
Any help will be much appreciated!
No need to use variables $a and $b. Simply do this:
for ($i = 1; $i <3 ; $i++){
$c = $i.$i;
echo $c;
}
Update(1): Based on your edited question, the solution would be like this:
for ($i = 2; $i < 6 ; $i++){
$j = (int)($i/2);
$c = $j.$j;
echo $c . '<br />';
}
for ($i = 1; $i <3 ; $i++)
{
$a = $i;
$b = $i;
$c = $a. $b;
echo $c;
}
This might work !!

PHP add to variable name

Is it possible to add or concatenate something into a variable name in a PHP variable? For example:
for($g = 7; $g <= 10; $g++){
for($i = 0; $i <= 4; $i++){
$counter = $g - 7;
if($i != $counter){
continue;
} else {
$grade.[$g] = $grades[$i];
}
}
}
I want this to happen:
$grade7 = 0
$grade8 = 1
$grade9 = 2
$grade10 = 3
Concatenates the $g with grade and make this value a variable by adding a $ sign at the starting line...
The example given below:
for($g = 7; $g <= 10; $g++){
for($i = 0; $i <= 4; $i++){
$counter = $g - 7;
if($i != $counter){
continue;
} else {
${"grade".$g} = $grades[$i];
}
}
}
echo $grade7; // 0
echo $grade8; // 1
echo $grade9; // 2
echo $grade10;// 3
One solution would be to create the variables dynamically:
for ($g = 7; $g <= 10; $g++) {
for ($i = 0; $i <= 4; $i++) {
$counter = $g - 7;
if ($i == $counter) {
${'grade' . $g} = $grades[$i];
}
}
}
You should use ARRAY instead of your method. :)
Try to look at variable named like "$$".
EDIT: Maybe something like
var $array = array();
for($g=7; $g<=10; $g++)
{
for($i=0; $i<=4; $i++)
{
$counter = $g - 7;
if($i != $counter) continue;
else $array[$grade.[$g]] = $grades[$i];
}
}
$data = new Array();
for($g = 7; $g <= 10; $g++){
for($i = 0; $i <= 4; $i++){
$counter = $g - 7;
if($i != $counter){
continue;
} else {
$data[$grade.[$g]] = $grades[$i]);
}
}
}

PHP for loop with exception rules

I am searching for a php for loop, which should output
22,23,24, 32,33,34, and so on 92,93,94 and all following like 102,103,104,122,123 until 902,903,904,992,993,9949, but not or 12,13,14... 112,13,114 ...912,913,914.
my code:
for ($a = 22; $a <= 1000; $a+=10) {
$b = $a + 1;
$c = $a + 2;
echo "$a <br>";
echo "$b <br>";
echo "$c <br>";
}
I need here an exception, because 112,123 and 124 and so on until 912,913,914 shouldn't be echoed.
All results should be stored in an array.
Try using this code:
$data = array();
for ($a = 2; $a < 10; $a++) {
for ($b = 2; $b <= 4; $b++) {
$c = ($a * 10) + $b;
$data[] = $c;
}
}
I have found all parts do finish kwestgrounds answer with my exceptions.
$delete = array_merge(range (112, 912, 100), range (113, 913, 100),range (114, 914, 100));
$data = array();
for ($a = 2; $a < 100; $a++) {
for ($b = 2; $b <= 4; $b++) {
$c = ($a * 10) + $b;
{
if (in_array($c, $delete)) continue;
}
$data[] = $c;
}
}

store variables in an mutidimensional array?

i tried to store variables which are set in a while loop in a multi dimensional arrays. Afterwarts i want to print the array out.
what i did:
$counter = 0;
while($counter < 10){
$a = $counter + 10;
$b = $counter + 5;
$file_ar[] = array($a,$b);
$counter++;
}
/* $file_ar[1-10] = "$a","$b" */
$i = 0;
while(isset($file_ar[$i])) {
$a = $file_ar[$i][0];
$b = $file_ar[$i][1];
echo $a.' is not '.$b;
}
When i run this code i will not get anything.
What is the reason for this?
Thank you!
Here is code-
<?php
$counter = 0;
while($counter < 10){
$a = $counter + 10;
$b = $counter + 5;
$file_ar[] = array($a,$b);
$counter++;
}
/* $file_ar[1-10] = "$a","$b" */
$i = 0;
while(isset($file_ar[$i])) {
$a = $file_ar[$i][0];
$b = $file_ar[$i][1];
echo $a.' is not '.$b;
$i++;
}
You need to add the index of the array you are adding to or you are just writing over it.
$counter = 0;
while($counter < 10){
$a = $counter + 10;
$b = $counter + 5;
$file_ar[$counter] = array($a,$b);
$counter++;
}
$i = 0;
while(isset($file_ar[$i])) {
$a = $file_ar[$i][0];
$b = $file_ar[$i][1];
if ($a != $b)
echo $a.' is not '.$b;
else
echo $a.'='.$b;
$i++;
}

print value of a variable that is composed for a string into a For Loop

In this loop, I'm trying to take the value of the variable, but to save code I want to use a For Loop to print it concatenating part of the variable with a number genereted into the loop. This is my Try.
<?php
$x0 = 0;
$x1 = 1;
$x2 = 2;
$x3 = 3;
for ($i=0; $i < 5; $i++) {
echo '$x'.$i;
}
?>
the result that I'm geting is
$x0$x1$x2$x3$x4
I want it to end up like this:
0123
Its supposed to be:
for ($i=0; $i < 5; $i++) {
echo ${"x$i"};
}
Sidenote: You'll have to define $x4 or terminate it to < 4 so you won't get a undefined index.
Try this :
$x0 = 0;
$x1 = 1;
$x2 = 2;
$x3 = 3;
for ($i=0; $i < 5; $i++) {
$y='x'.$i;
if(isset($$y)){
echo $$y;
}
}
Try this Code:
<?php
$x0 = 0;
$x1 = 1;
$x2 = 2;
$x3 = 3;
$string = '';
for ($i=0; $i < 5; $i++) {
$string .= $i;
}
echo $string;
?>

Categories