用PHP创建一个使用数组值的自定义HTML表单 [英] Generate a custom HTML form using array values with PHP

查看:104
本文介绍了用PHP创建一个使用数组值的自定义HTML表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我试图使用PHP在数组中创建一个自定义HTML表单。

PHP代码 $ row ['Key']将包含W,B和R ):

  $ numbers [$ row ['Key']] = array(
'C'=>
array($ b'b'BO'=> $ row ['BO'],
'BT'=> $ row ['BT']),
'D'=>
数组(
'MF'=> $ row ['MF'],
'MT'=> $ row ['MT'])
);

使用PHP制作的数组:

 数组

[W] =>数组

[C] =>数组










$ [MF] => 54
[MT] => 63



[B] =>数组

[C] =>数组

[BO] => 60
[BT] => 105

[D] => ;数组

[MF] => 90
[MT] => 105



[R] =>数组

[C] =>数组

[BO] => 12
[BT] => 21

[D] =>数组

[MF] => 18
[MT] => 24



结果应该如下所示。注意W / B / R和BO / BT / MF / MT的组合。

 < table> 
< tbody>
< tr>
< td> W< / td>
< td>< input type =textname =WBOid =WBOvalue =36>< / td>
< td>< input type =textname =WBTid =WBTvalue =63>< / td>
< td>< input type =textname =WMFid =WMFvalue =54>< / td>
< td>< input type =textname =WMTid =WMTvalue =63>< / td>
< / tr>
< tr>
< td> B< / td>
< td>< input type =textname =BBOid =BBOvalue =60>< / td>
< td>< input type =textname =BBTid =BBTvalue =105>< / td>
< td>< input type =textname =BMFid =BMFvalue =90>< / td>
< td>< input type =textname =BMTid =BMTvalue =105>< / td>
< / tr>
< tr>
< td> R< / td>
< td>< input type =textname =RBOid =RBOvalue =12>< / td>
< td>< input type =textname =RBTid =RBTvalue =21>< / td>
< td>< input type =textname =RMFid =RMFvalue =18>< / td>
< td>< input type =textname =RMTid =RMTvalue =24>< / td>
< / tr>
< / tbody>
< / table>


解决方案

不使用3个foreach循环

  $ var = print_r($ numbers,true); 

$ table = preg_replace_callback(
{。*?[^] [] {4} \ [(\S +)\]。*?[^] [] { 8} \)} s,
func,
$ var);
$ table = preg_replace('{\ n [^<]。*}','',$ table);

echo< table> {$ table}< / table>;

