Jump to content

Don't know how I should go about doing this


Johng123

Recommended Posts

I am trying to now make my site edit like different text files and delete certain lines.  What I wanted to do was be able to do was list out the text line by line with like a checkbox next to them and the option to delete that line.  I don't know how to do this though because the size of the text file is always changing and I need to have the checkboxes come up next to each line with option to delete.  I am pretty sure I cannot do this because I would probably have to make a form right?  I don't know how with php this can be done.  PLEASEEE HELP!

Link to comment
Share on other sites

You can do it your method, but it is not such a good idea as it will require much more than a form, but a database.

 

fgets / file_get_contents can pull out the file, to get each line you can simply explode it.

$file = file_get_contents('file.txt');
$lines = explode('\n', $file);

unset($lines[22]); //22'nd line.

foreach ($lines as $line) {{
    echo $line; //Display all lines.
}

 

Each will fill it's own array item, so you can do what you want with it, array search text you don't wish etc.

Link to comment
Share on other sites

You can do it your method, but it is not such a good idea as it will require much more than a form, but a database.

 

fgets / file_get_contents can pull out the file, to get each line you can simply explode it.

$file = file_get_contents('file.txt');
$lines = explode('\n', $file);

unset($lines[22]); //22'nd line.

foreach ($lines as $line) {{
    echo $line; //Display all lines.
}

 

Each will fill it's own array item, so you can do what you want with it, array search text you don't wish etc.

 

I liked the splitting of them into arrays, but I still don't know how I would let the user like delete specific ones, or make a form that contains $line amount of checkboxes

Link to comment
Share on other sites

$file = 'I \n am \n a \n line \n number did you \n know \n this? \n \n';
$lines = explode('\n', $file);

for ($i=0;$i<=(count($lines)-1); $i++) {
    echo ($i+1) . "." . $lines[$i] . "\n<br/>";
}

$delete = explode(',' , '1,2,3,4,5');
for ($i=0;$i<count($delete);$i++) {
   $lines[$i] = "";
}

 

Rough method, but it will list something like:

1. I

2. am

3. a

4. line

5. number did you

6. know

7. this?

8.

9.

 

The user can enter the lines they want deleted into the text box, $deleted can be the $_POST value (for ease) of it. Anc after the said lines are deleted (with the example code):

 

So if $deleted was '1,2,3,4,5' :

 

1.

2.

3.

4.

5.

6. know

7. this?

8.

9.

 

Then you can use implode and file_put_contents to put the file back. No \n means that the line will disappear, and not show in the result file.

Link to comment
Share on other sites

$file = 'I \n am \n a \n line \n number did you \n know \n this? \n \n';
$lines = explode('\n', $file);

for ($i=0;$i<=(count($lines)-1); $i++) {
    echo ($i+1) . "." . $lines[$i] . "\n<br/>";
}

$delete = explode(',' , '1,2,3,4,5');
for ($i=0;$i<count($delete);$i++) {
   $lines[$i] = "";
}

 

Rough method, but it will list something like:

1. I

2. am

3. a

4. line

5. number did you

6. know

7. this?

8.

9.

 

The user can enter the lines they want deleted into the text box, $deleted can be the $_POST value (for ease) of it. Anc after the said lines are deleted (with the example code):

 

So if $deleted was '1,2,3,4,5' :

 

1.

2.

3.

4.

5.

6. know

7. this?

8.

9.

 

Then you can use implode and file_put_contents to put the file back. No \n means that the line will disappear, and not show in the result file.

 

thats a big help thanks man, just a few questions.  How would I delete the entire line before it too like so if they typed in 5 its not just a blank line, but is instead removed.  Also would the form look like this?

 

<form action="newlist.php" method="post">

Lines: <input type="text" name="deleted" >

<input type="submit" />

</form>

Link to comment
Share on other sites

Yeah it would look like that, and be called through $_POST['deleted']

 

With the physical '\n' removed from the line, it will not show up as a blank line on the outputted page, as without the return it will just 'collapse'.

 

I don't know what you mean exactly.  I would text that code right now but I don't really feel like configuring it for my site right now I'll do it in the morning, but does that mean that there will or will not be any blank lines with that code?

Link to comment
Share on other sites

Yeah it would look like that, and be called through $_POST['deleted']

 

With the physical '\n' removed from the line, it will not show up as a blank line on the outputted page, as without the return it will just 'collapse'.

 

I don't know what you mean exactly.  I would text that code right now but I don't really feel like configuring it for my site right now I'll do it in the morning, but does that mean that there will or will not be any blank lines with that code?

 

You're not aware of whitespace?

 

Take this line i'm writing now

for instance

 

The actual line in code is 'Take this line i'm writing now\n for instance'.

 

That is why exploding \n will return all the lines. \n is the equivalent of pressing 'enter'. OS's just takes it and displays a return in place.

 

So yes, if you remove the \n return, it will not display a line (so the blank line would disappear).

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.