php text file read/write

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • npm
    New Member
    • Apr 2007
    • 57

    php text file read/write

    I'm building a simple Yes/No poll that uses Flash to vote, PHP to update the vote counts in a text file, and then Flash again to display the current results. My problem (other than being new to PHP) is getting the PHP to tell the difference between the "yes" counts and the "no" counts when it comes to updating them.

    Here's the simple text file where the counts are stored:
    Code:
    &countYes=243&countNo=31
    And here's the PHP code. I've been able to have it update one count from the text file (which is what I have below), but not two different counts.
    Code:
    <?
    
    $filename = "20091115.txt";
    
    $fp = fopen( $filename,"r"); 
    $Old = fread($fp, 100); 
    fclose( $fp ); 
    
    $Old = split("=", $Old, 4);
    
    $NewYesCount = $Old[1] + '1';
    
    $New = "countYes=$NewYesCount";
    
    $fp = fopen( $filename,"w+");
    if (flock($fp, 2)) { 
    fwrite($fp, $New, 100); }
    fclose( $fp ); 
    
    ?>
    I know it comes from having two different variables/split() functions/etc., I just can't figure out how to differentiate between the two.
    Thanks in advance!
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hey.

    You need to change the way you split your string. Currently, you just split the string on the = marks, which gives you three parts:
    Code:
    &countYes 
    243&countNo 
    31
    You are using the second one there for your calculations, and even tho the number is not the only thing there, PHP assumes you just want the number when you try to use it to do math.

    You could keep this, but it really isn't a good way of doing this. A better way would be to split the string on the & chars first, which would give you two strings, and then split each of those on the = chars.
    [code=php]
    // Split the string into YES and NO parts.
    // Note, explode() is preferable to split(). (See the manual for details)
    $parts = explode('&', $input);

    // Because you had a & at the start of the string
    // you will have an empty string before the other two.
    // So the YES part will be at index 1 and the NO part
    // will be at index 2.

    // Split the "countYes=2 43" and "countNo=24 3"
    // parts on the "=" char.
    $yes_parts = explode('=', $parts[1]);
    $no_parts = explode('=', $parts[2]);

    // Fetch the values from the yes and no part arrays.
    $yes_count = $yes_parts[1];
    $no_count = $no_parts[1];
    [/code]

    You can also use a more advanced method, regular expressions.
    [code=php]
    preg_match('/&countYes=(\d+) &countNo=(\d +)/i', $input, $output);
    $yes_count = $output[1];
    $no_count = $output[2];[/code]
    It's a bit more complex, but a lot more powerful. Worth looking into.

    From there, all you have to do is re-create the string with the updated numbers and overwrite the old one.
    [code=php]
    // Assuming $new_yes and $new_no hold the updated
    // values for their respective vote counts.
    $new_string = "&countYes={$ne w_yes}&countNo= {$new_no}";

    $fh = fopen("wb", "/path/to/file.ext");
    if($fh)
    {
    fwrite($fh, $new_string);
    fclose($fh);
    }[/code]

    Comment

    • npm
      New Member
      • Apr 2007
      • 57

      #3
      Thanks for the repsonse!

      I tried what you gave me, plus I added code to actually update the numbers, but it didn't work.

      Here's what I used:
      Code:
      <?
      
      $vote = $HTTP_POST_VARS['sender_vote'];
      
      $filename = "vote/20091115.txt";
      
      $fp = fopen($filename,"r"); 
      $Old = fread($fp, 100); 
      fclose($fp); 
      
      // Split the string into YES and NO parts.
      // Note, explode() is preferable to split(). (See the manual for details)
      $parts = explode('&', $input);
        
      // Because you had a & at the start of the string you will have an empty string before the other two.
      // So the YES part will be at index 1 and the NO part will be at index 2.
      
      // Split the "countYes=243" and "countNo=31" parts on the "=" char.
      $yes_parts = explode('=', $parts[1]);
      $no_parts = explode('=', $parts[2]);
      
      // Fetch the values from the yes and no part arrays.
      $yes_count = $yes_parts[1];
      $no_count = $no_parts[1];
      
      // Update the vote counts, based on a "YES" vote or a "NO" vote.
      if ($vote = "Yes") {
      	$new_yes = $yes_count + 1;
      }
      else {
      	$new_yes = $yes_count;
      }
      if ($vote = "No") {
      	$new_no = $no_count + 1;
      }
      else {
      	$new_no = $no_count;
      }
      
      // Assuming $new_yes and $new_no hold the updated values for their respective vote counts.
      $new_string = "&countYes={$new_yes}&countNo={$new_no}";
      
      $fh = fopen($filename, "wb");
      if ($fh) {
      	fwrite($fh, $new_string);
      	fclose($fh);
      }
      
      ?>
      What I added was the "$vote" variable that is sent from the Flash file. It's either "Yes" or "No" I also added the if() statements to check the vote and then update the appropriate count.

      As far as actually updating the counts, I assumed that went after the php retrieved the &yes_count and the &no_count variables, so that's where I put it.

      So far, it didn't work, so I must be doing something wrong.

      Comment

      • Atli
        Recognized Expert Expert
        • Nov 2006
        • 5062

        #4
        The problem is on line #13. The $input variable I used in my example was just an example. You need to swap it out for the variable you read the old string into. Which is $Old, in your case.

        Also, two things you should consider:
        1. The $HTTP_POST_VARS array is deprecated, as of PHP 4.1. You should use $_POST instead. (On line #3 in your code)
        2. You should avoid using the short_open_tags ; <? ... ?>
          They are disabled by default, so your code may be unusable on servers not specifically configured to use them.
          Ideally, you should try to always use the <?php ... ?> tags.

        Comment

        • npm
          New Member
          • Apr 2007
          • 57

          #5
          OK,
          I changed the $input variable back to my $Old variable and changed to $_POST as well as to the <?php ... ?>.

          So what I have is this:
          Code:
          <?php
          $vote = $_POST['sender_vote'];
          
          $filename = "vote/20091115.txt";
           
          $fp = fopen($filename,"r"); 
          $Old = fread($fp, 100); 
          fclose($fp); 
           
          // Split the string into YES and NO parts.
          // Note, explode() is preferable to split(). (See the manual for details)
          $parts = explode('&', $Old);
           
          // Because you had a & at the start of the string you will have an empty string before the other two.
          // So the YES part will be at index 1 and the NO part will be at index 2.
           
          // Split the "countYes=243" and "countNo=31" parts on the "=" char.
          $yes_parts = explode('=', $parts[1]);
          $no_parts = explode('=', $parts[2]);
           
          // Fetch the values from the yes and no part arrays.
          $yes_count = $yes_parts[1];
          $no_count = $no_parts[1];
           
          // Update the vote counts, based on a "YES" vote or a "NO" vote.
          if ($vote = "Yes") {
              $new_yes = $yes_count + 1;
          }
          else {
              $new_yes = $yes_count;
          }
          if ($vote = "No") {
              $new_no = $no_count + 1;
          }
          else {
              $new_no = $no_count;
          }
           
          // Assuming $new_yes and $new_no hold the updated values for their respective vote counts.
          $new_string = "&countYes={$new_yes}&countNo={$new_no}";
           
          $fh = fopen($filename, "wb");
          if ($fh) {
              fwrite($fh, $new_string);
              fclose($fh);
          }
           
          ?>
          I double checked and made sure the text file's permissions were set to 777, but it's still not being updated by the php. My guess is that my if/else statements aren't doing what I thought they would.

          Comment

          Working...