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&#1111;'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&#1111;PHP_SELF]" method="POST">\n";

$data = file($data_file);
$count = count($data);
$pos = 0; // Index of current item

for ($x = 0; $x < $count; $x++)
&#123;
	$data&#1111;$x] = trim($data&#1111;$x]); // Since file() includes the newlines \n

	// Print item & checkbox:
	if (!empty($data&#1111;$x]))
	&#123;
		print "<input type="checkbox" name="item".$pos++."">";
		print "$data&#1111;$x]<br><br>\n";
	&#125;
&#125;

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()
&#123;
	$fp = fopen($GLOBALS&#1111;'data_file'], 'r+');
	$data = fread($fp, filesize($GLOBALS&#1111;'data_file']));

	// Get each item:
	$data = preg_replace("/&#1111;\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++)
	&#123;
		if (!empty($array&#1111;$x]) && empty($_POST&#1111;'item'.$pos++]))
		&#123;
			fwrite($fp, $array&#1111;$x]."\n");
		&#125;
	&#125;

	fclose($fp);

	// Redirect back to the form:
	header("Location: http://$_SERVER&#1111;HTTP_HOST]$_SERVER&#1111;PHP_SELF]");
	exit();
&#125;

?>

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&#1111;'is_sent'])) process_form();

print "<form action="$HTTP_SERVER_VARS&#1111;PHP_SELF]" method="POST">\n"; 

$data = file($data_file); 
$count = count($data); 
$pos = 0; // Index of current item 

for ($x = 0; $x < $count; $x++) 
&#123; 
   $data&#1111;$x] = trim($data&#1111;$x]); // Since file() includes the newlines \n 

   // Print item & checkbox: 
   if (!empty($data&#1111;$x])) 
   &#123; 
      print "<input type="checkbox" name="item".$pos++."">"; 
      print "$data&#1111;$x]<br><br>\n"; 
   &#125; 
&#125; 

print "<input type="hidden" name="is_sent" value="1">\n"; 
print "<input type="submit">\n"; 
print "</form>\n";

function process_form() 
&#123; 
   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("/&#1111;\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++) 
   &#123; 
      if (!empty($array&#1111;$x]) && empty($HTTP_POST_VARS&#1111;'item'.$pos++])) 
      &#123; 
         fwrite($fp, $array&#1111;$x]."\n"); 
      &#125; 
   &#125; 

   fclose($fp); 

   // Redirect back to the form: 
   header("Location: http://$HTTP_SERVER_VARS&#1111;HTTP_HOST]$HTTP_SERVER_VARS&#1111;PHP_SELF]"); 
   exit(); 
&#125; 

?>

Posted: Sat Aug 03, 2002 11:53 am
by Zeceer
Thanx!!!!