How do I add to an array from a static method?

Need To Sort A Text File in PHP And To Add 2 Functions Togther.

  • 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

  • Answer:

    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>

sjw-ga at Google Answers Visit the source

Was this solution helpful to you?

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.