Remove an Array from file
Moderator: General Moderators
Remove an Array from file
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.
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.
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:
2. Get the items from the file, and then print each of them in a form:
3. After the form is submitted, remove the items the user selected:
(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();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";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();
}
?>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?
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?
- hob_goblin
- Forum Regular
- Posts: 978
- Joined: Sun Apr 28, 2002 9:53 pm
- Contact:
- hob_goblin
- Forum Regular
- Posts: 978
- Joined: Sun Apr 28, 2002 9:53 pm
- Contact:
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();
}
?>