函数:(计数空间以查找数组级别)

 函数func($ matches)
{
$ table ='';
$ k = $ matches [1];
$ t = $匹配[0];

$ tr = preg_replace('{。*?[^] [] {20} \ [(\S +)\]。*?=>。*?(\ S + )} s',PHP_EOL。< td>< input type ='text'id ='{$ k} $ 1'name ='{$ k} $ 1'value ='$ 2'>< / td> ,$ T);

$ table。=< tr>。PHP_EOL;
$ table。=< td> {$ k}< td> {$ tr}。PHP_EOL;
$ table。=< / tr>。PHP_EOL;
返回$ table;

echo

 <表>< TR> 
< td> W< td>
< td>< input type ='text'id ='WBO'name ='WBO'value ='36'>< / td>
< td>< input type ='text'id ='WBT'name ='WBT'value ='63'>< / td>
< td>< input type ='text'id ='WMF'name ='WMF'value ='54'>< / td>
< td>< input type ='text'id ='WMT'name ='WMT'value ='63'>< / td>
< / tr>
< tr>
< td> B< td>
< td>< input type ='text'id ='BBO'name ='BBO'value ='60'>< / td>
< td>< input type ='text'id ='BBT'name ='BBT'value ='105'>< / td>
< td>< input type ='text'id ='BMF'name ='BMF'value ='90'>< / td>
< td>< input type ='text'id ='BMT'name ='BMT'value ='105'>< / td>
< / tr>
< / table>


Problem:

I am trying to generate a custom HTML form with help of values in an array using PHP.

The PHP code ($row['Key'] will contain W, B and R):

$numbers[$row['Key']] = array (
                            'C' => 
                                array (
                                    'BO' => $row['BO'], 
                                    'BT' => $row['BT']),
                            'D' => 
                                array (
                                    'MF' => $row['MF'], 
                                    'MT' => $row['MT'])
                        );

Array produced with PHP:

Array
(
    [W] => Array
        (
            [C] => Array
                (
                    [BO] => 36
                    [BT] => 63
                )
            [D] => Array
                (
                    [MF] => 54
                    [MT] => 63
                )
        )

    [B] => Array
        (
            [C] => Array
                (
                    [BO] => 60
                    [BT] => 105
                )
            [D] => Array
                (
                    [MF] => 90
                    [MT] => 105
                )
        )

    [R] => Array
        (
            [C] => Array
                (
                    [BO] => 12
                    [BT] => 21
                )
            [D] => Array
                (
                    [MF] => 18
                    [MT] => 24
                )
        )
)

The outcome should look like the following. Notice the combination of W/B/R and BO/BT/MF/MT.

<table>
    <tbody>
        <tr>
            <td>W</td>
            <td><input type="text" name="WBO" id="WBO" value="36"></td>
            <td><input type="text" name="WBT" id="WBT" value="63"></td>
            <td><input type="text" name="WMF" id="WMF" value="54"></td>
            <td><input type="text" name="WMT" id="WMT" value="63"></td>
        </tr>
        <tr>
            <td>B</td>
            <td><input type="text" name="BBO" id="BBO" value="60"></td>
            <td><input type="text" name="BBT" id="BBT" value="105"></td>
            <td><input type="text" name="BMF" id="BMF" value="90"></td>
            <td><input type="text" name="BMT" id="BMT" value="105"></td>
        </tr>
        <tr>
            <td>R</td>
            <td><input type="text" name="RBO" id="RBO" value="12"></td>
            <td><input type="text" name="RBT" id="RBT" value="21"></td>
            <td><input type="text" name="RMF" id="RMF" value="18"></td>
            <td><input type="text" name="RMT" id="RMT" value="24"></td>
        </tr>
    </tbody>
</table>

解决方案

without using 3 foreach loops

$var = print_r($numbers, true);

$table =  preg_replace_callback(
            "{.*?[^ ][ ]{4}\[(\S+)\].*?[^ ][ ]{8}\)}s",
            "func",
            $var);
$table= preg_replace('{\n[^<].*}','',$table);   

echo "<table>{$table}</table>";   

function: (count space to find array level)

function func($matches)
{
  $table='';
  $k=$matches[1];
  $t=$matches[0];

  $tr = preg_replace('{.*?[^ ][ ]{20}\[(\S+)\].*?=>.*?(\S+)}s',PHP_EOL."<td><input type='text' id='{$k}$1' name='{$k}$1' value='$2'></td>",$t);

  $table .= "<tr>".PHP_EOL;
  $table .= "<td>{$k}<td>{$tr}".PHP_EOL;
  $table .= "</tr>".PHP_EOL;
   return  $table;
}

echo

<table><tr>
<td>W<td>
<td><input type='text' id='WBO' name='WBO' value='36'></td>
<td><input type='text' id='WBT' name='WBT' value='63'></td>
<td><input type='text' id='WMF' name='WMF' value='54'></td>
<td><input type='text' id='WMT' name='WMT' value='63'></td>
</tr>
<tr>
<td>B<td>
<td><input type='text' id='BBO' name='BBO' value='60'></td>
<td><input type='text' id='BBT' name='BBT' value='105'></td>
<td><input type='text' id='BMF' name='BMF' value='90'></td>
<td><input type='text' id='BMT' name='BMT' value='105'></td>
</tr>
</table>

这篇关于用PHP创建一个使用数组值的自定义HTML表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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