Results 1 to 8 of 8

Thread: delete marked line in an included file

  1. #1
    Join Date
    Oct 2006
    Posts
    183
    Thanks
    0
    Thanked 11 Times in 11 Posts

    Default delete marked line in an included file

    Okay I have a script that I want to set up to where you can set the string to be deleted by a checked checkbox next to it, so that it will delete it from the file and not delete everything in the file.

    PHP Code:
    <?

    $dataf 
    "posts.txt";
    $name $_POST['name'];
    $message $_POST['message'];


    if (
    $message != "") {$file fopen("$dataf","a");
    $write fwrite($file,"&nbsp;&nbsp;&middot;&middot;&nbsp;&nbsp; <a href=$name>$message</a>");
    fclose($file);


    }
    ?>
    <br>
    <? include("posts.txt"); ?>
    <form name="add" action="" method="post">
    Locate: <input name="name" size="50" type="text" id="name">
    Text: <input name="message" size= "50" type="text" id="message">
    <input type="submit" name="Submit" value="Submit"><input type="reset" name="Reset" value="Reset">
        </form></td>
      </tr>
    </table>
    I want it to have another button next to reset that is named as delete and when it saves files to the .txt file it will add a checkbox in front of it, but then you can click on the checkbox and it will delete what is checked when you click delete button.

    If that made any sense then please help me

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    This is why form element arrays exist.
    Code:
    <?php
      $thefile = 'your_file.conf';
    
      function deleteLine($file, $lineNum) {
        $c = file($file);
        $c[$lineNum] = '';
        $c = implode('', $c);
        $f = fopen($file, 'w');
        fwrite($f, $c);
        fclose($f);
      }
    
      if(isset($_POST['lines']))
        for($i = 0; $i < count($_POST['lines']); ++$i)
          deleteLine($thefile, $_POST['lines'][$i]);
    
      $c = file($thefile);
    ?>
    <form action="<?php echo($PHP_SELF); ?>" method="post">
    <?php for($i = 0; $i < count($c); ++$i) { ?>
      <label style="display: block;">
        <input type="checkbox" name="lines[]" value="<?php echo($i); ?>">
        <?php echo($c[$i]); ?>
      </label>
    <?php } ?>
      <input type="submit" value="Delete these lines">
    </form>
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Oct 2006
    Posts
    183
    Thanks
    0
    Thanked 11 Times in 11 Posts

    Default

    can i change the file extension or does it have to be a conf file?

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    No, any file will do.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  5. #5
    Join Date
    Oct 2006
    Posts
    183
    Thanks
    0
    Thanked 11 Times in 11 Posts

    Default

    okay, I combined it wiht my otyher script and now it works except one things...

    If you have any checked then it will delete that even if you don't click "delete", such as adding something. Can someone tell me the script to set each button to do a function?

    Code:
    <?php
    $thefile = 'posts.txt';
    $dataf = 'posts.txt';
    $name = $_POST['name'];
    $message = $_POST['message'];
    
      function deleteLine($file, $lineNum) {
        $c = file($file);
        $c[$lineNum] = '';
        $c = implode('', $c);
        $f = fopen($file, 'w');
        fwrite($f, $c);
        fclose($f);
      }
    
      if ($message != "") {$file = fopen("$dataf","a");
    	$write = fwrite($file,"text inserted here
    ");
    	fclose($file);
    
      }
    
      if(isset($_POST['lines']))
        for($i = 0; $i < count($_POST['lines']); ++$i)
          deleteLine($thefile, $_POST['lines'][$i]);
    
      $c = file($thefile);
    ?>
    <form action="<?php echo($PHP_SELF); ?>" method="post">
    <?php for($i = 0; $i < count($c); ++$i) { ?>
      <label style="display: block;">
        <input type="checkbox" name="lines[]" value="<?php echo($i); ?>">
        <?php echo($c[$i]); ?>
      </label>
    <?php } ?>
    Link Text: <input name="message" size= "30" type="text" id="message">
    URL: <input name="name" size="30" type="text" id="name">
      <input type="submit" name="Submit" value="Submit"><input type="reset" name="Reset" value="Reset"><input type="submit" value="Delete these lines">
    </form>
    i want the red to be used when they click on "delete" and the blue to happen when they click on the "submit" button

  6. #6
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
      if(isset($_POST['lines']) && isset($_POST['del']))
        for($i = 0; $i < count($_POST['lines']); ++$i)
          deleteLine($thefile, $_POST['lines'][$i]);
    Code:
    <input type="submit" value="Delete these lines" name="del">
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  7. #7
    Join Date
    Oct 2006
    Posts
    183
    Thanks
    0
    Thanked 11 Times in 11 Posts

    Default

    that leaves a blank checkbox there taht I don't want.

  8. #8
    Join Date
    Oct 2006
    Posts
    183
    Thanks
    0
    Thanked 11 Times in 11 Posts

    Default

    I fixed that problem but now there is a problem that was in it since the beginning, I havent said anything about it though.

    If you make 5 lines so that there is:
    (checkbox) 1
    (checkbox) 2
    (checkbox) 3
    (checkbox) 4
    (checkbox) 5

    then check all of them and delete, for some reason it only deletes 1, 3, and 5. Then if you try to delete 2 and 4 at the same time it only deletes 2.

    After more tests (using # 1-11 and it deleting all odds) I have concluded...

    Its only deleting every other one. Can you help me fix this?

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •