I have a text file that I want to be able to sort with php based on
column heading. Text file is tab delimited. Also I want the ability to
add what I'm deleting to a new file for archiving. Here's the code I
have at this moment:
?>
<table>
<tr>
<td>
<?php
$path_to_dir = 'issues'; // path to text file
$file_contents = file("$path_to_dir/issues.txt"); // the file
// [ DELETE ROUTINE ]
if($_POST['action'] == "delete"){ // delete triggered
$arrToDelete = $_POST['todelete']; // assign checked items array to var
if($arrToDelete != "") { // if the array isnt open proceed
foreach($arrToDelete as $key => $value) { // assign a key and value to
each checked item
array_splice($file_contents, $key, 1, "");// assign null value to each
checked item in file_contents array
print("Deleted <b>$value</b><br>");
}
$sizeof = count($file_contents); // count updated file_contents array
if($filehandle = fopen("$path_to_dir/issues.txt", "w")){ // open the
file for writing
for($y = 0; $y < $sizeof; $y++){ // loop through the new file_contents array
if($file_contents){
fputs($filehandle, $file_contents[$y]); // write the new file_contents
array to the file
}
}
fclose($filehandle); // close the file
}
}
}
?>
<?php // [ READ & OUTPUT FILE CONTENTS ] ?>
<!-- The Form & Table -->
<form method="POST" action="<?php $PHP_SELF ?>" style="margin:0;">
<input type="hidden" name="action" value="delete">
<table border="1">
<tr>
<td class="spectable"><b><h2>User</td><td
class="spectable"><b><h2>Name</td><td
class="spectable"><b><h2>Issue</td><td
class="spectable"><b><h2>Description</td><td
class="spectable"><b><h2>Email</td><td class="spectable"><b><h2>Time
Sent</td><td class="spectable"><b><h2>Date</td><td
class="spectable"><b><h2>Issue Resolved</td>
</tr>
<?php
$file_contents = file("$path_to_dir/issues.txt"); // the file
foreach($file_contents as $key=>$value){ // assign a key to each line in the file
if($file_contents[$key] != ""){
$entry = explode("\t", $file_contents[$key]); // break out each
delimeter - tab in this case
print("<tr> <td class=spectable><b><h3><center>".$entry[0]."</td>
<td class=spectable><b><h3><center>"".$entry[1].""</td>"
<td class=spectable><b><h3><center>"".$entry[2].""</td>
<td class=spectable><b><h3><center>".$entry[3]."</td>
<td class=spectable><b><h3><center>"".$entry[4].""</td>"
<td class=spectable><b><h3><center>"".$entry[5].""</td>"
<td class=spectable><b><h3><center>"".$entry[6].""</td>"
<td class=spectable><center><input type=checkbox name=todelete[$key]
value=$value></center></td>
</tr>"); // giving key and value to each checked item
}
}
?>
<tr>
<td colspan="8" align="center" class="spectable"><input type="submit"
value="Remove Resolved Issue"></td>
</tr>
</table>
</form>
</td>
</table>
<br><br><br>
Thanks In Advance |
Request for Question Clarification by
studboy-ga
on
25 Apr 2005 11:57 PDT
Hi
Is it possible to post issues.txt?
You can sort by key with ksort.
|
Request for Question Clarification by
studboy-ga
on
25 Apr 2005 11:59 PDT
Also this part of the code appears to be corrupted when you post it--
the quotes don't match:
print("<tr> <td class=spectable><b><h3><center>".$entry[0]."</td>
<td class=spectable><b><h3><center>"".$entry[1].""</td>"
<td class=spectable><b><h3><center>"".$entry[2].""</td>
<td class=spectable><b><h3><center>".$entry[3]."</td>
<td class=spectable><b><h3><center>"".$entry[4].""</td>"
<td class=spectable><b><h3><center>"".$entry[5].""</td>"
<td class=spectable><b><h3><center>"".$entry[6].""</td>"
<td class=spectable><center><input type=checkbox name=todelete[$key]
value=$value></center></td>
</tr>"); // giving key and value to each checked item
|
Clarification of Question by
sjw-ga
on
25 Apr 2005 12:06 PDT
The text file is as follows:
oneword twoword threeword fourword fiveword sixword
oneword twoword threeword fourword fiveword sixword
oneword twoword threeword fourword fiveword sixword
oneword twoword threeword fourword fiveword sixword
oneword twoword threeword fourword fiveword sixword
oneword twoword threeword fourword fiveword sixword
It is tab delimited with no key above the column. The sorting that I
need, needs to be done by column above each grouping. Do you need me
send repost the code due to the quote mispalcement?
|
Clarification of Question by
sjw-ga
on
25 Apr 2005 12:06 PDT
What would the code look like if I used ksort?
|
Request for Question Clarification by
studboy-ga
on
25 Apr 2005 14:48 PDT
OK, first let's take care of the deletion->archived file. I called
the archived file issues_deleted.txt (see and try the code below).
For the sorting, you will need to break the entries into a
multidimensional array and sort it using array_multisort (see the
database example in http://us3.php.net/manual/en/function.array-multisort.php).
If I'm on the right track, please let me know so I can post the
formal answer (showing the multisort). Thanks.
-----
<table>
<tr>
<td>
<?php
$path_to_dir = 'issues'; // path to text file
$file_contents = file("$path_to_dir/issues.txt"); // the file
$file_contents_deleted = file("$path_to_dir/issues_deleted.txt"); // the file
// [ DELETE ROUTINE ]
if($_POST['action'] == "delete"){ // delete triggered
$arrToDelete = $_POST['todelete']; // assign checked items array to var
if($arrToDelete != "") { // if the array isnt open proceed
foreach($arrToDelete as $key => $value) { // assign a key and value to
each checked item
#$file_contents_deleted[count($file_contents_deleted)] = $file_contents[$key];
$file_contents_deleted[] = $file_contents[$key];
array_splice($file_contents, $key, 1, "");// assign null value to each
checked item in file_contents array
print("Deleted <b>$value</b><br>");
}
$sizeof = count($file_contents_deleted); // count updated file_contents array
if($filehandle = fopen("$path_to_dir/issues_deleted.txt", "w")){ //
open the file for writing
for($y = 0; $y < $sizeof; $y++){ // loop through the new file_contents array
if($file_contents_deleted){
fputs($filehandle, $file_contents_deleted[$y]); // write the new
file_contents array to the file
}
}
fclose($filehandle); // close the file
}
$sizeof = count($file_contents); // count updated file_contents array
if($filehandle = fopen("$path_to_dir/issues.txt", "w")){ // open the
file for writing
for($y = 0; $y < $sizeof; $y++){ // loop through the new file_contents array
if($file_contents){
fputs($filehandle, $file_contents[$y]); // write the new file_contents
array to the file
}
}
fclose($filehandle); // close the file
}
}
}
?>
<?php // [ READ & OUTPUT FILE CONTENTS ] ?>
<!-- The Form & Table -->
<form method="POST" action="<?php $PHP_SELF ?>" style="margin:0;">
<input type="hidden" name="action" value="delete">
<table border="1">
<tr>
<td class="spectable"><b><h2>User</td><td
class="spectable"><b><h2>Name</td><td
class="spectable"><b><h2>Issue</td><td
class="spectable"><b><h2>Description</td><td
class="spectable"><b><h2>Email</td><td class="spectable"><b><h2>Time
Sent</td><td class="spectable"><b><h2>Date</td><td
class="spectable"><b><h2>Issue Resolved</td>
</tr>
<?php
$file_contents = file("$path_to_dir/issues.txt"); // the file
foreach($file_contents as $key=>$value){ // assign a key to each line in the file
if($file_contents[$key] != ""){
$entry = explode("\t", $file_contents[$key]); // break out each
delimeter - tab in this case
$count++;
print("<tr>
<td class=spectable><b><h3><center>".$entry[0]."</td>
<td class=spectable><b><h3><center>".$entry[1]."</td>
<td class=spectable><b><h3><center>".$entry[2]."</td>
<td class=spectable><b><h3><center>".$entry[3]."</td>
<td class=spectable><b><h3><center>".$entry[4]."</td>
<td class=spectable><b><h3><center>".$entry[5]."</td>
<td class=spectable><b><h3><center>".$entry[6]."</td>
<td class=spectable><b><h3><center><input type=checkbox
name=todelete[$key] value=$value></center></td>
</tr>");
}
}
?>
<tr>
<td colspan="8" align="center" class="spectable"><input type="submit"
value="Remove Resolved Issue"></td>
</tr>
</table>
</form>
</td>
</table>
<br><br><br>
|
Clarification of Question by
sjw-ga
on
25 Apr 2005 15:12 PDT
Just to clarify, I need the ability to sort by column, by clicking the
top of the column like a hyperlink. My server is down at the present
moment but I will test the above code as soon as I can
|
Request for Question Clarification by
studboy-ga
on
25 Apr 2005 15:51 PDT
Like this? (I disable the write on my server for security reasons)
http://www.idealedge.com/issues.php
|
Clarification of Question by
sjw-ga
on
25 Apr 2005 17:12 PDT
Yes, this is what I need, along with the archive function which I'll
be able to test within a hour
|
Clarification of Question by
sjw-ga
on
25 Apr 2005 21:53 PDT
Yes, this code does work exactly as I need, could you post the final
code with the sorting as well?
|
Hi sjw-ga,
Thanks. Glad I can help. Code below:
--------------------------------------
<table>
<tr>
<td>
<?php
$path_to_dir = 'issues'; // path to text file
$file_contents = file("$path_to_dir/issues.txt"); // the file
$file_contents_deleted = file("$path_to_dir/issues_deleted.txt"); // the file
// [ DELETE ROUTINE ]
if($_POST['action'] == "delete"){ // delete triggered
$arrToDelete = $_POST['todelete']; // assign checked items array to var
if($arrToDelete != "") { // if the array isnt open proceed
foreach($arrToDelete as $key => $value) { // assign a key and value to
each checked item
#$file_contents_deleted[count($file_contents_deleted)] = $file_contents[$key];
$file_contents_deleted[] = $file_contents[$key];
array_splice($file_contents, $key, 1, "");// assign null value to each
checked item in file_contents array
print("Deleted <b>$value</b><br>");
}
$sizeof = count($file_contents_deleted); // count updated file_contents array
if($filehandle = fopen("$path_to_dir/issues_deleted.txt", "w")){ //
open the file for writing
for($y = 0; $y < $sizeof; $y++){ // loop through the new file_contents array
if($file_contents_deleted){
fputs($filehandle, $file_contents_deleted[$y]); // write the new
file_contents array to the file
}
}
fclose($filehandle); // close the file
}
$sizeof = count($file_contents); // count updated file_contents array
if($filehandle = fopen("$path_to_dir/issues.txt", "w")){ // open the
file for writing
for($y = 0; $y < $sizeof; $y++){ // loop through the new file_contents array
if($file_contents){
fputs($filehandle, $file_contents[$y]); // write the new file_contents
array to the file
}
}
fclose($filehandle); // close the file
}
}
}
?>
<?php // [ READ & OUTPUT FILE CONTENTS ] ?>
<!-- The Form & Table -->
<form method="POST" action="<?php $PHP_SELF ?>" style="margin:0;">
<input type="hidden" name="action" value="delete">
<table border="1">
<tr>
<td class="spectable"><b><h2><a href="<?php $PHP_SELF
?>?sort=user">User</a></td><td
class="spectable"><b><h2><a href="<?php $PHP_SELF ?>?sort=name">Name</a></td><td
class="spectable"><b><h2><a href="<?php $PHP_SELF ?>?sort=issue">Issue</a></td><td
class="spectable"><b><h2><a href="<?php $PHP_SELF
?>?sort=description">Description</a></td><td
class="spectable"><b><h2><a href="<?php $PHP_SELF
?>?sort=email">Email</a></td><td class="spectable"><b><h2><a
href="<?php $PHP_SELF ?>?sort=timesent">Time Sent</a></td><td
class="spectable"><b><h2><a href="<?php $PHP_SELF
?>?sort=date">Date</a></td><td
class="spectable"><b><h2>Issue Resolved</td>
</tr>
<?php
$file_contents = file("$path_to_dir/issues.txt"); // the file
foreach($file_contents as $key=>$value){ // assign a key to each line in the file
if($file_contents[$key] != ""){
$entry = explode("\t", $file_contents[$key]); // break out each
delimeter - tab in this case
$entries[]=array('user'=>$entry[0], 'name'=>$entry[1],
'issue'=>$entry[2], 'description'=>$entry[3], 'email'=>$entry[4],
'timesent'=>$entry[5], 'date'=>$entry[6]);
#print("<tr>
#<td class=spectable><b><h3><center>".$entry[0]."</td>
#<td class=spectable><b><h3><center>".$entry[1]."</td>
#<td class=spectable><b><h3><center>".$entry[2]."</td>
#<td class=spectable><b><h3><center>".$entry[3]."</td>
#<td class=spectable><b><h3><center>".$entry[4]."</td>
#<td class=spectable><b><h3><center>".$entry[5]."</td>
#<td class=spectable><b><h3><center>".$entry[6]."</td>
#<td class=spectable><b><h3><center><input type=checkbox
name=todelete[$key] value=$value></center></td>
#</tr>");
}
}
if ($entries) {
foreach ($entries as $key => $row) {
$user[$key] = $row['user'];
$name[$key] = $row['name'];
$issue[$key] = $row['issue'];
$description[$key] = $row['description'];
$email[$key] = $row['email'];
$timesent[$key] = $row['timesent'];
$date[$key] = $row['date'];
}
switch ($sort) {
case "user": array_multisort($user, SORT_ASC, $entries); break;
case "name": array_multisort($name, SORT_ASC, $entries); break;
case "issue": array_multisort($issue, SORT_ASC, $entries); break;
case "description": array_multisort($description, SORT_ASC, $entries); break;
case "email": array_multisort($email, SORT_ASC, $entries); break;
case "timesent": array_multisort($timesent, SORT_ASC, $entries); break;
case "date": array_multisort($date, SORT_ASC, $entries); break;
}
}
for($i=0; $i<sizeof($entries); $i++) {
print("<tr>
<td class=spectable><b><h3><center>".$entries[$i]['user']."</td>
<td class=spectable><b><h3><center>".$entries[$i]['name']."</td>
<td class=spectable><b><h3><center>".$entries[$i]['issue']."</td>
<td class=spectable><b><h3><center>".$entries[$i]['description']."</td>
<td class=spectable><b><h3><center>".$entries[$i]['email']."</td>
<td class=spectable><b><h3><center>".$entries[$i]['timesent']."</td>
<td class=spectable><b><h3><center>".$entries[$i]['date']."</td>
<td class=spectable><b><h3><center><input type=checkbox
name=todelete[$key] value=$value></center></td>
</tr>");
}
?>
<tr>
<td colspan="8" align="center" class="spectable"><input type="submit"
value="Remove Resolved Issue"></td>
</tr>
</table>
</form>
</td>
</table>
<br><br><br> |
Request for Answer Clarification by
sjw-ga
on
26 Apr 2005 01:37 PDT
When I check any of the boxes,it moves the last line in the table to
the file not the one I select. Can you send me a solution?
|
Request for Answer Clarification by
sjw-ga
on
26 Apr 2005 02:06 PDT
This code also only allows me to move one line at a time, Can you send
me a solution for this as well?
|
Clarification of Answer by
studboy-ga
on
26 Apr 2005 02:07 PDT
Hi sjw-ga
Can you post issues.txt?
It's probably a small typo in the code.
I don't have access to the server until tomorrow morning--
if you post the issues.txt by then I can debug it then.
Thanks
|
Clarification of Answer by
studboy-ga
on
26 Apr 2005 02:08 PDT
Also, can you tell me whether my FIRST code works for the moving
(ie, can my first code (before the sorting) move the correct
line/multiple lines? Thanks.
|
Request for Answer Clarification by
sjw-ga
on
26 Apr 2005 02:21 PDT
Here is the issue.txt file:
jsmith John Smith Email Needs Password mine@mine.com
5:07:49 04/26/05
jsmith John Smith Eamil Needs Password mine@mine.com
5:08:49 04/26/05
jsmith John Smith Email Needs Password mine@mine.com
5:09:49 04/26/05
jsmith John Smith Email Needs Password mine@mine.com
5:12:49 04/26/05
jsmith John Smith Email Needs Password mine@mine.com
5:14:49 04/26/05
jsmith John Smith Email Needs Password mine@mine.com
5:16:49 04/26/05
And Yes the first part of the code worked exactly as needed before the
sort function was added.
|
Clarification of Answer by
studboy-ga
on
26 Apr 2005 03:00 PDT
OK, give this a try--I found the typo by inspection--can you test it
and see? Thanks.
--------------------------------------
<table>
<tr>
<td>
<?php
$path_to_dir = 'issues'; // path to text file
$file_contents = file("$path_to_dir/issues.txt"); // the file
$file_contents_deleted = file("$path_to_dir/issues_deleted.txt"); // the file
// [ DELETE ROUTINE ]
if($_POST['action'] == "delete"){ // delete triggered
$arrToDelete = $_POST['todelete']; // assign checked items array to var
if($arrToDelete != "") { // if the array isnt open proceed
foreach($arrToDelete as $key => $value) { // assign a key and value to
each checked item
#$file_contents_deleted[count($file_contents_deleted)] = $file_contents[$key];
$file_contents_deleted[] = $file_contents[$key];
array_splice($file_contents, $key, 1, "");// assign null value to each
checked item in file_contents array
print("Deleted <b>$value</b><br>");
}
$sizeof = count($file_contents_deleted); // count updated file_contents array
if($filehandle = fopen("$path_to_dir/issues_deleted.txt", "w")){ //
open the file for writing
for($y = 0; $y < $sizeof; $y++){ // loop through the new file_contents array
if($file_contents_deleted){
fputs($filehandle, $file_contents_deleted[$y]); // write the new
file_contents array to the file
}
}
fclose($filehandle); // close the file
}
$sizeof = count($file_contents); // count updated file_contents array
if($filehandle = fopen("$path_to_dir/issues.txt", "w")){ // open the
file for writing
for($y = 0; $y < $sizeof; $y++){ // loop through the new file_contents array
if($file_contents){
fputs($filehandle, $file_contents[$y]); // write the new file_contents
array to the file
}
}
fclose($filehandle); // close the file
}
}
}
?>
<?php // [ READ & OUTPUT FILE CONTENTS ] ?>
<!-- The Form & Table -->
<form method="POST" action="<?php $PHP_SELF ?>" style="margin:0;">
<input type="hidden" name="action" value="delete">
<table border="1">
<tr>
<td class="spectable"><b><h2><a href="<?php $PHP_SELF
?>?sort=user">User</a></td><td
class="spectable"><b><h2><a href="<?php $PHP_SELF ?>?sort=name">Name</a></td><td
class="spectable"><b><h2><a href="<?php $PHP_SELF ?>?sort=issue">Issue</a></td><td
class="spectable"><b><h2><a href="<?php $PHP_SELF
?>?sort=description">Description</a></td><td
class="spectable"><b><h2><a href="<?php $PHP_SELF
?>?sort=email">Email</a></td><td class="spectable"><b><h2><a
href="<?php $PHP_SELF ?>?sort=timesent">Time Sent</a></td><td
class="spectable"><b><h2><a href="<?php $PHP_SELF
?>?sort=date">Date</a></td><td
class="spectable"><b><h2>Issue Resolved</td>
</tr>
<?php
$file_contents = file("$path_to_dir/issues.txt"); // the file
foreach($file_contents as $key=>$value){ // assign a key to each line in the file
if($file_contents[$key] != ""){
$entry = explode("\t", $file_contents[$key]); // break out each
delimeter - tab in this case
$entries[]=array('user'=>$entry[0], 'name'=>$entry[1],
'issue'=>$entry[2], 'description'=>$entry[3], 'email'=>$entry[4],
'timesent'=>$entry[5], 'date'=>$entry[6], 'key'=>$key,
'value'=>$value);
#print("<tr>
#<td class=spectable><b><h3><center>".$entry[0]."</td>
#<td class=spectable><b><h3><center>".$entry[1]."</td>
#<td class=spectable><b><h3><center>".$entry[2]."</td>
#<td class=spectable><b><h3><center>".$entry[3]."</td>
#<td class=spectable><b><h3><center>".$entry[4]."</td>
#<td class=spectable><b><h3><center>".$entry[5]."</td>
#<td class=spectable><b><h3><center>".$entry[6]."</td>
#<td class=spectable><b><h3><center><input type=checkbox
name=todelete[$key] value=$value></center></td>
#</tr>");
}
}
if ($entries) {
foreach ($entries as $key => $row) {
$user[$key] = $row['user'];
$name[$key] = $row['name'];
$issue[$key] = $row['issue'];
$description[$key] = $row['description'];
$email[$key] = $row['email'];
$timesent[$key] = $row['timesent'];
$date[$key] = $row['date'];
}
switch ($sort) {
case "user": array_multisort($user, SORT_ASC, $entries); break;
case "name": array_multisort($name, SORT_ASC, $entries); break;
case "issue": array_multisort($issue, SORT_ASC, $entries); break;
case "description": array_multisort($description, SORT_ASC, $entries); break;
case "email": array_multisort($email, SORT_ASC, $entries); break;
case "timesent": array_multisort($timesent, SORT_ASC, $entries); break;
case "date": array_multisort($date, SORT_ASC, $entries); break;
}
}
for($i=0; $i<sizeof($entries); $i++) {
print("<tr>
<td class=spectable><b><h3><center>".$entries[$i]['user']."</td>
<td class=spectable><b><h3><center>".$entries[$i]['name']."</td>
<td class=spectable><b><h3><center>".$entries[$i]['issue']."</td>
<td class=spectable><b><h3><center>".$entries[$i]['description']."</td>
<td class=spectable><b><h3><center>".$entries[$i]['email']."</td>
<td class=spectable><b><h3><center>".$entries[$i]['timesent']."</td>
<td class=spectable><b><h3><center>".$entries[$i]['date']."</td>
<td class=spectable><b><h3><center><input type=checkbox
name=todelete[".$entries[$i]['key']."]
value=".$entries[$i]['value']."></center></td>
</tr>");
}
?>
<tr>
<td colspan="8" align="center" class="spectable"><input type="submit"
value="Remove Resolved Issue"></td>
</tr>
</table>
</form>
</td>
</table>
<br><br><br>
|
Request for Answer Clarification by
sjw-ga
on
26 Apr 2005 22:27 PDT
So it deletes(moving) multiple entries (no longer the last one)and
sorts descending, but when deleting any entry it says "Deleted on",
it used to list the user, I can not find anywhere in the code to
change this. Is it possible to list the whole line that was deleted or
restore the user again?
|
Clarification of Answer by
studboy-ga
on
26 Apr 2005 23:07 PDT
I don't experience that problem--
http://www.idealedge.com/issues.php
when I delete, it says delete oneword1, exactly as before.
It must be the cut and paste from the web browser to your code--
make sure you cut and paste every line carefully. If you like,
give me access to your website (post a temporary user name and passwd)
and I can upload it to prevent cut and paste corruption.
|
Clarification of Answer by
studboy-ga
on
26 Apr 2005 23:09 PDT
The line that prints out "Delete oneword1" is this line:
print("Deleted <b>$value</b><br>");
Again, check the cut and paste carefully.
|