Jump to content

Form to Flat File


ministrypixel

Recommended Posts

Below is a php script that helps with what I am trying to do.  What I am trying to do is have a place where the person can go in, post a goal (or edit exsiting goal) and put yes or no if the goal has been achieved.  Everything works fine, but when you click on an existing goal and put yes or no, the output is "1".  I don't know how to fix this.  There are also a couple of things I would like to add/change in the script that maybe someone can help me with.  I would like it where there is a text box for the goal, a drop down menu with what semester week it is (week 1, week 2, week 3, week 4), a yes or no drop down, and for the date the data is submitted to be automatically included.  Then where the data is shown it will list data as follows:  week / date / goal / achieved (yes or no)  and have it organized by week and date.  I hope this makes sense.  Maybe someone can help me achieve this.  Thank you so much for your help.  Code is below.  You can view it live at http://www.ministrypixel.com/thealtar/demo/admin/goals_edit.php

 

<?php
# initialise variables
$goal = $achieved = "";
$data = array();
$changed = false;

# define your data file and its path, ensure you have write permission to folder
$myTextFile = '../goals.txt';

# get the data from current file
    if (file_exists($myTextFile))
        $data = parse_ini_file($myTextFile, true);


# check if user data form submitted
if (isset($_POST['userdata'])) {
if (!empty($_POST['goal'])  ) {
        # add new data (or update if exists already)
        $data[$_POST['goal']]['achieved'] = $_POST['achieved'];

    
        $changed = true;
}
else echo "<p>Must have username and email</p>";
}

# do we want to delete a record
if (isset($_GET['delid'])) {

    # remove deleted item from the array
    unset($data[$_GET['delid']]);
    $changed = true;
}

# or do we want to edit a record
if (isset($_GET['changeid'])) {
        # get the items we want to edit so they can appear in the form
        $goal = $_GET['changeid'];
        $achieved = $data[$goal]['achieved'];

}

if ($changed) {
    # now create new output file, writing in ini file format
    $fp = fopen($myTextFile, 'w');

    ksort($data);
    foreach ($data as $key=>$dataArray) {
             fwrite($fp, "[$key]\n");
             foreach ($dataArray as $k => $v) {
                      fwrite($fp, "$k=$v\n");
             }
             fwrite($fp, "\n");
    }
    fclose($fp);
}

# function to list the current data
function listData($data) {
         if (count($data) > 0) {
             echo "<TABLE class='edit' border='0' cellspacing='1' cellpadding='2'>\n";
             # headings
             echo "<tr><th>Goal</th>";
             list ( $key, $dataArray) = each($data);
             foreach($dataArray as $k=>$v) {
                     echo "<th>$k</th>";
             }
             echo "<th></th></tr>";

             #data
             $i = 0;
             foreach ($data as $key=>$dataArray) {
                     $bg = ++$i % 2 ? "#444444" : "#555555";
                     echo "<tr style='background: $bg'>
                            <td>
                            <a href='$_SERVER[php_SELF]?changeid=$key'>$key<a>
                            </td>";
                     foreach ($dataArray as $v) {
                               echo "<td>$v</td>";
                     }
                     echo "<td><a href='$_SERVER[php_SELF]?delid=$key'><a></td></tr>";
            }
            echo "</TABLE>";
        }
        else echo "<p>No data</p>";
}
?>
<? include "header.php"; ?>
<b>SPIRITUAL GOALS</b><br><br>
<FORM  method="POST" action="<?php echo $_SERVER['PHP_SELF']?>">
<INPUT TYPE="HIDDEN"  name="userdata" value="1">
<?php
if (!empty($goal))
     echo "<INPUT TYPE=\"HIDDEN\"  name=\"goal\" value=\"$goal\">";
?>
<B>Goal</b><br>  <INPUT TYPE="TEXT"  name="goal" size="50" 
                value='<?php echo $goal ?>'
                <?php echo  empty($goal) ? '' : ' DISABLED ' ?> >

<br>
<B>Achieved</b><br>  <INPUT TYPE="TEXT"  name="achieved" size="20" maxlength="20" value='<?php echo $achieved ?>'>
<br>
    <INPUT TYPE="SUBMIT"  value="Save">
    <?php
    if (!empty($goal))
         echo "<INPUT TYPE=\"BUTTON\"  value=\"Cancel\"   onClick=\"history.go(-1);\">";
    ?>


</FORM>

<?php
     # if we are not editing a record, list the data
     if (!isset($_GET['changeid'])) {
          listData($data);
     }
?>
<br>
For goals with "**" please write a specific answer including yes or no if you achieved the goal.
<? include "footer.php"; ?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.