fwrite() command RE: saving information to a txt file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

fwrite() command RE: saving information to a txt file

Post by kendall »

I have a bit of code i made up that will allow visitors to edit information on a text file.
this is the following code :-

function insertRow($j)
{
echo "<td><input name=\"dish[$j]\" value=\"Enter Dish\"></td><td><input name=\"price[$j]\" value=\"Enter Price\"></td><td><textarea name=\"description[$j]\" cols=\"47\" rows=\"10\">Enter Description</textarea></td>";
echo "</tr>";
}
if (!$fp)
{
// Report Errors if Any

echo "<h3>Sorry there has been an error with this $fileText[0] and you could not open this file or the file cannot be found.</h3><br>";
#Mail Error to Me!;
mail("kendall\@apex-solutions.com","Error on Save","Error saving to the $fileText[0]","rafters\@6awarnerst.com");
exit;
}else{
global $fp;
$fileContents = file("$DOCUMENT_ROOT/rafters/menus/$file");
fclose($fp); // Close File
$countItems = count($fileContents);
$fileText = explode(".",$file);

// Print Html OutPut

echo "<html>";
require ("top.htm");
require ("instruct.txt");
echo "<form action=\"save_file.php\" method=\"POST \" enctype=\"text/plain\">";
echo "<table border=1>\n";
echo "<tr>";
echo "<th colspan =3>\n";
echo "<H4>EDIT $fileText[0]</h4></th>\n";
echo "<tr>";
echo "<th>Name of Dish</th> <th>Price</th> <th>Description</th>";
echo "</tr>";
for ($i = 0; $i <$countItems; $i++)
{
$value = explode("|",StripSlashes($fileContents[$i]));
echo "<tr>";
echo "<td><input name=\"dish[$i]\" value=\"$value[0]\"></td><td><input name=\"price[$i]\" value=\"$value[1]\"></td><td><textarea name=\"description[$i]\" cols=\"47\" rows=\"10\">$value[2]</textarea></td>";
echo "</tr>";
}
}
if ($functionCall==1)
insertRow($j);
# pass variable along scripts
echo "<tr>";
echo "<td colspan=3><input name = \"submit\" value=\"submit\" type=\"submit\"></td>";
echo "</tr>";
echo "<input name =\"file\" type=\"hidden\" value=\"$file\">";
echo "</table>\n";
echo "</form>\n";
if ($j=="")
echo "<a href=\"menu_editor.php?file=$fileText[0]&j=$i&functionCall=1\">Add Entry</a>";
echo "</body>\n";

echo "</html>\n";
# END

now i have a code that saves the information back to the txt file in a defined format using the fputs command

// Write to the File
while (list($k,$v) = each($dish)) {
# get each variable in format sequence
$d = $v;
$p = $price[$k];
$ds =$description[$k];
$form = "$d|$p|$ds\r";
@ fputs($fp,$form); # write to file command
}
}
// Output to HTML Page
echo "<html>\n";
require ("top.htm");
echo "<p>Menu Saved successfully. The following is what was saved to the $fileText[0]:-</p>";

# display to Html Page
require("display_menu.php");
# END

the problem is that it keeps saving an extra \n and \r carriage return
i want to know how i can eliminate the problem. also how does the fwrite command and fputs command really work
gnu2php
Forum Contributor
Posts: 122
Joined: Thu Jul 11, 2002 2:53 am

Post by gnu2php »

What if you use a newline character \n instead of a carriage return, like this:
  • $form = "$d|$p|$ds\n";
You could also try removing any \n and \r characters from the data before it is written.

Code: Select all

$form = preg_replace('/&#1111;\r\n]/', ' ', "$d|$p|$ds");
$form .= "\n";

@ fputs($fp,$form);
how does the fwrite command and fputs command really work
I think when you pass a newline character to fwrite() / fputs() in Windows it gets converted to "\r\n" (except in binary mode, if I'm not mistaken). Otherwise, the data is written exactly as you pass to these functions.

Also, according to the PHP Manual:
fputs() is an alias to fwrite(), and is identical in every way
User avatar
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

thanks

Post by kendall »

thanks for the tip man it worked i just couldn't understand how it was writing to the file in the loop. as i know that it is suppose to delete the exsisting content and continue

is that really you?

Kendall
User avatar
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

array function

Post by kendall »

I have another question.
is there an array function that will filter and reset the keys in an array in case an aray is empty.

the scenario is that if the visitor deletes something it will be submitted as an empty array i want to alteast eliminate that array and reset the keys in the main array to suit

Kendall
gnu2php
Forum Contributor
Posts: 122
Joined: Thu Jul 11, 2002 2:53 am

Post by gnu2php »

You can use the unset() function to delete an item from an array. It works like this:

Code: Select all

if (!$array&#1111;'item']) unset($array&#1111;'item'])
Or like this:

Code: Select all

<?php

$array = array('hello', '', 'world');
$keyes = array_keys($array);

foreach ($keyes as $item)
&#123;
	if (!$array&#1111;$item]) unset($array&#1111;$item]);
&#125;

var_dump($array);

?>
The only problem is that the keys will now be '0' and '2' (without the '1' key).
is that really you?
Oh, I'm not Alan Keyes. I actually work for him. I use his picture to make me look smart. :)
User avatar
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

Post by kendall »

Hey thanks :)

i don't think the keys thing is going to be a problem.

I can always use a reset array function to reset the arrays into a proper order right?:lol:

anyway i dont really need the keys as such :wink:
User avatar
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

don't understand why it doesnt work

Post by kendall »

ok i am trying to use your theory but not your style

i have come up with the following code which searches through the a multi-dimensional array to check if the array is empty

code: -
function checkArray(&$form)
{
$compare = array (""," ",NULL);
while (list($key,$value)=each(&$form)
{
$key = array_search("",$key)
if ($key)
{
unset($key);
}
var_dump (&$form);
}
}
array_walk(&$form,checkArray);

end

how ever in running the script i keep getting a T_variable error in line 15

what i am suppose to be doing is using a walk through array to check each array array separte. the while statement runss through the 2nd dimensianl arrays to get the value.

i am searching for the value " " empty in each second dimension array and comaparring it to either null or " " inorder to unset it

to me i think my problem is am repeating the the search through arrays part too many times (my array is 2 dimensianl) but not too sure if that is the problem

what is my problem?
gnu2php
Forum Contributor
Posts: 122
Joined: Thu Jul 11, 2002 2:53 am

Post by gnu2php »

I did notice a couple of parse errors:
  • function checkArray(&$form)
    {
    $compare = array (""," ",NULL);
    while (list($key,$value)=each(&$form)) // Missing )
    {
    $key = array_search("",$key); // Missing ;
    if ($key)
    {
    unset($key);
    }
    var_dump (&$form);
    }
    }
    array_walk(&$form,checkArray);
Just wondering--isn't $value the one with the array?

array_search("",$value)
Post Reply