duanguochi6194 2017-07-06 05:07
浏览 284
已采纳

如何使用另一个php文件编辑php文件的变量

Ok so I want to do something similar to how Simple Machine Forum edits some variable within the Admin panel on the forum. For instance I have a settings.php file. I want tool.php to open and find all declared php variables in the settings.php like $background_color='Orange' and output in tools.php Background Color: Orange inside of a form tag that, once changed to 'Purple' and submitted, replaces 'Orange' with 'Purple' and now if i open 'settings.php' the variable inside the file is now "$background_color='Purple'" without having to go in an manually edit it. I can just submit the new data for the variable and replace the old.

  • 写回答

3条回答 默认 最新

  • doqau82086 2017-07-06 08:33
    关注

    To keep the data persistent, you will need to store data in a database or separate file e.g. JSON file. Then you can use that data everywhere it is needed.

    Here is an example using a JSON file.

    setting_variables.json:

    {
        "background": "red",
        "color": "white"
    }
    

    settings.php

    $settings = json_decode(file_get_contents("settings_variables.json"), true);
    $background = $settings['background'];
    

    tools.php

    include ("settings.php");
    
    // If form is posted
    if(isset($_POST['submit'])){
        /* Updating variables in JSON file*/
    
        //Get settings_json file
        $setting_vars = json_decode(file_get_contents('settings_variables.json'), true);
    
        // Update variable
        $background = $setting_vars['background'] = $_POST['color'];
    
        // Store variable value to JSON file
        file_put_contents("settings_variables.json", json_encode($setting_vars));
    }
    
    ?>
    
    <form method="post" <?php echo "style='background-color:".$background . "'"; ?>>
        <select name="color">
        <option value="red">red</option>
        <option value="blue">blue</option>
        <option value="orange">orange</option>
        <option value="yellow">yellow</option>
        <option value="purple">purple</option>
      </select>
      <input type="submit" name="submit"/>
    </form>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 MddBootstrapInitialize2失败
  • ¥15 LCD Flicker
  • ¥15 Spring MVC项目,访问不到相应的控制器方法
  • ¥15 esp32在micropython环境下使用ssl/tls连接mqtt服务器出现以下报错Connected on 192.168.154.223发生意外错误: 5无法连接到 MQTT 代理,如何解决?
  • ¥15 关于#genesiscsheel#的问题,如何解决?
  • ¥15 Android aidl for hal
  • ¥15 STM32CubeIDE下载程序报错
  • ¥15 微信好友如何转变为会员系统?(相关搜索:小程序)
  • ¥15 c# 直接使用c++ 类库文件
  • ¥15 一个主机电脑有两个显示器,当前有两个软件主体是网页html打包的exe程序,如何通过cmd命令bat同时打开软件一个在主屏幕显示,另外一个软件在2副屏幕上显示