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

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

Related

a PHP table map

I want to create a game where I have to create a map with PHP. I want to create 100 tables boxes in the right way but I can't get it work...
$field = 100;
echo "<table border='3px' dir='ltr'>";
for ($row=0; $row < 10 ; $row++) {
echo "<tr>";
for ($column=0; $column < 10; $column++) {
echo "<td>";
echo $field;
$field--;
echo "</td>";
}
echo "</tr>";
}
echo "</table>";
?>
This gives me this table:
But I will need a table like:
If a for loop is not a requirement.
I would use an array, then you have a set to work from rather then calculating things, then you just need reverse the odd row, like so:
$rows = array_reverse(array_chunk(range(1, 100), 10));
echo "<table>\n";
foreach ($rows as $level => $row) {
if (($level-1) % 2) {
$row = array_reverse($row);
}
echo "\t<tr>\n";
foreach ($row as $value) {
echo "\t\t<td>$value</td>\n";
}
echo "\t</tr>\n";
}
echo "<table>\n";
https://3v4l.org/204eh

Get each array item from foreach loop as table data

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

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

Echo data to specific column in table

I am using a for loop to generate a table for a SQL query's results. I am trying to alter the code so that new text is echoed into a specific column of the table (column 8)
$fields_num = mysql_num_fields($result);
echo "<h1>Table: {$table}</h1>";
echo "<table border='0' id=resultTable><tr>";
for($i=0; $i<$fields_num; $i++)
{
$field = mysql_fetch_field($result);
echo "<td id" . $i;
echo "></td>";
}
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";
}
mysql_free_result($result);
?>
what actually do you mean by "new text" in "I am trying to alter the code so that new text is echoed into a specific column of the table"?
Is "text" the name of a particular table field? If so, it would be better to alter the sequence of the fields in the mysql_query that you are using instead of doing any change in the html generation for this
e.g.
SELECT field1, field2, ... new_text from ...
Put new_text in the eighth position.
Update
If you want to display various things for various columns in the result table, you can put a counter and do what you want as per count like this:-
// printing table rows
while($row = mysql_fetch_row($result))
{
echo "<tr>";
// $row is array... foreach( .. ) puts every element
// of $row to $cell variable
$counter = 0;
foreach($row as $cell)
{
if($counter == 7)
{//eighth column
echo "<td><img src='".$cell."' /></td>";
}
else if($counter == 5)
{//sixth col
echo //something else ...
}
else
echo "<td>$cell</td>";
//inside your default td
$counter++;
}
echo "</tr>\n";
}
But it would be more readable,maintainable and customizable to have some config array where you specify which column is going to hold what like this:-
function getColumnConfigs()
{
return $columnsConfigs = array(5=> "something_else", 7 => "img");
//specify special cases for necessary columns. columns not mentioned here will take default.
}
and then use this in your iteration:-
// printing table rows
$columnsConfig = getColumnConfigs();
while($row = mysql_fetch_row($result))
{
echo "<tr>";
// $row is array... foreach( .. ) puts every element
// of $row to $cell variable
$counter = 0;
foreach($row as $cell)
{
if(isset($columnsConfig[$counter]))
{
switch($columnsConfig[$counter])
{
case "img":
$html = "<td><img src='".$cell."' /></td>";
break;
case "something_else"
$html = "<td><img src='".$cell."' /></td>";
break;
}
}
else
{//default
$html = echo "<td>$cell</td>";
}
}
echo "</tr>\n";
}

Categories