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
fwrite() command RE: saving information to a txt file
Moderator: General Moderators
What if you use a newline character \n instead of a carriage return, like this:
Also, according to the PHP Manual:
- $form = "$d|$p|$ds\n";
Code: Select all
$form = preg_replace('/ї\r\n]/', ' ', "$d|$p|$ds");
$form .= "\n";
@ fputs($fp,$form);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.how does the fwrite command and fputs command really work
Also, according to the PHP Manual:
fputs() is an alias to fwrite(), and is identical in every way
- kendall
- Forum Regular
- Posts: 852
- Joined: Tue Jul 30, 2002 10:21 am
- Location: Trinidad, West Indies
- Contact:
thanks
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
is that really you?
Kendall
- kendall
- Forum Regular
- Posts: 852
- Joined: Tue Jul 30, 2002 10:21 am
- Location: Trinidad, West Indies
- Contact:
array function
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
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
You can use the unset() function to delete an item from an array. It works like this:
Or like this:
The only problem is that the keys will now be '0' and '2' (without the '1' key).

Code: Select all
if (!$arrayї'item']) unset($arrayї'item'])Code: Select all
<?php
$array = array('hello', '', 'world');
$keyes = array_keys($array);
foreach ($keyes as $item)
{
if (!$arrayї$item]) unset($arrayї$item]);
}
var_dump($array);
?>Oh, I'm not Alan Keyes. I actually work for him. I use his picture to make me look smart.is that really you?
- 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
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?
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?
I did notice a couple of parse errors:
array_search("",$value)
- 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);
array_search("",$value)