[英]Generate HTML Table from textarea data with PHP
我想在PHP中创建一个Table生成器,它将每个新行放在textarea表单字段中,并将每一行计为一个新的表格单元格。 每个textarea将计为一个新的表列。
因此,根据下面的HTML表单,它有4个textarea字段,这将导致一个包含4列<tr>
<form action="" id="tablegenform" method="post">
<textarea name="table-column[]">Enter table column row cell data on each line</textarea>
<textarea name="table-column[]">Enter table column row cell data on each line</textarea>
<textarea name="table-column[]">Enter table column row cell data on each line</textarea>
<textarea name="table-column[]">Enter table column row cell data on each line</textarea>
<input type="submit">
</form>
使我在代码中包围我的头部更困难的部分是textarea中的每个新行项目都是新的表格行。
我的第一种方法是这样的......
$tableColumnsData = isset($_POST['table-column'])?$_POST['table-column']:"";
foreach($tableColumnsData as $columnKey => $columns){
// turns each new line in textarea into array item
$columnRowItems = explode("\n", str_replace("\r", "", $columns));
// $columnRowItems now holds each row item for that column in an array
// this is where my logic breaks
// I need to generate a table row and populate the 4 columns on each row
// instead my code makes a new column for each items in textarea
// and only 4 rows instead of 4 columns and unlimited rows
echo '<tr>';
foreach($columnRowItems as $tkey => $tval){
echo '<td>'.$tval.'</td>';
}
echo '</tr>';
}
echo '</table>';
如何重新编写PHP以我描述的方式工作。
每个textarea字段都是Table列
textarea上的每一行都是该列中的新行
我现在正在使用此代码工作....欢迎改进!
<?php
if(isset($_POST['table-column'])) {
$tableColumnsData = $_POST['table-column'];
$columnCounts = array();
$tableDataArray = array();
echo '
<table border="1">
<thead>
<tr>';
// get count of rows from each column to determine highest number so our
// table will have this many rows
foreach($tableColumnsData as $columnKey => $rows){
echo "<th>$columnKey</th>";
$columnRowData = explode("\n", str_replace("\r", "", $rows));
$columnCounts[] = count($columnRowData);
$tableDataArray[] = $columnRowData;
}
$numberOfTableColumns = count($tableColumnsData);
$numberOfTableRows = max($columnCounts);
echo '</tr>
</thead>
<tbody>';
for ($rowCounter = 0; $rowCounter < $numberOfTableRows; $rowCounter++) {
echo '<tr>';
for ($columnCounter = 0; $columnCounter < $numberOfTableColumns; $columnCounter++) {
if(isset($tableDataArray[$columnCounter][$rowCounter]) && $tableDataArray[$columnCounter][$rowCounter] != ''){
echo '<td>'.$tableDataArray[$columnCounter][$rowCounter].'</td>';
}else{
echo '<td></td>';
}
}
echo '</tr>';
}
echo '
</tbody>
</table>
';
}
?>
<form action="" id="tablegenform" method="post">
<textarea name="table-column[col1]">
Enter one row on each line for this table column.
</textarea>
<textarea name="table-column[col2]">
Enter one row on each line for this table column.
Here's another Line.
</textarea>
<textarea name="table-column[col3]">
Enter one row on each line for this table column.
</textarea>
<textarea name="table-column[col4]">
Enter one row on each line for this table column.
This is a line.
Here's another row cell.
</textarea>
<textarea name="table-column[col5]">
Enter one row on each line for this table column.
</textarea>
<input type="submit">
</form>
结果输出:
<table border="1"> <thead> <tr><th>col1</th><th>col2</th><th>col3</th><th>col4</th><th>col5</th></tr> </thead> <tbody><tr><td> Enter one row on each line for this table column.</td><td> Enter one row on each line for this table column.</td><td> Enter one row on each line for this table column.</td><td> Enter one row on each line for this table column.</td><td> Enter one row on each line for this table column.</td></tr><tr><td></td><td> Here's another Line.</td><td></td><td> This is a line.</td><td>Again</td></tr><tr><td></td><td></td><td></td><td> Here's another row cell.</td><td>and again</td></tr><tr><td></td><td></td><td></td><td></td><td>again</td></tr> </tbody> </table> <form action="" id="tablegenform" method="post"> <textarea name="table-column[col1]"> Enter one row on each line for this table column. </textarea> <textarea name="table-column[col2]"> Enter one row on each line for this table column. Here's another Line. </textarea> <textarea name="table-column[col3]"> Enter one row on each line for this table column. </textarea> <textarea name="table-column[col4]"> Enter one row on each line for this table column. This is a line. Here's another row cell. </textarea> <textarea name="table-column[col5]"> Enter one row on each line for this table column. </textarea> <input type="submit"> </form>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.