I would like to loop 5 arrays in post.
What should be the right code to display:
$_POST["fp0"]
$_POST["fp1"]
..
$_POST["fp5"]
in a loop?
$x = 0;
while($x <= 5) {
$fp = ${'_POST["fp'.$x.'"]'};
echo $fp.'<br>';
$x++;
}
0 to 5 are six numbers, so you want to loop 6 elements in an array,
this is what you want
for( $i = 0; $i <= 5; $i++ ){
echo $_POST['fp'.$i]."<br>\n";
}
Try this:
$x = 0;
while($x <= 5) {
$fp = $_POST["fp$x"];
echo $fp.'<br>';
$x++;
}
You can do it simply by for loop, you form must POST values for
fp0, fp1 .... fp5
for($i = 0; $i < 5; $i++) {
$fp = $_POST['fp'.$i];
echo $fp . "<br>";
}
Related
I need your help to solve this problem, I am trying to write the necessary code so that the output is as follows:
PHP! PHP!! PHP!!! PHP!!!! PHP!!!!!
<?php
$y = ['!'];
for($i = 1; $i <= 5; $i++)
{
print "PHP!"."$y\t";
if($y+1 <5)
$y="!";
$y+'!';
}
?>
Until the second word, it adds one more exclamation point and in the rest of the repetitions it has the same number, that is two exclamation points.
How can I solve this problem?
Try this:
<?php
for ($i = 0; $i < 5; $i++) {
echo "PHP".str_repeat ("!", $i);
}
?>
You can try it:
<?php
for($i = 1; $i <= 5; $i++)
{
echo "PHP";
for($j = 1; $j <= $i; $j++){
echo "!";
}
}
?>
You can also use str_repeat():
<?php
for ($i = 1; $i <= 5; $i++) {
echo "PHP".str_repeat ("!", $i);
}
?>
Output will be: PHP!PHP!!PHP!!!PHP!!!!PHP!!!!!
Why this piece of code works when it is clearly wrong in the second for loop (for ($i==0; $i<$parts; $i++) {)?
Does php allows for multiple comparisons inside for loops?
function split_integer ($num,$parts) {
$value = 0;
$i = 0;
$result = [];
$modulus = $num%$parts;
if ($modulus == 0) {
for($i = 0; $i < $parts; $i++)
{
$value = $num/$parts;
$result[] = $value;
}
} else {
$valueMod = $parts - ($num % $parts);
$value = $num/$parts;
for ($i==0; $i<$parts; $i++) {
if ($i >= $valueMod) {
$result[] = floor($value+1);
} else {
$result[] = floor($value);
}
}
}
return $result;
}
Code for ($i==0; $i < $parts; $i++) runs because $i==0 has no impact on loop.
In normal for loop first statement just sets $i or any other counter's initial value. As you already set $i to 0 earlier, your loop runs from $i = 0 until second statement $i < $parts is not true.
Going further, you can even omit first statement:
$i = 0;
for (; $i < 3; $i++) {
echo $i;
}
And loop will still run 3 times from 0 to 2.
$big_box = array(1,2,3,16,17,18,31,32,33,46,47,48,61,62,63,76,77,78,91,92,93,106,107,108,121,122,123,136,137,138............);
$small_box = array(4,19,34,49,64.............);
I want to generate dynamic numbers array like above example upto 10000.
For Array $big_box Try to something like this.
$j = 0;
for ($i=0; $i <= 1000; $i++) {
if ($i!=0) {
$big_box[] = $i;
$j++;
}
if ($j == 3) {
$j = 1;
$big_box[] = $i+13;
$i =$i+13;
}
}
echo "<pre>";
print_r($big_box);
And array $small_box
$small_box = array();
for($i=0; $i <= 1000; $i++) {
if ($i == 0) {
$small_box[$i] = 4;
}else{
$small_box[$i] = $small_box[$i-1]+15;
}
}
echo "<pre>";
print_r($small_box);
I'm trying to output my array results in groups of 4.
<?php for ($i = 0; $i < 4; ++$i) { ?>
<div>
// code
</div>
<?php } ?>
The above does 4, but obviously doesn't re-loop.
You can loop whole array and group you output with help of "%" operator.
<div>
<?php for ($i = 0; $i < count($array); $i++) {
if (($i % 4) == 0) {
echo "</div><div>";
}
echo "Element " . $array[$i]; // CODE
}
</div>
Other than using Mod as the other answers show, you could use array_chunk() to create the groups:
$groups = array_chunk($original_array, 4);
foreach($groups as $group){
echo '<div>';
foreach($group as $item){
echo $item;
}
echo '</div>';
}
You can use a while loop to reloop for the whole results to be printed
<?php while(conditions) {
for ($i = 0; $i < 4; ++$i) { ?>
<div>
// code
</div>
<?php } } ?>
Try this that way you can jump by 4
for ($i = 0; $i < 20; $i = $i+4) {
echo $i.'<br/>';
}
I would use a foreach and then just throw in an extra check to output the divs.
$i=0;
foreach ($array as $key->$val)
{
if($i%3==0)
{
echo "<div>";
}
// your stuff
if($i%3==0)
{
echo "</div>";
}
$i++;
}
array_slice() returns the sequence of elements from the array array as specified by the offset and length parameters.
you can check out from here http://php.net/manual/en/function.array-slice.php
try this, use nested for loop, this will loop 4 times. You can try to integrate with your code. If
for ($i = 0; $i < 4; $i++){
for($j = 0; $j < 4; $j++){
echo $a[$j++];
}
echo "<br/>";
}
I hope it can help you.
you can try $i++, because you use ++$i in this way "for" works 3 times!
for ($i = 0; $i < 4; $i++)
i am facing a problem
can some one suggest me
for ($i = 1; $i <= 2; $i++) {
$r2 = 0;
for ($t = 1; $t <= 2; $t++) {
echo $r2;
$r2++
}
}
output is 0101;
can i get output 0123 ??? please
if
for ($i = 1; $i <= 3; $i++) {
$r2 = 0;
for ($t = 1; $t <= 3; $t++) {
echo $r2;
$r2++
}
}
output is 010101;
can output 012345678 ??? please
and if
for ($i = 1; $i <= 4; $i++) {
$r2 = 0;
for ($t = 1; $t <= 4; $t++) {
echo $r2;
$r2++
}
}
output is 01010101;
can output 0123456789101112131415 ??? please
i think you understand
thanks
In all of these cases you are initializing $r2=0; in the inner loop. It should be outside the loop.
$r2=0;
for($i=1;$i<=2;$i++){
for($t=1;$t<=2;$t++){
echo $r2;
$r2++
}
}
This would produce "1234".
why are you using two nested for loops ?
why not just use one:
for ($i=0; $i<=15; $i++) echo $i . " ";
Try this:
$r2 = 10;
for($t = 0; $t <= $r2; $t++){
echo $r2;
}
Oh wait.. I get it now, why you have the two nested loops, you want to essentially raise a number to the power of 2 in order to control the number of values output. In that case, what you want is simply this:
// this is the variable you need to change to affect the number of values outputed
$n = 2;
// Square $n
$m = $n * $n;
// Loop $m times
for ($i = 0; $i < $m; $i++) {
echo $i;
}