Deleting of a Line from a test file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Faisal Shah
    New Member
    • Mar 2008
    • 14

    #1

    Deleting of a Line from a test file

    HI,

    Well, Here is the code of my new project .. Actually it's just s scrap or it..

    I have made 2 files.

    the 1st file is test.php with of course creating a new file called testdb.txt.
    And the file called admin.php

    here is the code.

    Code of test.php

    [php]
    <?php

    $db = "testdb.txt ";
    $name= $_POST["name"];
    $like = $_POST["like"];
    $submit = $_POST["submit"];
    $full_mess = "<br> MY name is $name.<br>I like to $like.";
    $seprate = "<br>.......... ....";


    if($submit == submit)
    {
    $f_handle = fopen($db,'a');
    fwrite($f_handl e,$full_mess.$s eprate."\n");
    fclose($f_handl e);
    "<i>Message sent...";
    }
    else
    {
    echo "Message Can't be sent...";
    }
    ?>

    <form name="beta_test ing" action="test.ph p" method="post">
    <br>
    Name ; <input name="name" type="text" size="18">
    <br>
    Like ; <input name="like" type="test" size="20">
    <br>
    <input name="submit" type="submit" value="submit">
    </form>

    <?php

    @include($db);
    ?>
    [/php]

    and admin.php

    [php]
    <?php

    $db = "testdb.txt ";
    $pass = $_POST["pass"];
    $line_to_del = $_POST["line"];
    $log_in_form = "<form name=\"moderati on_login\" action=\"admin. php\" method=\"post\" >
    <br>
    Please insert your password to get in...<input name=\"pass\" type=\"text\" size=\"15\">
    <input name\"submit\" type=\"submit\" value=\"submit\ ">
    </form>";

    $lines_del_form = "<form name=\"moderati on\" action=\"admin. php\" method=\"post\" >
    <br>
    Line Number to delete.. Lines are counted from Down to up...<input name=\"line\" type=\"text\" size=\"15\">
    <input name\"submit\" type=\"submit\" value=\"submit\ ">
    </form>";

    if($pass == 123)
    {
    @include($db);
    $file = file_get_conten ts($dbname);

    $array = explode("\n", $file);

    array_diff(arra y($array), array($line_to_ del));

    echo $lines_del_form ;
    }else
    {
    echo "<b> Please insert correct value into pass field...";

    echo $log_in_form;
    }

    ?>

    [/php]

    Now, When a member/Dude inserts the info that his/her name with Likeness.
    It will be submitted, But will be written the info of each dude as 1 line..

    Now if suppose five people have inserted and i want to delete the info of 3rd member (3rd line) from testdb.txt..

    How can i>..

    Help please :)

    FAISAL!
  • TheServant
    Recognized Expert Top Contributor
    • Feb 2008
    • 1168

    #2
    I recommend that when you write the information, have a separator of some kind, like ; or something so your file will have:

    Person_1 ; Person_2 ; Person_3 ...

    Rewrite your document as a variable... say $doctoedit

    So now if you want to delete person 2:
    [PHP]$doctoedit = str_replace ( 'Person_2 ; ' , '' , $doctoedit );[/PHP]

    Well not sure about the coding, but logically that's how I would do it.

    [edit]
    Forgot it was 1 line per person, but you can do a similar thing with that.

    Comment

    • ronverdonk
      Recognized Expert Specialist
      • Jul 2006
      • 4259

      #3
      ....Now if suppose five people have inserted and i want to delete the info of 3rd member (3rd line) from testdb.txt....
      Then you will the risk of deleting more than you want. Because:
      you could delete line 3 and rewrite the file. The member 4 would become member 3. When the user refreshes the page or 'back-buttons' it will again delete line 3, i.e. the original line 4 and so on.

      Why don't you use a user identifier or a simple database instead of accessing / deleting the n.th record in a file?

      Ronald

      Comment

      • Faisal Shah
        New Member
        • Mar 2008
        • 14

        #4
        Well,
        Thanks. But actually before i get into MySQL. I wanted to have some practice on php file writing, reading and database deleting..

        therefore i asked for this help..

        Well, Can you help me. I TOTALLY agree that it might delete more then I want :(
        But actually I want to know it for my practice only.. And I might need it in future :)

        Waiting for help :

        FAISAL!

        Comment

        • ronverdonk
          Recognized Expert Specialist
          • Jul 2006
          • 4259

          #5
          Well okay, as long as you understand the risk with this.

          Asume you have a text file where ech text line 'normally' ends with a \n.
          Then you can read such a file in one command file() into an array: 1 line per array entry, starting at index 0. So when you want to delete a line from that, say 3, you can unset entry [2]. See this little sample.

          Text file:[
          Code:
          1-abcdefghijk
          2-dfgheritops
          3-nxvvq345qms
          4-jjfjjfjjfjf
          PHP script[php]<?php
          $line_to_delete =3;
          $file=file('tes t.txt');
          unset($file[$line_to_delete-1]);
          foreach ($file as $line)
          echo "$line<br>" ;
          ?>[/php]Result:[
          Code:
          1-abcdefghijk
          2-dfgheritops
          4-jjfjjfjjfjf
          Ronald

          Comment

          • Faisal Shah
            New Member
            • Mar 2008
            • 14

            #6
            Originally posted by ronverdonk
            Well okay, as long as you understand the risk with this.

            Asume you have a text file where ech text line 'normally' ends with a \n.
            Then you can read such a file in one command file() into an array: 1 line per array entry, starting at index 0. So when you want to delete a line from that, say 3, you can unset entry [2]. See this little sample.

            Text file:[
            Code:
            1-abcdefghijk
            2-dfgheritops
            3-nxvvq345qms
            4-jjfjjfjjfjf
            PHP script[php]<?php
            $line_to_delete =3;
            $file=file('tes t.txt');
            unset($file[$line_to_delete-1]);
            foreach ($file as $line)
            echo "$line<br>" ;
            ?>[/php]Result:[
            Code:
            1-abcdefghijk
            2-dfgheritops
            4-jjfjjfjjfjf
            Ronald
            Thanks Bro.

            You really made it so clear and Easy..
            Thanks once more....

            can you please explain me this bunch of code?
            unset($file[$line_to_delete-1]);

            FAISAL!

            Comment

            • ronverdonk
              Recognized Expert Specialist
              • Jul 2006
              • 4259

              #7
              unset() is a command to unset a variable or remove an entry in an array.
              You want to delete record number 3, that number is stored in variable $line_to_delete .

              The to-be-deleted record no 3 is stored in the array with array index 2 (array starts with index 0, so record 1 is in index 0 and record 3 is in index 2).[php]unset($file[$line_to_delete-1]);[/php]just means:

              remove the entry 2 from the array (i.e. record 3).

              Ronald

              Comment

              • Faisal Shah
                New Member
                • Mar 2008
                • 14

                #8
                Sorry to say :(

                BUT I am still stuck with this...

                [php]
                <?php
                //admin.php for deleting entry from a text the system.

                $db = "testdb.txt ";
                $pass = $_POST["pass"];
                $line_to_del = $_POST["del"];
                $pass_submit = $_POST["pass_submi t"];
                $entry_del = $_POST["entry_del_subm it"];
                $log_in_form = "<form method=\"post\" action=\"admin. php\">
                <br>
                Password > <input name=\"pass\" type=\"text\" size=\"10\">
                <input name=\"pass_sub mit\" type=\"submit\" value=\"pass_su bmit\">
                </form>";

                $del_form = "<form method=\"post\" action=\"admin. php\">
                <br>
                Line Number to delete > <input name=\"del\" type=\"text\" size=\"10\">
                <input name=\"entry_de l_submit\" type=\"submit\" value=\"entry_d el_submit\">
                </form>";

                if($pass == 123)
                {
                echo $del_form;
                $entry_to_del = $_POST['del'];
                $file = file("testdb.tx t");
                unset($file[$entry_to_del-1]);
                foreach($file as $line);
                echo $line;
                }else
                {
                "<b>Enter the password to GO!!!</b>";
                echo $log_in_form;
                }
                ?>
                [/php]

                It's the code of my admin.php where I want to delete the lines from an array in the way you told..IN THE BOX, when I put a line number.. NOTHING HAPPEN :(

                Help.
                FAISAL!

                Comment

                • ronverdonk
                  Recognized Expert Specialist
                  • Jul 2006
                  • 4259

                  #9
                  Firstly: the foreach in statement 27 may not be terminated with a semicolumn: it is a block to be executed and this semicolumn terminates it.

                  Secondly: your code is so badly structured that you can hardly see what belongs where and what executes when. Please indent any code so we can see structure.

                  Thirdly: you must test what caused (which form post) the invocation of the script. So you must test the $_POST array to see which of the 2 submits was used.

                  I changed the last part of your code, from line 21, a bit with the checks and now it runs as expected[php]if (isset($_POST['pass_submit']) and $pass == 123) {
                  echo $del_form;
                  exit;
                  }
                  elseif (isset($_POST['entry_del_subm it'])) {
                  $entry_to_del = $_POST['del'];
                  $file = file("testdb.tx t");
                  unset($file[$entry_to_del-1]);
                  foreach($file as $line)
                  echo "$line<br>" ;
                  }
                  else {
                  echo "<b>Enter the password to GO!!!</b>";
                  echo $log_in_form;
                  }
                  ?>[/php]P.S> I changed the thread title, because the subject of this is a file and not a database.

                  Ronald

                  Comment

                  • Faisal Shah
                    New Member
                    • Mar 2008
                    • 14

                    #10
                    Thanks for changing title, as well helping me..

                    Well, the thing is... that I changed to the code you gave. It logs me in, takes me to $form_del. But the problem here is that I am not able to delete any thing.

                    When, I put the number of line into del form, and click on Submit, it takes me and shows me the content of MY DATABASE and not deleting even any single line :(

                    Thanks.. HELP...

                    The code I am using at this time is ;

                    [php]
                    <?php
                    //admin.php for deleting entry from a text the system.

                    $db = "testdb.txt ";
                    $pass = $_POST["pass"];
                    $line_to_del = $_POST["del"];
                    $pass_submit = $_POST["pass_submi t"];
                    $entry_del = $_POST["entry_del_subm it"];
                    $log_in_form = "<form method=\"post\" action=\"admin. php\">
                    <br>
                    Password > <input name=\"pass\" type=\"text\" size=\"10\">
                    <input name=\"pass_sub mit\" type=\"submit\" value=\"pass_su bmit\">
                    </form>";

                    $del_form = "<form method=\"post\" action=\"admin. php\">
                    <br>
                    Line Number to delete > <input name=\"del\" type=\"text\" size=\"10\">
                    <input name=\"entry_de l_submit\" type=\"submit\" value=\"entry_d el_submit\">
                    </form>";

                    if (isset($_POST['pass_submit']) and $pass == 123) {

                    echo $del_form;

                    exit;

                    }

                    elseif (isset($_POST['entry_del_subm it'])) {

                    $entry_to_del = $_POST['del'];

                    $file = file("testdb.tx t");

                    unset($file[$entry_to_del-1]);

                    foreach($file as $line)

                    echo "$line<br>" ;

                    }

                    else {

                    echo "<b>Enter the password to GO!!!</b>";

                    echo $log_in_form;
                    }

                    ?>
                    [/php]

                    Comment

                    • ronverdonk
                      Recognized Expert Specialist
                      • Jul 2006
                      • 4259

                      #11
                      Now hold it a second!

                      Do you expect me to write all the code of this?

                      I showed you code on how to read a file into a lines array, to delete a record from the lines array. I also showed you that it was deleted by printing out the array after the deletion. Now all you have to do is write the text lines BACK into the file.

                      You can surely do that without help. Some things you have to do yourself. And for a PHP programmer writing to a file cannot be a problem.

                      Ronald

                      Comment

                      Working...