Jump to content

Saving data to a flat file


duffman452001

Recommended Posts

Let me start off by saying I know very little about PHP.  This is card I borrowed from another thread and modified to fit my needs.

 

http://hardforum.com/archive/index.php/t-914963.html

 

So far I've been able successfully modify it to the point where it is usable for my needs.  I'm having trouble with my most recent addition.  I am trying to add a second text input box to store some separate information, and have it save to the text file.  Originally the file stored 3 values per line but now I want it to save this 4th value from the textbox but I guess I'm not understanding how to get it to save properly.  Any help would be appreciated

 


<HEAD>
<TITLE>Kent County Systems Status Page</TITLE>
<STYLE type="text/css">
BODY {text-align: center}
</STYLE>
<BODY>
<b><font size=24>Kent County Applications <br> and Services Status Page</font></b>
<br>
<br>

<?
$ff="statusdb.txt";

ini_set('display_errors', 'Off');
ini_set('display_startup_errors', 'Off');
error_reporting(0);

chmod("inoutdb.txt",0755);

if (isset($_POST['update'])) {
foreach ($_POST as $name=>$value) {

if ($value=="on" || $value=="po" || $value=="of" ) {
$somecontent.= $value . "\n";
}
elseif ($value=='update' || $value=='Update') {
}
else {
$somecontent.= $value . ",";
}

}
$handle=fopen($ff, "w+");
if (fwrite($handle, $somecontent) === FALSE) {
echo "Cannot write to file ($ff)";
exit;
}
fclose($ff);
}

display_file($ff);

//Functions------------------------
function display_file($file){
//Put file into $contents
$contents = file_get_contents($file);

//Split each file row into an array value
$contents=explode("\n",$contents);

// You could try using $PHP_SELF instead of $_SERVER['PHP_SELF'], but if the page is only being accessed through IE you can just leave it.

$i=0;
echo "<form action='".$PHP_SELF."' method='POST'><table>";
echo "<tr><td colspan=2><b>Application/Service</td><td><b>Description</td><td><b>Up/Down</td></tr>";
foreach ($contents as $item){
$person=explode(",",$item);
if ($person[0]=="" || $person[2]==""){
}
else {
echo "
<tr><td><input type='hidden' name='$i' value='$person[0]'>$person[0]</td><td>";
if (substr($person[2],0,2)=='on') {
$mark1='CHECKED';
$mark2='';
$mark3='';
$color = "#00CC66";
}
if (substr($person[2],0,2)=='po') {
$mark1='';
$mark2='CHECKED';
$mark3='';
$color = "#CCFF00";
}
if (substr($person[2],0,2)=='of') {
$mark1='';
$mark2='';
$mark3='CHECKED';
$color = "#FF0000";
}
echo "<td><input type='text' size='100' height='40px' name='".$i."t' value='".$person[1]."' style='background-color:$color'></td>";
echo "<td><input type='text' size='10' height='40px' name='".$i."t' value='".$person[3]."' ></td>";
echo "<td><input type='radio' name='".$i."c' value='on' $mark1>Up <input type='radio' name='".$i."c' value='po' $mark2>Partial Outage<input type='radio' name='".$i."c' value='of' $mark3>Down</td></tr>";
$i+=1;
}
}
// Put the Update button under it all
echo "<tr><td><input type='hidden' name='update' value='update'><input type='submit' name='Submit' value='Update'></td></tr>";

echo "</table></form>";
}
?>
[/Code]

Link to comment
Share on other sites

1) I just tried your code and I can't get it to work. You have too many things wrong.

 

2) It should display the form if the file is blank to enter the data so you can at least be able to start the text file.

 

3) You should turn on the error checking so it will give you some idea of what is going on like... $somecontent not defined and using $php_self instead of $_SERVER['PHP_SELF'].

 

4) You need to set the script to see if the $_POST vars are set by using:

