data['select'];delLineFromFile($fileName, $lineNum);}else if ($form->data['select'] == '3') {$fileName = "prbari.options";// the line to delete$lineNum = $form->data['select'];delLineFromFile($fileName, $lineNum);}function delLineFromFile($fileName, $lineNum){// check the file exists if(!is_writable($fileName)) { // print an error print "The file $fileName is not writable"; // exit the function exit; } else { // read the file into an array $arr = file($fileName); } // the line to delete is the line number minus 1, because arrays begin at zero $lineToDelete = $lineNum-1; // check if the line to delete is greater than the length of the file if($lineToDelete > sizeof($arr)) { // print an error print "You have chosen a line number, [$lineNum], higher than the length of the file."; // exit the function exit; } //remove the line unset($arr["$lineToDelete"]); // open the file for reading if (!$fp = fopen($fileName, 'w+')) { // print an error print "Cannot open file ($fileName)"; // exit the function exit; } // if $fp is valid if($fp) { // write the array to the file foreach($arr as $line) { fwrite($fp,$line); } // close the file fclose($fp); }echo "done";}?> It work but the order of value change everytimesuppose i have in a text1=12=23=34=45=56=6if i choose 1the code delete 1i reload the page and if i choose 3 the code delete 4is possible make a code decide every time only the selected item and not in order -1?Please i need a help."> DELETE LINE FROM TEXT FILE - Forums

Forums

DELETE LINE FROM TEXT FILE

solfri 10 May, 2012
Hi people i wanna ask a question
I have a form with select box retrive data from text file with dinamic data action in onload event.
i want when one people choose one value, delete that value.
This is my code in custom code in onsubmit
<?php  
if($form->data['select'] == '1') {
$fileName = "prbari.options";

// the line to delete
$lineNum = $form->data['select'];

delLineFromFile($fileName, $lineNum);
}
else if ($form->data['select'] == '3') {
$fileName = "prbari.options";

// the line to delete
$lineNum = $form->data['select'];

delLineFromFile($fileName, $lineNum);
}
function delLineFromFile($fileName, $lineNum){
// check the file exists 
  if(!is_writable($fileName))
    {
    // print an error
    print "The file $fileName is not writable";
    // exit the function
    exit;
    }
  else
      {
    // read the file into an array    
    $arr = file($fileName);
    }

  // the line to delete is the line number minus 1, because arrays begin at zero
  $lineToDelete = $lineNum-1;
 
  // check if the line to delete is greater than the length of the file
  if($lineToDelete > sizeof($arr))
    {
      // print an error
    print "You have chosen a line number, <b>[$lineNum]</b>,  higher than the length of the file.";
    // exit the function
    exit;
    }

  //remove the line

  unset($arr["$lineToDelete"]);

  // open the file for reading
  if (!$fp = fopen($fileName, 'w+'))
    {
    // print an error
        print "Cannot open file ($fileName)";
      // exit the function
        exit;
        }
  
  // if $fp is valid
  if($fp)
    {
        // write the array to the file
        foreach($arr as $line) { fwrite($fp,$line); }

        // close the file
        fclose($fp);
        }

echo "done";
}


?>
It work but the order of value change everytime
suppose i have in a text
1=1
2=2
3=3
4=4
5=5
6=6
if i choose 1
the code delete 1
i reload the page and if i choose 3
the code delete 4
is possible make a code decide every time only the selected item and not in order -1?
Please i need a help.
Max_admin 11 May, 2012
Hi,

What about splitting the file contents by "\n" then split it again by "=", check the value and remove the correct line ? you will need 2 explode functions + the basic file read and write functions, I see that you have got almost all correct except the logic to find the correct line.

Regards,
Max
Max
ChronoForms developer...
Did you try ChronoMyAdmin for managing your Joomla database tables ?
solfri 11 May, 2012
Can you give me an example in code? im novice in php and i need this process. Thanks
This topic is locked and no more replies can be posted.