FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Delete one txt line depending in wich "radio" butt





Nachui
the codes are in spanish, is because i'm from Chile

Please help me, i've trying to do this for like 2 hours XD

i have a txt archive named "personas.txt" (people.txt) looks like this:
Code:
Karol   Suchan   karol.suchan@uai.cl
Pablo   Carrasco   pabcarrasco@alumnos.uai.cl
Manuel   Matas   mmatas@alumnos.uai.cl


what i have to do is make a php that can delete a line depending in a radio button i selected
borrar.php (delete.php)
Code:
<html><head><title>Borrar un usuario</title></head>
<body>
    <form action="borrar2.php">
       
        <h3>Que usuario desea borrar?</h3>
        Seleccione su e-mail:<br>
        <p>
    <?php
$arch_nombre = "personas.txt";
     $arch_manija = fopen($arch_nombre, "r");
          while (!feof($arch_manija)) {
      $linea = fgets($arch_manija);
      $campos = explode("\t", $linea);
                echo "<input type='radio' name='email' value='email[$campos{2}]'/>";
                echo "<tr><td>{$campos[2]}</td></tr><br>";   
     }
          fclose ($arch_manija);
?>
        <p><br>
    <input type="submit" value="Borrar"/>
</form></body>
</html>


the idea is that when i select one of the buttons, the selected e-mail (and the entire line of the txt: name, second name and e-mail) has to be deleted, so when I look again the list of people saved the one selected is there no more.
listado.php (list.php)
Code:
 <html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Listado de contactos</title>
  </head>
  <body>
   <table>
     <thead>
      <tr><th>Nombre</th><th>Apellido</th><th>E-mail</th></tr>
     </thead>
     <?php
     $arch_nombre = "personas.txt";
     $arch_manija = fopen($arch_nombre, "r");
     while (!feof($arch_manija)) {
      $linea = fgets($arch_manija);
      $campos = explode("\t", $linea);
      echo "<tr><td>{$campos[0]}</td><td>{$campos[1]}</td><td>{$campos[2]}</td></tr>";
     }
     fclose($arch_manija);
     ?>
   </table>
   <br/>
    <form action='nuevo.php'>
      <input type='submit' name='agregar' value='Agregar'/>
    </form>
  </body>
</html>


what i have in "borrar2.php" (delete2.php) is
Code:
<html><head><title>Borrar</title></head>
    <body>
            <?php 
            $email=$_REQUEST['email'];
            if (!empty($_REQUEST['email'])) {
                $asdf = 1;
            } else { $asdf = 0; }
       
           
            if ($asdf == 1) {

           ??????????????????? here is where i think should be the "delete" code ??????
   
        echo "<h2>El usuario se ha borrado satisfactoriamente !</h2><br><p>";
        echo "<form action='listado.php'><input type='submit' value='Ver nuevo listado'/></form>";
       
        } elseif ($asdf == 0) {
        echo "<p><br><h2>Al parecer no seleccionaste a nadie...<h2><br><p>";
        echo "<form action='borrar.php'><input type='submit' value='Volver'/></form>";           
                    }
                    ?>
    </body></html>


(hope my english is not THAT bad, sorry if it is xD)
Nachui
Y U HO HELP ME D: ! XD
Mr_Howl
For this particular application, it would probably be easier to use mySQL. It's easy to delete from the database in SQL.

I think files can only be opened as Read, Write, or Append. So, to delete a line from a file, I believe the only way to do it is this:

Read in the entire file. You'd probably want to store each line as an element in an array. You could also make it multidimensional, and store each field as an element (eg, first name, last name, phone number) if you are planning to do other manipulations.

Delete the chosen line from the array. Fortunately, PHP makes this easy for you--just use unset(). If this was C, doing this operation would probably make you cringe.

Write the entire array back into the file.

Rather inefficient, yes. Again, a database would probably better suit your needs.

Hope that helps.
sonam
Like all others I am suggest to use Mysql but if you don't like it then maybe this script can help you. This script is my old one what I am using before and working very well. I am little bit change script for your needs and include backup copy (for security). Wink
NOTE:
I am not suggest to use spaces in your data base because this can cause lot of errors. It is much better to use | separator.

Code:
Karol|Suchan|karol.suchan@uai.cl
Pablo|Carrasco|pabcarrasco@alumnos.uai.cl
Manuel|Matas|mmatas@alumnos.uai.cl



Code:
<?php
$del = trim("pabcarrasco@alumnos.uai.cl");
$file = 'personal.txt';
$members = file($file); // Create an array from data file

   if (!$members) {
      echo "ERROR: Unable to open data file!<br />\n";
      exit;
  } else {
    $start_del=0;
     for($i = 0; $i < count($members); $i++) { // Count the elements in the array
       $tmp = explode("|", $members[$i]); // Explode each elements in the tmp array
         if(strpos($tmp[2], $del) !== FALSE) {
            $start_del=1; // Activate delete process
            $copyfile = 'personal_backup.txt'; // Backup file
            copy($file, $copyfile); // Create backup file (overwrite if exist)
             break; // Stop the loops when find id in the tmp array
         }
    }
    if($start_del == 1) {
        $newLine2 = ""; // Create empty line
        $members[$i] = $newLine2; // Replace old line in the array with new line
        $lines = count($members); // Count the lines in the array

        $fileNew = fopen($file, "wb"); // Open the data file for writing
        for($f=0; $f<$lines; $f++) {
           fwrite($fileNew, $members[$f]); // Overwrite the existing content
         }
         fclose($fileNew); // Close the file
         echo "DONE!";
      } else {
         echo "Data don't exist!";
      }
   }
?>


Sonam
jmraker
Something like this should work, you'd read the personas.txt and write to personas.tmp
at the end of the loop personas.tmp should contain every line except 1, then you rename the file to replace the original. If the 2nd fopen fails you could try one of the built in temporary file creating functions. If the code is used frequently you should add file locking so 2 instances aren't deleting and/or adding lines at the same time or it needs to imported into a database

Code:
$arch_nombre = "personas.txt";
$arch_manija = fopen($arch_nombre, "r");
$arch_manija_out = fopen('personas.tmp', "w");  // Write to a temporary file
while (!feof($arch_manija)) {
  $linea = fgets($arch_manija);
  $campos = explode("\t", $linea);
  if($_REQUEST['email'] != $campos[2])  // If not the email address they selected
    fwrite(($arch_manija_out, $linea);  // Write to the temporary file
}
fclose ($arch_manija);
fclose ($arch_manija_out);
rename('personas.txt', 'personas.tmp'); // Overwrite original file with temp file
Related topics
unable to transfer db.
how to connect them
Changing a cursor?
insert data to mySQL database problem
php prob
.txt file to .php Lined to tables
IP Banning...
The-Yeah-Gang: Messed up in IE ~10 $FRIH~
Removing Windows 2003 from Dual Boot
Edit / Delete
how to insert data into mysql base from a web page
the vi editor of unix
Deleting certain lines from text file
Radio Wave Propagation
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.