PHP - Insert Data to INI File
Submitted by razormist on Tuesday, August 28, 2018 - 14:31.
In this tutorial we will create a Insert Data to INI File using PHP. PHP is a server-side scripting language designed primarily for web development. It is a lean and consistent way to access databases. This means developers can write portable code much easier. It is mostly used by a newly coders for its user friendly environment. So Let's do the coding...
There you have it we successfully created Insert Data to INI File using PHP. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!
Getting started:
First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html. Lastly, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.Creating The Interface
This is where we will create a simple form for our application. To create the forms simply copy and write it into your text editor, then save it as index.php.- <?php require 'generate_ini.php'?>
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
- <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
- </head>
- <body>
- <nav class="navbar navbar-default">
- <div class="container-fluid">
- <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
- </div>
- </nav>
- <div class="col-md-3"></div>
- <div class="col-md-6 well">
- <h3 class="text-primary">PHP - Insert Data to INI File</h3>
- <hr style="border-top:1px dotted #ccc;"/>
- <form method="POST" action="create_data.php" class="form-inline">
- <label>Enter a Data</label>
- <input type="text" class="form-control" name="data" required="required"/>
- <button class="btn btn-success" name="add"><span class="glyphicon glyphicon-plus"></span> Add Data</button>
- </form>
- <br />
- <table class="table table-bordered">
- <thead class="alert-info">
- <?php
- $file = "config.ini";
- $parse = parse_ini_string($section[0], false, INI_SCANNER_RAW);
- ?>
- <tr>
- <th colspan="2"><center>Start Settings</center></th>
- </tr>
- </thead>
- <tbody style="background-color:#fff;">
- <?php
- foreach($parse as $data => $list){
- ?>
- <tr>
- <td><?php echo $data?></td>
- <td><?php echo $list?></td>
- </tr>
- <?php
- }
- ?>
- </tbody>
- </table>
- <table class="table table-bordered">
- <thead class="alert-warning">
- <tr>
- <th colspan="2"><center>Manual Settings</center></th>
- </tr>
- </thead>
- <tbody>
- <?php
- $parse2 = parse_ini_string($section[1], true, INI_SCANNER_RAW);
- foreach($parse2 as $head => $list){
- ?>
- <tr class="alert-success">
- <td colspan="2"><center><?php echo $head?></center></td>
- </tr>
- <?php
- foreach($list as $key => $value){
- ?>
- <tr style="background-color:#fff;">
- <td><?php echo $key?></td>
- <td><?php echo $value?></td>
- </tr>
- <?php
- }
- }
- ?>
- </tbody>
- </table>
- </div>
- </body>
- </html>
Creating the Ini File
This code contains a function for creating the ini file. This code will generate an ini file to the corresponded directory. To do that just copy and write these block of codes inside the text editor, then save it as generate_ini.php- <?php
- $file = "./config.ini";
- "# Start Settings",
- "system_clean=true",
- "system_read_data=ALL",
- "system_count=10",
- "",
- "# Manual Settings"
- );
- }
- ?>
Creating the Main Function
This code contains the main function of the application. This code will save the data inputs to the ini file. To this just copy and write down these codes inside the text editor, then save it as create_data.php- <?php
- $file = "config.ini";
- $parse = parse_ini_string($section[0], false, INI_SCANNER_RAW);
- $content = "";
- "",
- "[".$_POST['data']."]",
- );
- $content .= "# Start Settings\n";
- foreach($parse as $head => $list){
- $content .= $head."=".$list."\n";
- }
- $parse2 = parse_ini_string($section[1], true, INI_SCANNER_RAW);
- $content .= "\n# Manual Settings\n";
- foreach($parse2 as $head => $list){
- $content .= "\n[".$head."]\n";
- foreach($list as $key => $value){
- $content .= $key."=".$value."\n";
- }
- }
- foreach($new_array as $value ){
- $content .= $value."\n";
- }
- return false;
- }
- }
- ?>
Add new comment
- 166 views