如何从用户输入动态创建表? [英] How do I create tables dynamically from user input?

查看:78
本文介绍了如何从用户输入动态创建表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做的是创建一个用户输入的行和列的表,但我不明白如何做到这一点。



Ex。

 用户输入:
行数:4
列数:5

这是我想以html格式显示的行和列。

 <?php 
$ table ='';
if($ _POST){
$ table。='< table border =1>';
for($ i = 0; $ i <$ _POST ['qty_line']; $ i ++){
$ table。='< tr>';
for($ j = 0; $ j <$ _POST ['qty_colunn']; $ j ++){
$ table。='< td width =50>& nbsp; < / TD>';
}
$ table。='< / tr>';
}
$ table。='< / table>';
}
?>
< form action =method =post>
< table border =0width =200>
< tr>
< td width =80>< label>列< / label>< / td>
< td width =120>< input type =textname =qty_colunn>< / td>
< / tr>
< tr>
< td>< label>行< / label>< / td>
< td>< input type =textname =qty_line>< / td>
< / tr>
< tr>
< td colspan =2align =right>< input type =submitvalue =Create Table>< / td>
< / tr>
< / table>
< / form>
< br />
< br />
<?php
echo $ table;
?>


Want I want to do is create a table with what the user input for rows and columns, but I don't understand how to do this.

Ex.

User inputs:
number of rows: 4
number of columns: 5

this are the rows and columns that I want to appear in a html format.

解决方案

Copy and paste into a php page, place the column and lines in text fields click the Create Table button, keep testing and looking at the source code that one day you'll understand.

<?php
    $table = '';
    if ($_POST) {
        $table .= '<table border="1">';
        for ($i = 0; $i < $_POST['qty_line']; $i++) {
            $table .= '<tr>';
            for ($j = 0; $j < $_POST['qty_colunn']; $j++) {
                $table .= '<td width="50">&nbsp;</td>';
            }
            $table .= '</tr>';
        }
        $table .= '</table>';
    }
?>
    <form action="" method="post">
        <table border="0" width="200">
            <tr>
                <td width="80"><label>Column</label></td>
                <td width="120"><input type="text" name="qty_colunn"></td>
            </tr>
            <tr>
                <td><label>Line</label></td>
                <td><input type="text" name="qty_line"></td>
            </tr>
            <tr>
                <td colspan="2" align="right"><input type="submit" value="Create Table"></td>
            </tr>
        </table>    
    </form>
    <br />
    <br />
<?php
    echo $table;
?>

这篇关于如何从用户输入动态创建表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