Ok so we are having fun with flat files lately...
and i'm working on this curious script that will not obey like a good lil' script...
i can't seem to get only the lines with a state abbrev, state full name to show up only... i keep getting blank lines when i delete one of the states.
take a look to understand...
the script in action
the code:
the other script that works with the above script:Code:<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <title></title> </head> <body> <form action="state_manager.php" method="POST"> NEW RECORD <table> <td> Abbr: <input name="abbr" type="text"> </td><td> State: <input name="state_name" type="text"> </td> <td> <input type="submit" name="insert_state" value="Insert"> </td> </tr> </table> </form> MODIFY RECORDS <table> <tr> <td> <?php $fp = fopen( "states.csv" , "r" ); if ($fp) { while (!feof($fp)) { $line = fgets($fp, 4096); $fullstate = strstr ($line, ','); $csvstate = str_replace($fullstate, '', $line);; $fullstate = ltrim(substr_replace($fullstate, '', 0, 1)); \\i put this following line so that blank lines would not be read... however they are being read anyway!!! why????!?!?? if (!$line==""){ echo "<form action=\"state_manager.php\" method=\"POST\">"; echo "Abbr: <input type=\"text\" name=\"abbr\" value=\"$csvstate\" readonly=\"readonly\">"; echo "</td><td>"; echo "State: <input type=\"text\" name=\"state_name\" value=\"$fullstate\">"; echo "</td><td>"; echo "<input type=\"submit\" name=\"delete_state\" value=\"Delete\">"; echo "<input type=\"submit\" name=\"modify_state\" value=\"save\">"; echo "</td></tr><tr><td>"; echo "</form>"; } } } fclose($fp); ?> </td></tr> </body> </html>
Code:<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <title>Untitled Document</title> </head> <body> <? if(isset($_POST['insert_state'])) { $abbr=$_POST['abbr']; $state_name=$_POST['state_name']; $fp = fopen("states.csv" , "a"); fputs ($fp, "$abbr, $state_name\n"); fclose ($fp); echo "State was inserted into file!!!<br>"; echo "<a href=\"http://www.mygreensaver.com/a8/state_form.php\">State Form</a>"; } if(isset($_POST['delete_state'])) { $edit = "{$_POST[abbr]}, {$_POST[state_name]}"; $text = file_get_contents("states.csv"); $text = str_replace($edit, "", $text); file_put_contents("states.csv", $text); echo "$edit was deleted from file!!!<br>"; echo "<a href=\"http://www.mygreensaver.com/a8/state_form.php\">State Form</a>"; } ?> </body> </html>




Bookmarks