SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Nov 2006
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Same flat files... Brand new problem.

    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:
    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>
    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>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>

  2. #2
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    at the end of each line, are new line characters.

    either \n or \r or \r\n

    now, when a condition is not working as expected, inspect the conditon
    if (!$line==""){


    obviously, $line is not what you think it is. use var_dump() to see the contents of variables. view the html source output, never let a browser render output for you, look at the raw text instead.

    this is cause by your way of deleting a line. you delete the contents of the line, but never delete the new line character(s) so you end up with "blank lines".

    you might be able to just use your existing code but use
    $edit = "{$_POST[abbr]}, {$_POST[state_name]}\n";

    and change \n to the appropriate line ending if needed. that could get messy though if you start getting different line endings in there.

    to delete a line, i would recomend you just loop through each line 1 by 1. when you find the line you dont want, dont write it back to the file. write all other lines back to the file.

  3. #3
    SitePoint Member
    Join Date
    Nov 2006
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the solution to my problem was very simple ...
    Code:
    $line = trim(fgets($fp, 4096));
    trim took out the white spaces for me... problem solved....
    var_dump did lead me to that solution so thanks clammy...

  4. #4
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you would be doing a good thing by not allowing your database to be filled with blank lines in the first place.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •