Reputation:
I have a array of row and column.
<?php
$rows=array(3); // rows 0 to 3
$cols=array(3); // column 0 to 3
);
?>
And i want to create a table according to following fashion with loop.
<table border="1px">
<tr>
<td>10</td> // 0 row digit of 1 column
<td>20</td> // 0 row digit of 2 column
<td>30</td> // 0 row digit of 3 column
</tr>
<tr>
<td>15</td> // 1 row digit of 1 column
<td>16</td> // 1 row digit of 2 column
<td>17</td> // 1 row digit of 3 column
</tr>
<tr>
<td>22</td> // 2 row digit of 1 column
<td>23</td> // 2 row digit of 2 column
<td>24</td> // 2 row digit of 3 column
</tr>
<tr>
<td>35</td> // 3 row digit of 1 column
<td>33</td> // 3 row digit of 2 column
<td>32</td> // 3 row digit of 3 column
</tr>
</table>
Here first column contains following value
10
15
22
35
Ans:
Here 2nd column contains following value
20
16
23
33
Ans
Here 3rd column contains following value
30
17
24
32
Ans
I want to display it in php. Here is my php code.
<table border="1px">
<?php
for($row=0;$row<3;$row++){
?>
<tr>
<?php
for($cols=0;$cols<3;$cols++){
?>
<td><?php echo $cols; ?></td>
<?php
}
?>
</tr>
<?php
}
?>
</table>
Here is the output of it
0 1 2
0 1 2
0 1 2
But i required
0 0 0
1 1 1
2 2 2
Why? how can i make this correctly?
Upvotes: 0
Views: 1124
Reputation: 54831
Modify your code and move tr
/ /tr
outside second for
loop:
<table>
<?php
for($row=0;$row<3;$row++){?>
<tr> <!-- Open tr here -->
<?php
for($cols=0;$cols<3;$cols++){?>
<td><?php echo rand(0, 10); ?></td>
<?php
}?>
</tr> <!-- Close tr here -->
<?php
}?>
</table>
Upvotes: 2