Page 1 of 1
Remove an Array from file
Posted: Fri Aug 02, 2002 7:15 pm
by Zeceer
Test 750g|It's The Best|249,-|test1
Test 1500g|It's The Best|399,-|test2
Test 3000g|It's The Best|699,-|test3
This is a file called "produkter.php". What I need help making is a script that can delete one of the arrays. I'm gonna make a form where you select the array you would like to delete.
Posted: Sat Aug 03, 2002 3:46 am
by gnu2php
Here's some code I just threw together. This might give you some idea of what you could do.
(Copy & paste the code below.)
1. Specify the file that has the data. Then call process_form() if the form was submitted:
Code: Select all
<?php
$data_file = 'info.txt';
if (!empty($_POSTї'is_sent'])) process_form();
2. Get the items from the file, and then print each of them in a form:
Code: Select all
print "<form action="$_SERVERїPHP_SELF]" method="POST">\n";
$data = file($data_file);
$count = count($data);
$pos = 0; // Index of current item
for ($x = 0; $x < $count; $x++)
{
$dataї$x] = trim($dataї$x]); // Since file() includes the newlines \n
// Print item & checkbox:
if (!empty($dataї$x]))
{
print "<input type="checkbox" name="item".$pos++."">";
print "$dataї$x]<br><br>\n";
}
}
print "<input type="hidden" name="is_sent" value="1">\n";
print "<input type="submit">\n";
print "</form>\n";
3. After the form is submitted, remove the items the user selected:
Code: Select all
function process_form()
{
$fp = fopen($GLOBALSї'data_file'], 'r+');
$data = fread($fp, filesize($GLOBALSї'data_file']));
// Get each item:
$data = preg_replace("/ї\r\n]+/", "\n", $data);
$array = explode("\n", $data);
ftruncate($fp, 0); // Erase the file
fseek($fp, 0); // Go to start of file
$count = count($array);
$pos = 0; // Index of current item
// Write items NOT selected:
for ($x = 0; $x < $count; $x++)
{
if (!empty($arrayї$x]) && empty($_POSTї'item'.$pos++]))
{
fwrite($fp, $arrayї$x]."\n");
}
}
fclose($fp);
// Redirect back to the form:
header("Location: http://$_SERVERїHTTP_HOST]$_SERVERїPHP_SELF]");
exit();
}
?>
Posted: Sat Aug 03, 2002 5:52 am
by Zeceer
Tanx, I'll study it

Posted: Sat Aug 03, 2002 6:10 am
by Zeceer
I get this error:
Warning: Undefined variable: _SERVER in D:\array cart\delete.php on line 9
The second thing is that it doesn't delete the selected items. It only removes the selected radio-buttons. The only thing i changed in the script was the data_file on top of the file. Is there more that needs to be changed?
Posted: Sat Aug 03, 2002 8:36 am
by hob_goblin
try changing, $_SERVER to $HTTP_SERVER_VARS ...
Posted: Sat Aug 03, 2002 9:36 am
by Zeceer
That thru away the error message, but the script still doesn't remove the arrays that i select it to remove.
Posted: Sat Aug 03, 2002 9:50 am
by hob_goblin
Code: Select all
<?php
$data_file = 'info.txt';
if (!empty($HTTP_POST_VARSї'is_sent'])) process_form();
print "<form action="$HTTP_SERVER_VARSїPHP_SELF]" method="POST">\n";
$data = file($data_file);
$count = count($data);
$pos = 0; // Index of current item
for ($x = 0; $x < $count; $x++)
{
$dataї$x] = trim($dataї$x]); // Since file() includes the newlines \n
// Print item & checkbox:
if (!empty($dataї$x]))
{
print "<input type="checkbox" name="item".$pos++."">";
print "$dataї$x]<br><br>\n";
}
}
print "<input type="hidden" name="is_sent" value="1">\n";
print "<input type="submit">\n";
print "</form>\n";
function process_form()
{
global $data_file;
global $HTTP_POST_VARS;
global $HTTP_SERVER_VARS;
$fp = fopen($data_file, 'r+');
$data = fread($fp, filesize($data_file));
// Get each item:
$data = preg_replace("/ї\r\n]+/", "\n", $data);
$array = explode("\n", $data);
ftruncate($fp, 0); // Erase the file
fseek($fp, 0); // Go to start of file
$count = count($array);
$pos = 0; // Index of current item
// Write items NOT selected:
for ($x = 0; $x < $count; $x++)
{
if (!empty($arrayї$x]) && empty($HTTP_POST_VARSї'item'.$pos++]))
{
fwrite($fp, $arrayї$x]."\n");
}
}
fclose($fp);
// Redirect back to the form:
header("Location: http://$HTTP_SERVER_VARSїHTTP_HOST]$HTTP_SERVER_VARSїPHP_SELF]");
exit();
}
?>
Posted: Sat Aug 03, 2002 11:53 am
by Zeceer
Thanx!!!!