if(isset($_POST['submit']) {

if so then add them together like this to write to the file:

$somecontent .= $_POST['var1name'].'your_choice_of_delimiter'.$_POST['var2name'].'your_choice_of_delimiter'.$_POST['var3name'].'your_choice_of_delimiter'.$_POST['var4name']."/n";

} else {

If not, then show the input form

}

Also it looks like your are overwriting the file however if you need the data from before then you need to append the file not write to the file.

 

5) You are setting your input names to the same name using the $i. So basically you are overwriting the values so you technically only have one variable name. You need to make all four of them unique so you can call them in your $_POST like above.

 

6) To add 1 to a var just do this $i++; you have $i+=1;

 

 

Link to comment
Share on other sites

in your original post (in the other forum) you said:

I'm set on learning php now. I have a ton of things I want to do. It's all in my head, and I want to get it out.....I just have a lot to learn.

 

I would start by trying to understand exactly what your code is doing, (and why it's kind of messy). Once you understand this, you will be able to add all the fields you want.

Link to comment
Share on other sites

in your original post (in the other forum) you said:

I'm set on learning php now. I have a ton of things I want to do. It's all in my head, and I want to get it out.....I just have a lot to learn.

 

I would start by trying to understand exactly what your code is doing, (and why it's kind of messy). Once you understand this, you will be able to add all the fields you want.

 

Oh I actually never posted in that thread, I just borrowed from whatever they had worked out.

 

1) I just tried your code and I can't get it to work. You have too many things wrong.

 

2) It should display the form if the file is blank to enter the data so you can at least be able to start the text file.

 

3) You should turn on the error checking so it will give you some idea of what is going on like... $somecontent not defined and using $php_self instead of $_SERVER['PHP_SELF'].

 

4) You need to set the script to see if the $_POST vars are set by using:

if(isset($_POST['submit']) {

if so then add them together like this to write to the file:

$somecontent .= $_POST['var1name'].'your_choice_of_delimiter'.$_POST['var2name'].'your_choice_of_delimiter'.$_POST['var3name'].'your_choice_of_delimiter'.$_POST['var4name']."/n";

} else {

If not, then show the input form

}

Also it looks like your are overwriting the file however if you need the data from before then you need to append the file not write to the file.

 

5) You are setting your input names to the same name using the $i. So basically you are overwriting the values so you technically only have one variable name. You need to make all four of them unique so you can call them in your $_POST like above.

 

6) To add 1 to a var just do this $i++; you have $i+=1;

 

 

 

I'll try some of these.  Like I said I know every little about PHP, aka pretty much nothing.  I have a little experience in VB but just enough so it kind of helps me understand whats going on in this PHP code.  Pardon my complete lack of knowledge on the subject

Link to comment
Share on other sites

in your original post (in the other forum) you said:

I'm set on learning php now. I have a ton of things I want to do. It's all in my head, and I want to get it out.....I just have a lot to learn.

 

I would start by trying to understand exactly what your code is doing, (and why it's kind of messy). Once you understand this, you will be able to add all the fields you want.

 

The code is supposed to store information about each system in a line in a text file.  Originally in each line there were 3 things stored; The system name, a description in the form of text from a text box, and which of the 3 radio buttons is selected.  What I want is text from a second text box to be stored as a 4th variable in each line of the text file.

 

Original code stored date as: System name, text from text box 1, one of three radio button states

New code stores data as: System name, text from text box 1, text from text box 2, one of three radio button states

 

First of all turn your error reporting on and see if you get some errors.

 

ini_set('display_errors', 'On');
ini_set('display_startup_errors', 'On');
error_reporting(-1);

 

After removing this line, I get the following error message when update is clicked

Warning: fclose() expects parameter 1 to be resource, string given in C:\Web\StatusUpdate.php on line 35

Link to comment
Share on other sites

That's because you are trying to close a file that isn't open. You need to think about what you want to do with the script and logically work through it...

set vars,

display header

check for submit

if yes, then get data from post and order it, then open file and write / append data to it, close file.

If no, then show form

display footer

 

I'm off to work...

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.