代码之家  ›  专栏  ›  技术社区  ›  Halit YILMAZ

编辑包含数组的php页面?

  •  0
  • Halit YILMAZ  · 技术社区  · 7 年前

    我正在尝试用php开发一个网页。我在包含数组(其中的键和值)的php文件上保留了一些翻译。是否可以将数组加载到表单中,并在表单提交时编辑键的值?

    非常感谢。

    1 回复  |  直到 7 年前
        1
  •  0
  •   Brian Otto    7 年前

    下面是一个简化的示例,说明如何做到这一点。。。

    翻译。php

    <?php
    $string["hello"] = "Hello";
    $string["world"] = "World";
    ?>
    

    编辑php

    <?php
    if ($_POST) {
        $translations = '<?php' . PHP_EOL;
    
        foreach ($_POST as $name => $value) {
            $translations .= '$string["' . $name . '"] = "' . htmlentities($value) . '";' . PHP_EOL;
        }
    
        $translations .= '?>' . PHP_EOL;
    
        file_put_contents('translations.php', $translations);
    }
    
    require_once('translations.php');
    ?>
    
    <!doctype html>
    <html>
    
    <body>
        <form action="edit.php" method="post">
    
        <table cellpadding="5" cellspacing="0">
        <?php foreach ($string as $name => $value) { ?>
        <tr>
            <td align="right">
                <strong><?php echo $name; ?></strong>
            </td>
            <td>
                <input type="text" name="<?php echo $name; ?>" value="<?php echo $value; ?>" />
            </td>
        </tr>
        <?php } ?>
        <tr>
            <td colspan="2" align="right">
                <input type="submit" value= "Save Changes" />
            </td>
        </tr>
        </table>
    
        </form>
    </body>
    
    </html>