I have an array:
$A = array(
'A'=>
array('a1'=>array(1,2,3),
'a2'=>array(1,2,3),
'a3'=>array(1,2,3)),
'B' =>
array('b1'=>array(1,2,3),
'b2'=>array(1,2,3),
'b3'=>array(1,2,3)),
'C');
How Can I use loop to present data (html table ):
-----------------------------
| | time1| time1 | time2 |
----------------------------
A | 3 | 6 | 8
-----------------------------
a1 | 1 | 2 | 2
-----------------------------
a2 | 1 | 2 | 3
-----------------------------
a3 | 1 | 2 | 3
----------------------------
B
b1
b2
b3
C
I am trying( get the total && display C instead 0):
echo '<table border="1">';
echo "<tr><td></td><td>time1</td><td>time2</td><td>time3</td></tr>";
foreach ($A as $key=>$main){
echo '<tr>';
echo "<td>$key</td>";
echo "<td>???</td>";
echo "<td>???</td>";
echo "<td>???</td>";
echo '</tr>';
foreach ($main as $k=>$sub) {
echo '<tr>';
echo "<td>$k</td>";
foreach ($sub as $kk =>$val) {
echo "<td>$val</td>";
}
echo '</tr>';
}
}
echo '</table>';
<?php
$A = array(
'A'=>
array('a1'=>array(1,2,2),
'a2'=>array(1,2,3),
'a3'=>array(1,2,3)),
'B' =>
array('b1'=>array(1,2,3),
'b2'=>array(1,2,3),
'b3'=>array(1,2,3)),
'C'=> null); // in your case it won't be key it will be value
echo '<table border="1">';
echo "<tr><td></td><td>time1</td><td>time2</td><td>time3</td></tr>";
foreach($A as $key => $value)
{
echo "<tr><td>".$key."</td>";
if(!is_array($value))
continue;
echo "<td>".implode("</td><td>", findSum($value))."</td></tr>";
foreach($value as $cKey => $cValue){
echo "<tr><td>".$cKey."</td>";
echo "<td>".implode("</td><td>", $cValue)."</td>";
}
echo "\n";
}
function findSum($value)
{
if(!is_array($value))
return array(0);
$result = array();
foreach($value as $childValue)
{
foreach($childValue as $cKey => $cValue)
$result[$cKey] = isset($result[$cKey]) ? $result[$cKey] + $cValue : $cValue;
}
return $result;
}
Related
Error
Defined array in the controller, then transformed to the model which uses the implode function.
Problem = Data are inserted in the sql table in the same column. Eg: Coke,Pepsi,Slice (1 column)
t_id| product_name | unit | cost
1 | Coke,Pepsi,Slice | 5 | 1000
Solution Wanted = Data should be inserted in multiple column such as
t_id| product_name | unit | cost
1 | Coke | 5 | 1000
2 | Pepsi | 2 | 500
3 | Slice | 3 | 600
View
<div class="col-lg-6">
<?php
$getNameValue = $this->session->userdata('nameValue');
echo '<h3>' .$getNameValue['name'] .' paid Rs ' .$getNameValue['cash_amount']. '</h3>';
$getValue = $this->session->userdata('sessiondata');
if ($getValue != NULL){
echo '<table class="table table-bordered table-striped">';
echo '<tr>';
echo '<td><strong>Product Name</strong></td>';
echo '<td><strong>Unit</strong></td>';
echo '<td><strong>Cost</strong></td>';
echo "<form method='post' action=''>";
foreach ($getValue as $row)
{
echo '<tr>';
echo '<td><input type="hidden" name="allProduct[product_name][]" value="'.$row['product_name'].'">' .$row['product_name']. '</td>';
echo '<td><input type="hidden" name="allProduct[unit][]" value="'.$row['unit'].'">' .$row['unit'].'</td>';
echo '<td><input type="hidden" name="allProduct[cost][]" value="'.$row['cost'].'">' .$row['cost'].'</td>';
echo '</tr>';
}
echo '<tr><td></td><td><td><button type="submit" class="btn btn-success">POST</button></td></td></tr>';
echo "</form>";
echo '</table>';
}
?>
</div>
Controller
public function newSystemBatch($getBatch)
{
if ($data = $this->input->post('systemProduct')) {
$data['batch_id'] = $getBatch;
if (isset($data['sum'])) {
$data['cost'] = $data['cost'] / $data['unit'];
unset($data['sum']);
}
$productName = $data['product_name'];
$unit = $data['unit'];
$cost = $data['cost'];
$getValue = $this->session->userdata('sessiondata');
$getValue[] = array(
'product_name' => $productName,
'unit' => $unit,
'cost' => $cost
);
$this->session->set_userdata('sessiondata', $getValue);
$this->session->set_flashdata('message', $data);
redirect('inventory/newSystemMessage/' . $getBatch['batch_id']);
}
if ($data = $this->input->post('allProduct')) {
$this->header();
$this->footer();
$this->transaction->addNew($data);
$this->load->view("admin/newSystemTable", $data);
}
else {
$this->header();
$data['action'] = 'System';
$data['users_id'] = $this->user->users_id;
$this->load->view("admin/newSystemTable", $data);
$this->footer();
}
}
Model
function addNew($data)
{
if(is_array($data['product_name'])) $data['product_name'] = implode(",", $data['product_name']);
if(is_array($data['unit'])) $data['unit'] = implode(",", $data['unit']);
if(is_array($data['cost'])) $data['cost'] = implode(",", $data['cost']);
$this->db->insert('try', $data);
}
Model:
function addNew($data)
{
// Test that all all sub arrays are equal size
$length = array_unique(array_map('count', $data));
if (count($length) == 1) {
$length = current($length);
$keys = array_keys($data);
$res = array();
for($i = 0; $i < $length; $i++)
foreach($keys as $key)
$res[$i][$key] = $data[$key][$i];
$this->db->insert_batch('try', $res);
}
else {
// incorrect data
return false;
}
return true;
}
I have this table "city" in my database:
|id |id_city_a |id_city_b|distance|
|1 |1 | 1 | 0 |
|2 |1 | 2 | 8 |
|3 |1 | 3 | 6 |
|4 |2 | 1 | 8 |
|5 |2 | 2 | 0 |
|6 |2 | 3 | 9 |
|7 |3 | 1 | 6 |
|8 |3 | 2 | 9 |
|9 |3 | 3 | 0 |
I want the end result to be in a matrix, such as:
| | 1 | 2 | 3 |
| 1 | 0 | 8 | 6 |
| 2 | 8 | 0 | 9 |
| 3 | 6 | 9 | 0 |
This is my code :
function random()
{
include('config/koneksi.php');
$result = mysql_query("select * from temp_hasil");
$n =mysql_num_rows(mysql_query("SELECT * FROM temp_hasil"));
for ($i = 1; $i <= $n; $i++)
{
for ($j = 1; $j <= $n; $j++)
{
$rows = mysql_fetch_array($result);
$this->table[$i][$j] = $i == $j ? INF : $rows['id'];
}
}
}
function __toString()
{
$str = '<table class="table table-bordered" id="tableInput"> <tbody>';
$str .= '<tr><td></td>';
foreach ($this->table as $rowName => $row)
{
$str .= "<td>$rowName</td>";
}
$str .= '</tr>';
foreach ($this->table as $rowName => $row)
{
$str .= "<tr><td>$rowName</td>";
foreach ($row as $columnName => $value)
{
$str .= "<td>";
$str .=
'<input class="form-control" type="text" value="' . $value . '" name="table[' . $rowName . '][' .
$columnName . ']" requied' . ($columnName == $rowName ? ' disabled' : '') . '>';
$str .= "</td>";
}
$str .= '</tr>';
}
$str .= '</tbody></table>';
return $str;
}
}
$str .= '</tr>';
foreach ($this->table as $rowName => $row)
{
$str .= "<tr><td>$rowName</td>";
foreach ($row as $columnName => $value)
{
$str .= "<td>";
$str .=
'<input class="form-control" type="text" value="' . $value . '" name="table[' . $rowName . '][' .
$columnName . ']" requied' . ($columnName == $rowName ? ' disabled' : '') . '>';
$str .= "</td>";
}
$str .= '</tr>';
}
$str .= '</tbody></table>';
return $str;
}
`
How do I code it in php? please help me.
Make city_a index completely different from city_b index so it is easy to check.
// generate a two-dimensional matrix in here
$distMatrix = array();
foreach($tableRows as $cityDist) {
$from = $cityDist['id_city_a'];
$to = $cityDist['id_city_b'];
$dist = $cityDist['distance'];
$distMatrix[$from][$to] = $dist;
}
Display as an HTML Table...
echo '<table border="1">';
echo '<tr>';
echo '<td>', '#', '</td>';
foreach(array_keys(current($distMatrix)) as $city_b) { // city_b headings
echo '<td>', $city_b ,'</td>';
}
echo '</tr>';
foreach(array_keys($distMatrix) as $city_a) { // need the city_a as row index
echo '<tr>';
echo '<td>', $city_a, '</td>'; // city_a ad
foreach(array_keys($distMatrix[$city_a]) as $city_b) { // need the city_b as column index
echo '<td>', $distMatrix[$city_a][$city_b], '</td>'; // distance from the matrix;
}
echo '</tr>';
}
echo '</table>';
Test data - used data from #ashkufaraz
// changed the city ids so we can easily see city_a and city_b
$tableRows[0]=array("id"=>1, "id_city_a"=>1, "id_city_b"=>11, "distance"=>0);
$tableRows[1]=array("id"=>2, "id_city_a"=>1, "id_city_b"=>12, "distance"=>8);
$tableRows[2]=array("id"=>3, "id_city_a"=>1, "id_city_b"=>13, "distance"=>6);
$tableRows[3]=array("id"=>4, "id_city_a"=>2, "id_city_b"=>11, "distance"=>8);
$tableRows[4]=array("id"=>5, "id_city_a"=>2, "id_city_b"=>12, "distance"=>0);
$tableRows[5]=array("id"=>6, "id_city_a"=>2, "id_city_b"=>13, "distance"=>9);
$tableRows[6]=array("id"=>7, "id_city_a"=>3, "id_city_b"=>11, "distance"=>6);
$tableRows[7]=array("id"=>8, "id_city_a"=>3, "id_city_b"=>12, "distance"=>9);
$tableRows[8]=array("id"=>9, "id_city_a"=>3, "id_city_b"=>13, "distance"=>0);
Output:
# 11 12 13
1 0 8 6
2 8 0 9
3 6 9 0
try like this
Online Demo
$tableRows[0]=array("id"=>1,"id_city_a"=>1,"id_city_b"=>1,"distance"=>0);
$tableRows[1]=array("id"=>2,"id_city_a"=>1,"id_city_b"=>2,"distance"=>8);
$tableRows[2]=array("id"=>3,"id_city_a"=>1,"id_city_b"=>3,"distance"=>6);
$tableRows[3]=array("id"=>4,"id_city_a"=>2,"id_city_b"=>1,"distance"=>8);
$tableRows[4]=array("id"=>5,"id_city_a"=>2,"id_city_b"=>2,"distance"=>0);
$tableRows[5]=array("id"=>6,"id_city_a"=>2,"id_city_b"=>3,"distance"=>9);
$tableRows[6]=array("id"=>7,"id_city_a"=>3,"id_city_b"=>1,"distance"=>6);
$tableRows[7]=array("id"=>8,"id_city_a"=>3,"id_city_b"=>2,"distance"=>9);
$tableRows[8]=array("id"=>9,"id_city_a"=>3,"id_city_b"=>3,"distance"=>0);
$counter=0;
$result=array();
foreach($tableRows as $tableRow)
{
$result[$tableRow["id_city_a"]][$tableRow["id_city_b"]]=array("id_city_a"=>$tableRow["id_city_a"],"id_city_b"=>$tableRow["id_city_b"],"distance"=>$tableRow["distance"] );
}
now in $result[cityA][cityB]=distance
get unique_id_city_a for column
$unique_id_city_a = array_unique(array_map(function ($i) { return $i['id_city_a']; }, $tableRows));
echo "\t";
foreach($unique_id_city_a as $R)
{
echo $R."\t";
}
echo "\n";
get unique_id_city_b for rows
$unique_id_city_b = array_unique(array_map(function ($i) { return $i['id_city_b']; }, $tableRows));
foreach($result as $R1)
{
echo $unique_id_city_b[$counter++]."\t";
foreach($R1 as $R2)
{
echo $R2["distance"]."\t";
}
echo "\n";
}
I'm upgrading all my mysql_* to PDO. I can handle simple SELECTs and INSERTs. But I have one complex SELECT using nested loops that I am making a dog's dinner of. I'll post the mysql_* code, which outputs a two-column table with total registrations at A1 level in the first column and total registrations at A1 level who have paid in brackets in the second column.
-------------------------
Smithsville : A1 | |
10 | (7) |
-------------------------
Grange : A1 | |
4 | (4) |
-------------------------
Beau Ridge : A1 | |
23 | (16)|
-------------------------
Jonesboro : A1 | |
9 | (9) |
-------------------------
Lexing : A1 | |
3 | (1) |
-------------------------
In the full application further tables are genertated across the page for the other levels.
$levels = array('A1', 'A2', 'B1', 'B2');
$centres = array('Smithsville', 'Grange', 'Plateau Ridge', 'Jonesboro', 'Lexing');
for ($j = 0; $j < 1; $j++) /* Does not loop; selects only A1 in queries below*/
{
for ($k=0; $k<5; $k++) /* Cycles through the centres */
{
$show_count = mysql_query("SELECT COUNT(Centre)
FROM exam
WHERE exam.Centre='".$centres[$k]."'
AND exam.Level='".$levels[$j]."'");
$show_paid = mysql_query("SELECT COUNT(Centre)
FROM exam
JOIN personal
ON exam.P_ID=personal.P_ID
WHERE personal.Paid='1'
AND exam.Centre='".$centres[$k]."'
AND exam.Level='".$levels[$j]."'");
$result_rows = mysql_num_rows($show_count);
$result_rows2 = mysql_num_rows($show_paid);
if ($result_rows == 0)
{ echo 'No registrations';}
if ($result_rows2 == 0)
{ echo 'No payments';}
echo '<table class="Font3White">
<tr class="Title">
<td class="width8">'.$centres[$k].' : '.$levels[$j].'</td>
<tr>';
while (($row = mysql_fetch_row($show_count)) && ($row2 = mysql_fetch_row($show_paid)))
{
foreach ($row as $key => $value)
{
echo '<td>';
echo $value;
echo '</td>';
}
foreach ($row2 as $key2 => $value2)
{
echo '<td> (';
echo $value2;
echo ')</td>';
}
echo '</tr>';
}
echo '</table>';
}
}
My PDO attempt is
for ($j = 0; $j < 1; $j++) /* Does not loop; selects only A1 */
{
for ($k=0; $k<5; $k++) /* Cycles through the centres */
{
/*//////////////////////////*/
/* Prepares statements */
/*//////////////////////////*/
$stmt1 = $db->query("SELECT COUNT(Centre)
FROM exam
WHERE exam.Centre=:centre
AND exam.Level=:level");
$stmt2 = $db->query("SELECT COUNT(Centre)
FROM exam
JOIN personal
ON exam.P_ID=personal.P_ID
WHERE personal.Paid='1'
AND exam.Centre=:centre
AND exam.Level=:level");
/*/////////////////////*/
/* Binds parameters */
/*/////////////////////*/
$stmt1->bindParam(':centre', $centre, PDO::PARAM_STR);
$stmt1->bindParam(':level', $level, PDO::PARAM_STR);
$stmt2->bindParam(':centre', $centre, PDO::PARAM_STR);
$stmt2->bindParam(':level', $level, PDO::PARAM_STR);
/*//////////////////////////*/
/* Executes statements */
/*//////////////////////////*/
$stmt1->execute(array($centre, $level));
$stmt2->execute(array($centre, $level));
echo '<table class="Font3White">
<tr class="Title">
<td class="width8">'.$centres[$k].' > '.$levels[$j].'</td>
<tr>';
while (($row = $stmt1->fetch(PDO::FETCH_ASSOC)) && ($row2 = $stmt2->fetch(PDO::FETCH_ASSOC)))
{
foreach ($row as $key => $value)
{
echo '<td>';
echo $value;
echo '</td>';
}
foreach ($row2 as $key2 => $value2)
{
echo '<td> (';
echo $value2;
echo ')</td>';
}
echo '</tr>';
}
echo '</table>';
}
}
I know this is terrible but I hope it can be worked with.
I know it's wrong to make you the dirty work, but that's it.
I made some changes to your code, so compare mine with yours.
Hope it helps you.
$levels = array('A1', 'A2', 'B1', 'B2');
$centres = array('Smithsville', 'Grange', 'Plateau Ridge', 'Jonesboro', 'Lexing');
foreach ($levels as $level) {
foreach ($centres as $centre) {
// As you said, PREPARE
$stmt1 = $db->prepare("SELECT COUNT(Centre)
FROM exam
WHERE exam.Centre=:centre
AND exam.Level=:level");
$stmt2 = $db->prepare("SELECT COUNT(Centre)
FROM exam
JOIN personal
ON exam.P_ID=personal.P_ID
WHERE personal.Paid='1'
AND exam.Centre=:centre
AND exam.Level=:level");
$stmt1->bindParam(':centre', $centre, PDO::PARAM_STR);
$stmt1->bindParam(':level', $level, PDO::PARAM_STR);
$stmt2->bindParam(':centre', $centre, PDO::PARAM_STR);
$stmt2->bindParam(':level', $level, PDO::PARAM_STR);
//You don't need to pass parameters here just on PDOStatement::bindParam
$stmt1->execute();
$stmt2->execute();
echo '<table class="Font3White">
<tr class="Title">
<td class="width8">' . $centre . ' > ' . $level . '</td>
<tr>';
// Fetch stuffs
$row = $stmt1->fetch(PDO::FETCH_ASSOC);
$row2 = $stmt2->fetch(PDO::FETCH_ASSOC);
// Don't use while with PDOStatement::fetch, use fetchAll then foreach it.
if ($row && $row2) {
foreach ($row as $key /* <= Do you need it? */ => $value) {
echo '<td>';
echo $value;
echo '</td>';
}
foreach ($row2 as $key2 /* <= Do you need it? */ => $value2) {
echo '<td> (';
echo $value2;
echo ')</td>';
}
echo '</tr>';
}
echo '</table>';
}
}
Can you show us how you set $centre and $level variables? Maybe they are arrays, not strings? If they are - you may have to create new variable and assign value from array to it. Like this:
$value_to_bind = $centres[$k];
Execute does not need parameters if you used bindParam() before.
I never tried to run 2 queries with same parameter names and same variables binded to them. Maybe this is a problem?
I've searched through older threads but haven't yet found a solution for the following issue: is it possible to group and arrange mysql results without resorting to nested queries?
Please see the sample below.
I have:
Month Jan, Location USA, Program DDD
Month Jan, Location UK, Program EEE
Month Jan, Location USA, Program LLL
Month FEB, Location UAE, Program EEE
Month FEB, Location USA, Program DDD
Month FEB, Location UK, Program MMM
Month MAR, Location USA, Program FFF
Month MAR, Location UAE, Program FFF
Month MAR, Location UK, Program FFF
I want them to display like this:
+---------++---------++---------+
|Month Jan||Month Feb||Month Mar|
+---------------+---------++---------++---------+
|Location USA | || || |
+---------------+---------++---------++---------+
| |DDD ||DDD ||FFF |
+---------------+---------++---------++---------+
| |EEE || || |
+---------------+---------++---------++---------+
| |LLL || || |
+---------------+---------++---------++---------+
|Location UK | || || |
+---------------+---------++---------++---------+
| |EEE ||MMM ||FFF |
+---------------+---------++---------++---------+
|Location UAE | || || |
+---------------+---------++---------++---------+
| | ||EEE ||FFF |
+---------------+---------++---------++---------+
I have tried grouping the query but I'm unable to render it to the HTML table.
SELECT
event.`event_id`,
event.`event_program_id`,
event.`event_month`,
event.`event_location_id`,
location.`location_name`,
program.`program_name`,
program.`program_shortname`
FROM
`event`
LEFT JOIN
`location`
ON
event.`event_location_id` = location.`location_id`
LEFT JOIN
`program`
ON
event.`event_program_id` = program.`program_id`;
Right now its listing like this:
column1 column2 column3
line1 34
line2 34
line3 34
line5 34
Where I wanted like this:
column1 column2 column3
line1 34 34
34 34
34
line2 34
34 34 34
line3 34
line5 34
You can do some inner queries to achieve this for each column, if you have a predefined number of columns in the expected result, something like just the months of the year.
I had some experience with reports based on SQL that used subqueries like this, at the end of the day, this solution preved to be a pain to manage :) today I just read the data, store in a matrix in memory and at the end generate the result based on the matrix, much easier to change in the future.
cheers !
here is an example in PHP
<pre>
<?php
$columns = array();
$data = array();
// add the data from the database
$data["line1"]["column1"] += 34;
$data["line2"]["column1"] += 34;
$data["line3"]["column4"] += 34;
$data["line5"]["column2"] += 34;
$data["line1"]["column1"] += 34;
$data["line3"]["column4"] += 34;
// find the columns
foreach ($data as $line => $column) {
foreach ($column as $cname => $value) {
if( ! in_array($cname, $columns) ){
$columns[] = $cname;
}
}
}
sort($columns);
// display column names
echo " \t";
foreach ($columns as $index => $cname) {
echo $cname . "\t";
}
echo "\n";
// display the data
foreach ($data as $line => $column) {
echo $line . "\t";
foreach ($columns as $index => $cname) {
echo $column[$cname] . "\t";
}
echo "\n";
}
?>
</pre>
Here's another way to solve the problem
<pre>
<?php
$columns = array();
$data = array();
// add the data from the database
// sort the data in the SQL
$data[] = array("l" => "line1", "c"=> "column1", "v" => "AAA");
$data[] = array("l" => "line1", "c"=> "column1", "v" => "BBB");
$data[] = array("l" => "line1", "c"=> "column3", "v" => "CCC");
$data[] = array("l" => "line2", "c"=> "column2", "v" => "AAA");
$data[] = array("l" => "line3", "c"=> "column2", "v" => "AAA");
// find the columns
foreach ($data as $line => $column) {
if( ! in_array($column["c"], $columns) ){
$columns[] = $column["c"];
}
}
sort($columns);
// display column names
echo " \t";
foreach ($columns as $index => $cname) {
echo $cname . "\t";
}
echo "\n";
$name = '';
$count = 0;
// display the data
foreach ($data as $line => $column) {
if( $column['l'] == $name ){
$count ++;
} else {
$name = $column['l'];
$count = 0;
}
if( $count == 0 ){
echo $name . "\t";
} else {
echo " \t";
}
foreach ($columns as $index => $cname) {
if( $column['c'] == $cname ){
echo $column['v'] . "\t";
} else {
echo " \t";
}
}
echo "\n";
}
?>
</pre>
other reference using html as output
<table border="1">
<?php
$columns = array();
$data = array();
// add the data from the database
// sort the data in the SQL
$data[] = array("l" => "line1", "c"=> "column1", "v" => "AAA");
$data[] = array("l" => "line1", "c"=> "column1", "v" => "BBB");
$data[] = array("l" => "line1", "c"=> "column3", "v" => "CCC");
$data[] = array("l" => "line2", "c"=> "column2", "v" => "AAA");
$data[] = array("l" => "line3", "c"=> "column2", "v" => "AAA");
// find the columns
foreach ($data as $line => $column) {
if( ! in_array($column["c"], $columns) ){
$columns[] = $column["c"];
}
}
sort($columns);
// display column names
echo "<tr>";
echo "<td> </td>";
foreach ($columns as $index => $cname) {
echo "<td>".$cname."</td>";
}
echo "</tr>";
$name = '';
$count = 0;
// display the data
foreach ($data as $line => $column) {
echo "<tr>";
if( $column['l'] == $name ){
$count ++;
} else {
$name = $column['l'];
$count = 0;
}
if( $count == 0 ){
echo "<td>".$name."</td>";
} else {
echo "<td> </td>";
}
foreach ($columns as $index => $cname) {
if( $column['c'] == $cname ){
echo "<td>".$column['v']."</td>";
} else {
echo "<td> </td>";
}
}
echo "</tr>";
}
?>
</table>
I would like make:
aaa | bbb | ccc | ddd etc
1 | 1 | 1 | 1
2 | 2 | 2 | 2
3 | 3 | 3 | 3
etc
for aaa, bbb etc i use FOREACH
<table><tr>
foreach ($data as $d){
echo "<td>" . $d . "</td>";
}
</tr>
for ($i = 0; $i < 20; $i++){
echo "<tr><td>" . $i . "</td></tr>";
}
but this working not ok. how can i use loop FOR for all data from foreach?
I think you can generate your table like this:
$columns = array('aaa','bbb','ccc','ddd');
$num_cols = count($columns);
echo "<table>";
echo "<tr>";
foreach($columns as $col)
{
echo "<td>$col</td>";
}
echo "</tr>";
for($i=1;$i<20;$i++)
{
echo "<tr>";
for($j=0;$j<$num_cols;$j++)
{
echo "<td>$i</td>";
}
echo "</tr>";
}
echo "</table>";
In general depends on what $data looks like.
<?php
$data = array('aaa', 'bbb', 'ccc', 'ddd'); // Assuming that $data is a columns storage
$rows = 10; // $rows = count($data); if you wish to have same number of columns and rows
echo '<table>';
echo '<tr>';
foreach ($data AS $item)
{
echo '<td>' . $item . '</td>';
}
echo '</tr>';
for ($idx = 0; $idx < $rows; $idx++)
{
echo '<tr>';
for ($col = 1, $col_num = count($data); $col <= $col_num; $col++)
{
echo '<td>' . $idx . '</td>';
}
echo '</tr>';
}
echo '</table>';
?>
P.S. haven't tested the code.