Hello Guys how can i output this array using for loop?
This code is working but it is not for loop.
$a=array('Pol','Peter');
$b=array('3.2','2.79');
$c=array('1','1.4');
$values = array($a,$b,$c);
echo '<table border="1"><tr>';
echo '<td>'.$values[0][0].'</td>';
echo '<td>'.$values[1][0].'</td>';
echo '<td>'.$values[2][0].'</td>>';
echo '<tr><tr>';
echo '<td>'.$values[0][1].'</td>';
echo '<td>'.$values[1][1].'</td>';
echo '<td>'.$values[2][1].'</td>';
echo '</tr></table>';
Output:
--------------------------------
| Pol | 3.2 | 2.79 |
| Peter | 1 | 1.4 |
--------------------------------
Help me how loop this output.
Thanks in advance
You're looking for a for loop inside of a for loop.
The <table> tags should come outside of the outer loop, and the <tr> tags should come inside of the outer loop, but outside of the inner loop. Naturally, the <td> tags should come inside both.
This would look something like:
echo '<table border="1">';
for ($j = 0; $j < $c; $j++) {
echo '<tr>';
for ($i = 0; $i < count($values); $i++) {
echo '<td>'.$values[$i][$j].'</td>';
}
echo '<tr>';
}
echo '</table>';
Hope this helps! :)
Related
For example, I have a array with 8 data. I want to display in this format
$a = array(1,2,3,4,5,6,7,8);
======
1 2
3 4
5 6
7 8
=====
OR array with 11 data
$a = array(1,2,3,4,5,6,7,8,9,10,11);
=========
1 2 3
4 5 6
7 8 9
10 11
=========
Not sure which part i got wrong. Here is my code
$a = array(1,2,3,4,5,6,7,8);
$c = ceil(count($a)/2);
echo "<table>";
for($i=0; $i<$c;$i++){
echo "<tr>";
echo '<td>'.$a[$i].'</td>';
echo '<td>'.$a[$i+1].'</td>';
}
echo "</table>";
However, my data display in this way instead
======
1 2
2 3
3 4
4 5
=====
Basically, I want to display my data from mysql in this format. But before that, I need to test out the ceil function and see if its working. Anyone knows whats wrong with my coding?
Since you have 2 cels per row you need to calculate the actual index. Also don't forget to close the <tr>
for($i=0; $i<$c;$i++){
echo "<tr>";
echo '<td>'.$a[$i * 2].'</td>';
echo '<td>'.$a[$i * 2 + 1].'</td>';
echo '</tr>';
}
Here is a full example of a cleaner solution:
<?php
$a = array(1,2,3,4,5,6,7,8);
$cols = 2;
$c = ceil(count($a) / $cols);
echo "<table>";
for($i = 0; $i < $c; $i++){
echo "<tr>";
for ($col = 0; $col < $cols; $col++) {
$value = isset($a[$i * $cols + $col]) ? $a[$i * $cols + $col] : '';
echo '<td>'. $value .'</td>';
}
echo "</tr>";
}
echo "</table>";
If you increase it manually you have to skip 1 loop every time.
$a = array(1,2,3,4,5,6,7,8,9,10,11);
$size = sizeof($a);
/* Your magic method to determine the total table cols.
*/
$cols = 3;
#$cols = 2;
echo "<table>";
for($i = 0; $i < $size; $i+=$cols){
echo "<tr>";
for($c = 0; $c < $cols; $c++){
if($i+$c >= $size){
break;
}
echo '<td>'.$a[$i+$c].'</td>';
}
echo "</tr>";
}
echo "</table>";
I've used the method of math to determine if it should print or not because it is faster. However, if you have empty indexes in the array you might want to use the isset() method as the whole code in the inner for loop.
if(isset($a[$i+$c])){
echo '<td>'.$a[$i+$c].'</td>';
}
im in serach for a generic piece of code that uses and array
$arr[$key1][$key2] = $value;
output should be like this where "SUM" is not part of the array.
| 1st key2 | 2nd key2 | 3rd key2 | SUM
1st key1 | 10 | 10 | 10 | 30
2nd key1 | 10 | 10 | 10 | 30
3rd key1 | 10 | 10 | 10 | 30
SUM | 30 | 30 | 30 | 90
so i startet an output to see how far i get:
echo '<table width="100%"><thead>';
foreach($arr as $linekey => $line)
{
echo '<tr>';
echo '<th align="center">';
echo '</th>';
foreach($line as $key => $value)
{
echo '<th align="center">';
echo $key;
echo '</th>';
}
echo '<th align="center">';
echo 'SUM'; //adding the SUM column
echo '</th>';
echo '</tr>';
break;
}
echo '</thead><tbody>';
foreach($arr as $key1 => $value1)
{ echo '<tr>';
echo'<td>'.$key1.'</td>';
$sumRow = 0; //reset sumRow
foreach($arr[$key1] as $key2 => $value2)
{
echo'<td>'.round($arr[$key1][$key2],0).'</td>';
$sumRow += $arr[$key1][$key2]; //summing up rows
$sumAll += $arr[$key1][$key2]; //summing up everything
$sumCol += $arr[$key1][$key2]; //where should be this?
}
echo'<td>'.round($sumRow,0).'</td>'; //echo $sumRow
echo '</tr>';
}
echo '</tbody></table>';
this alaredy works but im not sure where to sum the columns
You should use an array $sumCol to gather columns sums:
$sumCol[$key2] += $arr[$key1][$key2];
It size should be as number of columns.
You cannot do it in one loop without an array because you loop over columns index internally, so you could gather only sumRow in one temporary variable (without array).
Then, at the end:
echo '<tr><td>SUM</td>';
foreach($sumCol as $key2 => $value2)
{
echo'<td>'.round($sumCol[$key2],0).'</td>'; //echo $sumCol
}
echo '</tr>';
echo '</tbody></table>';
The other way is to define the second loop where you iterate first over columns and at second, internally, over rows.
This is a practice question I am having a problem with. So far, what I got to create is a table using the following code. I need help to fix it, with explanation on what my mistakes are.
What I am trying to to do is pretty simple: generating numbers from 1 to 100 in a table using php nested in an html code
Each 10 numbers should be in an aligned row, then break after 10
| 1 | 2 | . . . | 10 |
|11 | 12| .... | 20 |
|21 | 22| .... | 30 |
.
.
.
|91| 92 | ... |100|
<table border="1">
<?php
for ($x=1; $x <= 100; $x++){
if($x%11 ==0) {
echo "<tr><td>" . "<br>" . "</tr></td>";
}
echo "<tc><td>" . $x . "</td></tc>" ;
}
?>
</table>
Also using a for statement for the loop with if statement nested in it for the break the row
Could someone point out my mistakes in this code please.
all right the correct code should be like this:
<table border="1">
<?php
for ($x=1; $x <= 100; $x++){
if($x%10 ==1) {
echo "<tr>";
}
echo "<td>" . $x . "</td>" ;
}
?>
</table>
Thank you all for the help
We need to first understand the requirement.
Requirement:
Need incremental counter horizontally.
Every row should contain a whole 10 numbers starting from say 1 to 10, 11 to 20 etc.
So, if we consider the output as HTML table, <tr> increments by 11 and <td> increments by 1.
So, we need two loops: once incrementing by 10: the outer loop and other incrementing by 1: the inner loop.
So, write outer loop and inside it, add inner loop, both have step: 1
So, outer loop will increment the counter and inside it, inner loop will also run.
For every outer loop, the inner loop will run 10 times.
Thus, we get exact rows: 1-10, 11-20
Try this:
<table>
<?php
$k=0;
for ($i=0; $i<10; $i++) {
?>
<tr>
<?php
for ($j=0; $j<10; $j++) {
++$k;
?>
<td><?php print $k;?></td>
<?php
}
?>
</tr>
<?php
}
?>
</table>
Try this:
<?php
echo "<table border =\"1\" style='border-collapse: collapse'>";
for ($row=1; $row <= 10; $row++) {
echo "<tr> \n";
for ($col=1; $col <= 10; $col++) {
$p = $col * $row;
echo "<td>$p</td> \n";
}
echo "</tr>";
}
echo "</table>";
?>
<table>
<?php
for ($x=1; $x <= 100; $x++)
{
if($x%10 ==1)
**{
echo "<tr><td>" . "<br>" . "</tr></td>";**
}
echo "<tc><td>" . $x . "</td></tc>" ;
}
?>
</table>
I'm pretty sure I found no answer here or elsewhere or maybe because it's my fault not being able to ask the right question, but anyway, say I have a for-loop in my PHP that goes something like this:
for($i=0; $i<3; $i++){
echo "$i | ";
}
How do I make the last call for " | " to not appear in order to have a nicely output:
0 | 1 | 2
INSTEAD OF
0 | 1 | 2 | <-- remove this! :(
You guys are fabulous! Thanks!!!
What if $i < $undefinedNumber ?
Keep the flow, you only need that if it's not the first:
for ($i = 0; $i < 3; $i++) {
echo $i ? " | " : '', "$i";
}
And for the first $i is 0 which means it's false. Otherwise $i is a positive integer, which means true.
Sometimes finding an alternative way to describe the problem helps to formulate a simpler answer. Instead of:
0 | 1 | 2 | /* <-- remove this! :( */
It's that problem:
/* :( remove this! --> */ | 0 | 1 | 2
The same form of solution works for CSS, too:
a:not(:first-child):before {content: ' | '}
sometimes that's easier to integrate than fiddling with PHP and HTML (Example) - however that adds the | to the link-text or style (at least in chrome where I tested it, looks buggy).
for($i=0; $i<3; $i++){
echo "$i"
if ($i != 2) {
echo " | ";
}
}
or
echo "0";
for($i=1; $i<3; $i++){
echo " | $i"
}
for($i=0; $i<3; $i++) {
if ($i == 0) {
echo "$i;
}
else {
echo " | $i"
}
}
So you just need to remove the last bar from the output? Why not skip it as you loop? Something like this:
for($i=0; $i<3; $i++)
{
echo "$i";
if($i < 2)
echo "|";
}
This would ensure that the bar only shows after the first two links, and not after the third.
$output=array();
for($i=0; $i<3; $i++){
$output[]="$i";
}
$ouput=implode(' | ', $ouput);
You can also do this:
for($i=0; $i<3; $i++) {
$arr[] = "$i";
}
echo implode(' | ',$arr);
This is a commonly found situation in programming known as the Special Case pattern. There is no built in way of solving this other than using an if statement or the ternary operator
for($i=0; $i<3; $i++){
echo "$i" . ($i != 2) ? " | " : "";
Don't do it in the php, do it in the stylesheet.
First build your HTML
<ul class="navlinks">
<?php foreach($links as $linkName => $linkUrl): ?>
<li><?php echo $linkName; ?></li>
<?php endforeach; ?>
</ul>
Apply the CSS.
.navlinks > li:after
{
content:"<li>|</li>";
}
.navlinks > li:last-child:after
{
content:"";
}
You should never style your HTML in php, this is the correct wayt to do it.
$arr = array();
for ($i=0; i<3; i++)
$arr[] = "<a href=''>$i</a>";
echo implode(" | ",$arr);
More compact version:
echo implode(" | ", array_map(function($x){return "<a href=''>$x</a>";},range(1,3)));
I try to create a binary tree as html-table which is not recursive build. The order of the fields should be like this:
C1 C2 C3
7
3
8
1
9
4
10
11
5
12
2
13
6
14
C1 stands for col 1, C2 for col2 etc.
The following code creates a table in a recursive way, but this is not what I want!
<?php
$cols = 4;
$counter = 0;
$lines = pow(2,$cols);
echo '<table border=1 style="border:1px solid black;"> ';
for($i = 0; $i < $lines; $i++){
echo '<tr>';
for($j = 0; $j < $cols; $j++){
$rowspan = $lines/pow(2,$j+1);
if(0 === $i%$rowspan) {
$counter++;
echo '<td rowspan='.$rowspan.'>'.$counter;
}
}
}
echo '</table>';
?>
I hope someone could give me a hint how to solve this problem.
Used this expression to calculate the row's values: ($i / $rowspan + pow(2,$j+1) - 1) wherein $i / $rowspan is the number of the row in the current level starting with 0 for the first row and pow(2,$j+1) - 1 is the level's first value, i.e. 7 for the third level.
$cols = 4;
$lines = pow(2,$cols);
echo '<table border=1 style="border:1px solid black;">';
for($i = 0; $i < $lines; $i++){
echo '<tr>';
for($j = 0; $j < $cols; $j++){
$rowspan = $lines/pow(2,$j+1);
if(0 === $i%$rowspan) {
echo "<td rowspan='$rowspan'>".
($i/$rowspan + pow(2,$j+1)-1).
"</td>";
}
}
}
echo '</table>';
Outputs your desired result. Hope your teacher won't hate me now! ;-)
You can alternativly use ($i/$lines + 1 ) * pow(2,$j+1) - 1 for the row's value, if you don't want to depend on $rowspan.