php for loop in for loop - php

I have a problem trying using for loop inside a loop i can't get the result i want
<?php
for($sg = 1; $sg <= 11; $sg++){
echo "<b>".echo $sg;."</b>";
for($g = 1; $g <= 11; $g++){
echo "<p>".echo $g."</p>";
}
?>
I looking the result to be like
1
1 2 3 4 5 6 7 8 9 10 11
2
12 13 14 15 16 17 18 19 20
3
21 22 23 24 25 26 27 28 29 30
continuous ....

<?php
for($sg = 1; $sg <= 11; $sg++){
echo "<br /> <b>". $sg."</b> ";
for($g = 1+(10*($sg-1)); $g <= (10*($sg-1))+10; $g++){
echo "". $g." ";
}
}
?>
Output:
1 1 2 3 4 5 6 7 8 9 10
2 11 12 13 14 15 16 17 18 19 20
3 21 22 23 24 25 26 27 28 29 30
4 31 32 33 34 35 36 37 38 39 40
5 41 42 43 44 45 46 47 48 49 50
6 51 52 53 54 55 56 57 58 59 60
7 61 62 63 64 65 66 67 68 69 70
8 71 72 73 74 75 76 77 78 79 80
9 81 82 83 84 85 86 87 88 89 90
10 91 92 93 94 95 96 97 98 99 100
11 101 102 103 104 105 106 107 108 109 110

<?php
$inner_start = 1;
$inner_max = 11;
for($sg = 1; $sg <= 11; $sg++){
echo "<b>".$sg."</b></br>";
echo "<p>";
for($g = $inner_start; $g <= $inner_max; $g++){
echo $g . ' ';
}
$inner_start = $inner_start + 10;
$inner_max += 10;
echo "</p></br>";
}
?>

Related

Upside down PHP array values?

I have array echo this: (2 5 7 13 19 22 23 37 41 41 64 74 85 96 139);
But I need this echo: (139 96 85 74 64 41 41 37 23 22 19 13 7 5 2);
I can't find, how to upside down echo value?
$num= array(7,13,85,64,2,41,22,96,139,37,41,19,74,23,5);
$max= max($num);
$a= count($num);
sort($num);
for ($x=0; $x < $a; $x++) {
echo $num[$x]. " ";
}
output: 2 5 7 13 19 22 23 37 41 41 64 74 85 96 139
Replace sort($num); with rsort($num);.
sort() sorts from low to high.
rsort() sorts from high to low.
use array rsort()
$output=rsort($num);
If array is already sorted, then use array_reverse. It is more performant than sorting array in descending order.
$reversedOrderArray = array_reverse($yourArray);

IF Statement Looping Php

I have a problem with that script using the simulation calculation:
$no3=+1;
for ($i=0; $i<36; $i++)
{
$ No = + 1;
for ($ i = 0; $ i <36; $ i ++) {
enter code here
$ No3 ++;
}
Example :
No GradeA GradeB Total
1 5 5 10
2 10 5 15
3 15 5 20
4 20 5 25
5 25 5 30
6 30 5 35
7 35 5 40
8 40 5 45
9 45 5 50
10 50 5 55
11 55 5 60
12 60 5 65
13 65 5 70
14 70 10 80
15 80 10 90
16 90 10 100
17 100 10 110
18 110 10 120
19 120 10 130
20 130 10 140
21 140 10 150
22 150 10 160
23 160 10 170
24 170 10 180
25 180 10 190
26 190 20 210
27 210 20 230
28 230 20 250
29 250 20 270
30 270 20 290
31 290 20 310
32 310 20 330
33 330 20 350
34 350 20 370
35 370 20 390
36 380 20 410
$no3++;
}
how do I make the numbers 12,24 and 36 there is the text
Try this:
this is answer for your secound question to
<?php
$two = 5;
$three = 5;
$four = 10;
$mod = 14;
for ($i = 1; $i <=36; $i ++) {
echo $i."\t".$two."\t".$three."\t".$four."\n";
if($i % $mod == 0){
$three *= 2;
}
$two += $three;
$four += $three;
}
?>

Is there a way to remove two numbers not affecting others?

The code is giving the following output and each time, it will give a different output.
Is there any way to remove to 2 numbers for example 15 and 60, so that other numbers are not effected they remain the same?
I have tried but could not get the desired result.
Any help or any idea how to do this?
<?php
$arr = array();
for ($i = 1; $i < 82; $i++) {
$arr[] = $i;
}
shuffle($arr);
$lines = array_chunk($arr, 9);
echo "<table>";
foreach ($lines as $key => $line) {
echo "<tr>";
for ($i = 0; $i < sizeof($line); $i++) {
echo "<td align='right'>" . $line[$i] . "</td>";
}
echo "</tr>";
}
echo "</table>";
?>
Out put
52 67 5 44 76 49 1 27 28
73 33 19 66 4 14 63 45 62
26 75 50 80 70 38 12 54 78
9 69 36 32 2 7 56 11 51
40 20 22 15 60 65 31 41 77
57 29 34 79 68 23 18 71 39
42 10 72 17 81 30 35 48 47
37 59 53 6 3 55 13 46 58
61 25 24 64 74 43 21 8 16
This should work for you:
Just place the code before your shuffle() call. Basically first we create an array of patterns, by looping through all numbers, which we want to extract from the list, with array_map().
The pattern basically has two anchors to match the numbers fully in your array and not only partially (E.g. 2 will not match 24, but it will match 2).
With the created pattern array we just use preg_replace() to replace the numbers in your array with an empty string.
$numbers = [15, 60];
$pattern = array_map(function($v){
return "/^$v$/";
}, $numbers);
$arr = preg_replace($pattern, "", $arr);

Can't print 9x9 table correctly

The code is giving first 10 numbers in first row and 9 numbers in other 7 rows and 8 numbers in last row.
How can I obtain the 9x9 matrix so that all the rows have 9 numbers?
I have tried everything, but nothing works. Is there any way to do this?
<table border=1>
<tr>
<?php
for ($i = 1; $i < 82; $i++) {
$arr[] = $i;
}
for ($i = 0; $i < 81; $i++) {
echo '<td>' . $arr[$i] . '</td>';
if ($i % 9 == 0 && $i != 0) {
echo "</tr><tr>";
}
}
?>
</tr>
</table>
The best is what #Rizier says, but if you want to modify only your code then:-
<table border=1>
<tr>
<?php
for ($i = 1; $i < 82; $i++) {
$arr[] = $i;
}
$j=1; //add a new count starts from 1
for ($i=0; $i<81; $i++)
{
echo '<td>'.$arr[$i].'</td>';
if ($j%9==0) // check counter modules 9 will be zero or not. it will break after each 9 iteration.
{
echo "</tr><tr>";
}
$j++;} // increase the value of counter
?>
</tr>
</table>
Output:-http://prntscr.com/7bu34m
This should work for you:
Here I first create an array with 81 elements with range(). Then I array_chunk() the array into a 2-dimensional array, where each sub array has 9 elements.
At the end just loop through all sub array and implode() them into one single row.
<table border=1>
<?php
$arr = range(1, 81);
$arr = array_chunk($arr, 9);
foreach($arr as $v)
echo "<tr><td>" . implode("</td><td>", $v) . "</td></tr>";
?>
</table>
output:
1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18
19 20 21 22 23 24 25 26 27
28 29 30 31 32 33 34 35 36
37 38 39 40 41 42 43 44 45
46 47 48 49 50 51 52 53 54
55 56 57 58 59 60 61 62 63
64 65 66 67 68 69 70 71 72
73 74 75 76 77 78 79 80 81
you are starting the $i=0; so the first condition was true it was placed the </tr> after first result
<table border=1>
<tr>
<?php
for ($i = 1; $i < 82; $i++) {
$arr[] = $i;
}
$j=1;
for ($i=0; $i<81; $i++)
{
echo '<td>'.$arr[$i].'</td>';
if ($j%9==0)
{
echo "</tr><tr>";
}
$j++;}
?>
</tr>
</table>
output
1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18
19 20 21 22 23 24 25 26 27
28 29 30 31 32 33 34 35 36
37 38 39 40 41 42 43 44 45
46 47 48 49 50 51 52 53 54
55 56 57 58 59 60 61 62 63
64 65 66 67 68 69 70 71 72
73 74 75 76 77 78 79 80 81
try this
for ($i=1; $i<=81; $i++)
{
echo '<td>'.$arr[$i].'</td>';
if ($i%9==0 && $i!=1)
{
echo "</tr><tr>";
}
}
Your code is almost current ... Please echo ''.$arr[$i].''; line below the if condition like this...
<table border=1>
<tr>
<?php
for ($i = 1; $i < 82; $i++) {
$arr[] = $i;
}
for ($i=0; $i<81; $i++)
{
if ($i%9==0 && $i!=0)
{
echo "</tr><tr>";
}
echo '<td>'.$arr[$i].'</td>';
}
?>
</tr>
</table>

How to show these numbers in tabular format?

This code is giving the following output, I want to show these numbers in table is there any way to do this? and why it is not showing the numbers in proper format like why 53 is not exactly below 40 and others also not showing in proper order?
<?php
$arr = array();
for ($i=1;$i<82;$i++) {
$arr[] = $i;
}
shuffle($arr);
$lines = array_chunk($arr, 9);
foreach ($lines as $key => $line) {
$lines[$key] = implode("&nbsp&nbsp&nbsp", $line);
}
echo implode("<br>", $lines);
?>
Output
73 40 79 1 43 7 76 44 18
6 53 45 55 71 20 80 66 74
69 51 52 65 22 63 59 50 54
29 33 23 49 77 24 61 60 58
8 81 30 15 26 32 16 47 31
17 39 4 35 27 11 5 25 68
2 34 72 42 75 46 48 3 38
14 28 37 62 10 78 12 56 13
41 21 19 36 9 64 67 57 70
If you don't want to use tables or CSS. You can use a <pre> tag then use some tabs and newlines
<pre>
<?php
$arr = array();
for ($i=1;$i<82;$i++) {
$arr[] = $i;
}
shuffle($arr);
$lines = array_chunk($arr, 9);
foreach ($lines as $key => $line) {
$lines[$key] = implode("\t", $line);
}
echo implode("\n", $lines);
?>
Fiddle
Note: I didn't really care about your logic for creating those lines while answering this question since its only about formatting. You can trim down some code too.
Output
22 16 66 79 8 41 47 2 80
29 38 76 18 40 46 73 34 45
31 3 62 68 14 33 20 72 67
78 44 42 30 51 77 36 25 48
64 70 21 15 19 9 56 50 65
37 27 4 1 35 74 75 52 32
81 23 10 28 26 59 7 54 11
6 63 5 39 53 12 24 60 49
71 55 17 13 61 69 43 57 58
Now with slightly better code
<pre>
<?php
$numbers=range(1,81);
shuffle($numbers);
$c=0;
foreach($numbers as $n)
{
if($c%9==0)echo "\n";
echo $n."\t";
$c++;
}
?>
modify your code like this:
<?php
$arr = array();
for ($i=1;$i<82;$i++) {
$arr[] = $i;
}
shuffle($arr);
$lines = array_chunk($arr, 9);
echo '<table>';
foreach ($lines as $key => $line) {
echo '<tr><td align="right">';
echo $lines[$key] = implode('</td><td align="right">', $line);
echo '</td></tr>';
}
echo '</table>';
?>
Output
57 41 48 17 73 76 7 78 12
69 61 39 80 24 58 45 11 70
47 65 33 21 38 4 19 13 46
59 52 63 14 25 3 30 28 77
50 40 68 6 2 29 20 66 26
72 74 34 75 15 36 71 10 60
55 53 1 16 23 42 51 35 62
44 32 43 64 18 8 54 49 5
81 27 31 67 37 22 79 56 9
try this
<?php
$arr = array();
for ($i = 1; $i < 82; $i++) {
$arr[] = $i;
}
shuffle($arr);
$lines = array_chunk($arr, 9);
echo "<table>";
foreach ($lines as $key => $line) {
echo "<tr>";
for ($i = 0; $i < sizeof($line); $i++) {
echo "<td align='right'>" . $line[$i] . "</td>";
}
echo "</tr>";
}
echo "</table>";
?>
Your Out put

Categories