Welcome to Dream.In.Code
Getting PHP Help is Easy!

Join 132,462 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,346 people online right now. Registration is fast and FREE... Join Now!




Removing Lines From Text File In Array

 
Reply to this topicStart new topic

Removing Lines From Text File In Array

jweizenblut
post 5 Mar, 2005 - 07:18 PM
Post #1


New D.I.C Head

*
Joined: 5 Mar, 2005
Posts: 1

I have a tab delimeted file that gets read into an array and then dumped to a table, sorted and so forth.

I want to be able to delete lines from the tab file and move them to a new file by clicking on a button that would be in each row of the table.

For example:

If my table has the following rows and columns that are read from the file.

a | 1 | q | "button"
b | 2 | f | "button"
c | 3 | r | "button"

I want to be able to click on the button in the line that starts with the B and have that entire line moved to a new file and the value in the third column changed to a fixed value.

My code for reading the array, sorting and placing it into a table is as follows:

<?php
$file = file("submitted.txt");

foreach($file as $row) {
$data[] = explode("\t", $row);
}

foreach ($data as $key => $row) {
$col1[$key] = $row[0]; /*ID*/
$col2[$key] = $row[1]; /*NAME*/
$col3[$key] = $row[2]; /*CODE*/
$col4[$key] = $row[3]; /*ORG*/
$col5[$key] = $row[4]; /*TYPE*/
$col6[$key] = $row[5]; /*DSTART*/
$col7[$key] = $row[6]; /*DEND*/
$col8[$key] = $row[7]; /*TSTART*/
$col9[$key] = $row[8]; /*TEND*/
$col10[$key] = $row[9]; /*TOTAL*/
$col11[$key] = $row[10]; /*DATE*/
$col12[$key] = $row[11]; /*DATE*/
$col13[$key] = $row[12]; /*PURP*/
$col14[$key] = $row[13]; /*AVAIL*/
$col15[$key] = $row[14]; /*REM*/
$col16[$key] = $row[15]; /*SIGN*/
$col17[$key] = $row[16]; /*SBMT*/
$col18[$key] = $row[17]; /*STAT*/
$col19[$key] = $row[18]; /*WHY*/
$col20[$key] = $row[19]; /*APPR*/
$col21[$key] = $row[20]; /*PPRD*/
}

$text = array_multisort($col6, SORT_DESC, $col2, SORT_ASC, $data);

$count = 0;
foreach ($data as $values) {
$bgcolor = (($count%2)==0) ? "#FFFFFF" : "#CCCCCC";
echo "
<tr>
<td bgcolor=$bgcolor rowspan=2 align=center width=80><font size=1>$values[10]</font></td>
<td bgcolor=$bgcolor rowspan=2 align=center><font size=1>$values[11]</font></td>
<td bgcolor=$bgcolor><font size=1><b>$values[1]<b> ($values[2]) <i>$values[3]</i></font></td>
<td bgcolor=$bgcolor><font size=1>$values[4] | $values[9] Hour(s) ($values[7]-$values[8])</font></td>
<td bgcolor=$bgcolor rowspan=2><center><font size=1> ---BUTTON-GOES-HERE--- </font></center></td>
</tr>
<tr>
<td bgcolor=$bgcolor colspan=2 width=500><font size=1>Purpose: <b>$values[12]</b> $values[13] <br> $values[14]</font></td>
</tr>
";
$count++;
}
?>

I sort of imagine this would involve that button calling a function that would select the values for that line from my array and remove it. I am just not sure how this would be implemented at all. Any ideas, are welcomed.

Kind of figured out that the way this should somehow work is that when the button is clicked, it should find the line number where that key is, then copy the entired contents of that line into a string, and delete the line from the file.
Unfortunately, I still haven't figured out how this is to be done.
Please help.
User is offlineProfile CardPM

Go to the top of the page

cyberscribe
post 6 Mar, 2005 - 09:54 AM
Post #2


humble.genius

Group Icon
Joined: 5 May, 2002
Posts: 1,062



Thanked 2 times

Dream Kudos: 154
My Contributions


Well, I have to first point out that loading an entire file into memory via an array isn't going to be very efficient for large files. smile.gif

That said, if you have an array and want to selectively flag certain members of the array to do something with them (like delete or move them or whatever), you could use a button like so:

CODE

<form>
blah blah row 4
<input type="hidden" name="row[4]" value="TRUE"><input type="submit">
</form>


bear in mind, however, that with the resulting $row array will only have one value, so don't treat it as an index even though it is numeric -- just treat it as an associative array with a numeric key.

The easiest way is to just use array_keys() on the $row array, then snag the first value out of the resulting (flattened, non-associative) array.

Instead of hidden input with a button after each column, I suggest checkboxes and one button at the end:

CODE

<form>
<input type="checkbox" name="row[4]" value="TRUE">blah blah row 4
<input type="checkbox" name="row[5]" value="TRUE">blah blah row 5
...
<input type="submit">
</form>


Again, array_keys() will flatten $row, then you can just iterate with a for() loop to see what got checked.

hth.
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/22/08 01:05PM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month