hey I make a simple php table using nested for loop ... and it will be like this...
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
but the problem is , i can not print this value using a loop inside the column .. so what will be the solution ??? please
my code :
echo "<table border=1>\n";
for($row=1;$row<=3;$row++)
{
echo "<tr>";
for($col=1;$col<=5;$col++)
{
echo "<td>";
echo "MY PROBLEM HERE...I cant print column numbers \n";
echo "</td>";
}
echo "</tr>";
}
echo "</table> \n";
echo "<table border=1>\n";
for($row=1;$row<=3;$row++)
{
echo "<tr>";
for($col=1;$col<=5;$col++)
{
echo "<td>";
//echo "MY PROBLEM HERE...I cant print column numbers \n";
echo $col + ($row - 1) * 5;
echo "</td>";
}
echo "</tr>";
}
echo "</table> \n";
To save several loops:
$rows = 3;
$cols = 5;
$table = '<table border="1">';
for($i=1;$i<=$rows;$i++){
$table .= '<tr><td>'.implode('</td><td>', range($cols*$i-$cols+1,$cols*$i)).'</td></tr>';
}
$table .= '</table>';
echo $table;
It's not $col + $row * 5 it has to be $row - 1
<?php
echo "<table border=1>\n";
for($row=1;$row<=3;$row++)
{
echo "<tr>";
for($col=1;$col<=5;$col++)
{
echo "<td>";
echo $col + ($row-1) * 5;
echo "</td>";
}
echo "</tr>";
}
echo "</table> \n";
?>
Related
I'm trying a foreach loop on an array with 20 names. I should derive a table with 4 columns and 5 rows, with each cell(table data) having a unique name. My code is below with a snapshot of the output table. It hasn't worked just yet. How can I fix this?
<?php
$names = array("Patrick","Raymond","George","Hosea","Samuel","Alan","June","Edwin","Yvonne","John","Paul","Ruto","Uhuru","Raila","Kalonzo","Sonko","Joho","Wetangula","Mudavadi","Matiang'i");
echo "<table width='200' border='1' >";
foreach($names as $name){
echo "<tr>";
for($cols=1;$cols<5;$cols++){
echo "<td>".$name."</td>";
}
echo "<tr>";
}
echo "<table>";
?>
1st : Remove for loop
2nd : apply the limit using $i
Note 1 : Your looping single name 5 times .that should not .
Note 2 : for more detail read my comment lines .
<?php
$names = array("Patrick","Raymond","George","Hosea","Samuel","Alan","June","Edwin","Yvonne","John","Paul","Ruto","Uhuru","Raila","Kalonzo","Sonko","Joho","Wetangula","Mudavadi","Matiang'i");
echo "<table width='200' border='1' >";
$i=0;
foreach($names as $name){
if($i==0){ //open new tr if $i is 0
echo "<tr>";
}
echo "<td>".$name."</td>";
if($i==3){ //close the tr if the $i is reached the 3 .
echo "</tr>";
$i=-1; //why setting -1 means i'm incrementing after this so i set -1
}
$i++;
}
echo "<table>";
?>
Splitting the array into chunks of the wanted size may make for more readable code:
$names = array("Patrick","Raymond","George","Hosea","Samuel","Alan","June","Edwin","Yvonne","John","Paul","Ruto","Uhuru","Raila","Kalonzo","Sonko","Joho","Wetangula","Mudavadi","Matiang'i");
echo "<table width='200' border='1' >";
$names = array_chunk($names, 4);
foreach($names as $group){
echo "<tr>";
foreach($group as $name) {
echo "<td>".$name."</td>";
}
echo "</tr>";
}
echo "<table>";
The idea is to print an html table from this array:
$arr = ['1','2','3','4,'5,'6','7','8','9'];
I expect my table to be something like:
1 2 3
4 5 6
7 8 9
I tried a lot but I couldn't find an idea to do this.
My idea was to break each three element but I need something smarter.
You can use array-chunk like this:
$arr = ['1','2','3','4','5','6','7','8','9'];
echo "<table>";
foreach(array_chunk($arr, 3) as $row) {
echo "<tr>";
foreach($row as $cell) {
echo "<td>$cell</td>";
}
echo "</tr>";
}
echo "</table>";
$arr = ['1','2','3','4','5','6','7','8','9'];
$from=0; //index from of arr
$number=3; //number cell per row
echo "<table border='1'>";
while($row=array_slice($arr,$from,$number)){
echo "<tr>";
foreach($row as $cell) {
echo "<td>$cell</td>";
}
echo "</tr>";
$from+=$number;
}
echo "</table>";
<?php
$arr = ['1','2','3','4','5','6','7','8','9'];
print "<table>\n";
foreach(array_chunk($arr, 3) as $row) {
print "<tr>";
foreach($row as $col) {
print "<td>";
print $col;
print "</td>";
}
print "</tr>\n";
}
print "</table>";
?>
I am stuck in a weird problem, I want to show the continuous number till the last in the row and cols,
instead of that its showing form 1-50 and then again the row starts with 1 up-to 50.
<?php
$rows = 10;
$cols = 50;
$x=1 ;
echo "<table border='1'>";
for($tr=1;$tr<=$rows;$tr++){
echo "<tr>";
for($td=1;$td<=$cols;$td++){
echo "<td>$td</td>";
}
echo "</tr>";
}
echo "</table>";
?>
Thanks
$rows = 10;
$cols = 50;
$x=1 ;
echo "<table border='1'>";
for($tr=1;$tr<=$rows;$tr++){
$style = "green";
if ($tr % 2 == 0) {
$style = "#ccc";
}
echo "<tr style='background-color:".$style."'>";
for($td=1;$td<=$cols;$td++)
{
echo "<td>$x</td>";
$x++;
}
echo "</tr>";
}
echo "</table>";
You are outputting $td which resets at every new tablerow.
Instead, you would like to output an incrementing $x, if I'm not mistaken.
<?php
$rows = 10;
$cols = 50;
$x=1 ;
echo "<table border='1'>";
for($tr=1;$tr<=$rows;$tr++){
echo "<tr>";
for($td=1;$td<=$cols;$td++){
echo "<td>" . $x . "</td>";
$x++;
}
echo "</tr>";
}
echo "</table>";
?>
In response to your comment on another answer: if you would want alternating row colors, you could use $tr and check wether it is even or uneven:
if($tr % 2 == 0)
{
// use color1
}
else
{
// use color2
}
I'm trying to parse the arrivals table from here [1] and put in into an array to be able to format it and put it into a table.
I did some research here and there, I've got some code from other questions, but I can't make the array and table look as I'd like.
Anyone can help me out?
<?php
require('simple_html_dom.php');
$html = file_get_html('http://flightplan.romatsa.ro/init/fpl/flightslr/LRCL/');
$table = $html->find('table', 3);
foreach($table->find('tr') as $row) {
// initialize array to store the cell data from each row
$rowData = array();
foreach($row->find('td') as $cell) {
// push the cell's text to the array
$rowData[] = $cell->innertext;
}
echo "<table>";
echo "<td>";
echo $rowData[0]. " ";
echo "</td>";
echo "<td>";
echo $rowData[1]. " ";
echo "</td>";
echo "<td>";
echo $rowData[2]. " ";
echo "</td>";
echo "<td>";
echo $rowData[3]. " ";
echo "</td>";
echo "<td>";
echo $rowData[4]. " ";
echo "</td>";
echo "<td>";
echo $rowData[5]. " ";
echo "</td>";
echo "<td>";
echo $rowData[6]. " ";
echo "</td>";
echo "<td>";
echo $rowData[7]. " ";
echo "</td>";
echo "<td>";
echo $rowData[8]. " ";
echo "</td>";
echo "</table>";
}
?>
Maybe try putting each row into an array and then each cell into another array. Hopefully, that will do what you want.
require('simple_html_dom.php');
$html = file_get_html('http://flightplan.romatsa.ro/init/fpl/flightslr/LRCL/');
$table = $html->find('table', 3);
$rowData = array();
foreach($table->find('tr') as $row) {
// initialize array to store the cell data from each row
$flight = array();
foreach($row->find('td') as $cell) {
// push the cell's text to the array
$flight[] = $cell->plaintext;
}
$rowData[] = $flight;
}
echo '<table>';
foreach ($rowData as $row => $tr) {
echo '<tr>';
foreach ($tr as $td)
echo '<td>' . $td .'</td>';
echo '</tr>';
}
echo '</table>';
Note: this solution requires the simple_html_dom.php library. Get it here!
I currently use 2 arrays to place values in my table like so:
echo "<table>";
foreach ($products as $key => $product){
$number = isset($_POST[$key])?$_POST[$key]:'';
if (!$number){
echo "";
} else {
echo "<tr>";
echo "<td>";
echo $product;
echo "</td>";
echo "<td>";
echo $number;
echo "</td>";
echo "</tr>";
}
}
echo "</table>";
The $product and $number are showing just fine, but my problem is that i need a third column with the $prices in them. Looking like this:
echo "<td>";
echo $price;
echo "</td>";
I made an array for $prices looking like this:
$prices = array(7, 8, 9, 11, 13, 5, 5.50, 6, 0.50, 0.50);
But it gets more complicated.
It needs to be so that $price = $number * $prices.
Meaning that if $number = 5 and $prices = 8 it should place 40 in the $price.
I don't know if this is easy to do or not. But i'd like some help with this since im not really familiar with arrays.
Thanks in advance!
echo "<table>";
$count = 0;
foreach ($products as $key => $product){
$number = isset($_POST[$key])?$_POST[$key]:'';
if (!$number){
$count++;
echo "";
} else {
echo "<tr>";
echo "<td>";
echo $product;
echo "</td>";
echo "<td>";
echo $number;
echo "</td>";
echo "<td>";
$price = $prices[$count] * $number;
echo $price;
echo "</td>";
echo "</tr>";
$count++;
}
}
echo "</table>";