Get each array item from foreach loop as table data - php

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>";

Related

Single php array to html 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>";
?>

why does foreach skip 2 rows from the query result?

For 10 rows in the query it only returns 8 rows but i get the fields right:
For Query data which returns less than 2 rows I get an error.
//create table to display all data
echo "<table border="1"> ";
echo "<tr>";
$row = mysqli_fetch_assoc($result);
foreach($row as $key => $value)
{
echo "<th>$key</th>";
}
echo "</tr>";
while (($row = $result->fetch_assoc()) !== null)
{
$output = array();
$i=1;
echo "<tr>";
foreach ($row as $columnName => $columnValue)
{
$output[] = $columnName."=>". $columnValue;
echo "<td>".$columnValue."</td>";
}
echo "</tr>";
}
echo "</table>";
Edit: Thanks to #Michael Berkowski for his comments on the question.
Here is a modified version of your code that should work:
//create table to display all data
echo "<table border=1 >";
echo "<tr>";
//echo "<th> ## </th>";
$row = $result->fetch_assoc(); // stick to the object-oriented form. It is cleaner.
foreach ($row as $key => $value)
{
echo "<th>$key</th>";
}
echo "</tr>";
do
{
$output = array();
echo "<tr>";
foreach ($row as $columnName => $columnValue)
{
$output[$columnName] = $columnValue; // this is neater.
echo "<td>" . $columnValue . "</td>";
}
echo "</tr>";
} while ($row = $result->fetch_assoc());
echo "</table>";
You can use your first foreach() loop to print the keys and then use a do-while() loop to get your desired output.
Additional reading:
PHP do-while loop
You need to use
mysqli_fetch_assoc($result);

Dynamic Table Continuous <td> Count

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
}

php fun codeing

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";
?>

using PHP foreach loop to add ID's to table columns

I am looking to add column IDs to a table created by a foreach loop like this. Help is appreciated!:
echo "<h1>Table: {$table}</h1>";
echo "<table border='0'><tr>";
echo "</tr>\n";
// printing table rows
while($row = mysql_fetch_row($result))
{
echo "<tr>";
// $row is array... foreach( .. ) puts every element
// of $row to $cell variable
foreach($row as $cell)
echo "<td>$cell</td>";
echo "</tr>\n";
Thanks,
Mike
I quite don't understand if you need an id for each cell or row or... column ?
For row :
$i = 1;
while($row = mysql_fetch_row($result))
{
echo '<tr id="row-"'.$i.'">';
// $row is array... foreach( .. ) puts every element
// of $row to $cell variable
foreach($row as $cell)
echo "<td>$cell</td>";
echo "</tr>\n";
$i++;
}
For cell :
$i = 1;
while($row = mysql_fetch_row($result))
{
echo '<tr>';
// $row is array... foreach( .. ) puts every element
// of $row to $cell variable
foreach($row as $cell)
echo '<td id="cell-"'.$i.'">$cell</td>';
echo "</tr>\n";
$i++;
}
Notes :
1-
Why are you using this :
echo "<table border='0'><tr>";
echo "</tr>\n";
instead of :
echo "<table border='0'><tr></tr>\n";
or even better :
echo "<table border='0'><tr><td></td></tr>";
Since you know you're using only one cell.
2-
Since you're introducing a int as an id (well, it's usually the case... incremental Ids) and that html's id can't start with a number, you must put something in front of it.
while($row = mysql_fetch_assoc($result)){
echo "<tr>";
// $row is array... foreach( .. ) puts every element
// of $row to $cell variable
foreach($row as $colName=>$colValue)
// do wathever you like here
echo "</tr>\n";
Regards
#user547794: I'm not entirely sure what you mean, but if you just wish to assign an id to each cell, this should do the trick --
echo "<h1>Table: {$table}</h1>\n";
echo '<table cellspacing="0">' . "\n";
$i = 1;
// printing table rows
while($row = mysql_fetch_row($result))
{
echo "<tr>\n";
// $row is array... foreach( .. ) puts every element
// of $row to $cell variable
foreach($row as $cell)
{
echo '<td id="r' . $i . '">$cell</td>' . "\n";
}
echo "</tr>\n";
$i++;
}
echo "</table>\n";

Categories