473,396 Members | 2,070 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

php text file read/write

57
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:
Expand|Select|Wrap|Line Numbers
  1. &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.
Expand|Select|Wrap|Line Numbers
  1. <?
  2.  
  3. $filename = "20091115.txt";
  4.  
  5. $fp = fopen( $filename,"r"); 
  6. $Old = fread($fp, 100); 
  7. fclose( $fp ); 
  8.  
  9. $Old = split("=", $Old, 4);
  10.  
  11. $NewYesCount = $Old[1] + '1';
  12.  
  13. $New = "countYes=$NewYesCount";
  14.  
  15. $fp = fopen( $filename,"w+");
  16. if (flock($fp, 2)) { 
  17. fwrite($fp, $New, 100); }
  18. fclose( $fp ); 
  19.  
  20. ?>
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!
Nov 16 '09 #1
4 2537
Atli
5,058 Expert 4TB
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:
Expand|Select|Wrap|Line Numbers
  1. &countYes 
  2. 243&countNo 
  3. 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.
Expand|Select|Wrap|Line Numbers
  1. // Split the string into YES and NO parts.
  2. // Note, explode() is preferable to split(). (See the manual for details)
  3. $parts = explode('&', $input);
  4.  
  5. // Because you had a & at the start of the string
  6. // you will have an empty string before the other two.
  7. // So the YES part will be at index 1 and the NO part
  8. // will be at index 2.
  9.  
  10. // Split the "countYes=243" and "countNo=243"
  11. // parts on the "=" char.
  12. $yes_parts = explode('=', $parts[1]);
  13. $no_parts = explode('=', $parts[2]);
  14.  
  15. // Fetch the values from the yes and no part arrays.
  16. $yes_count = $yes_parts[1];
  17. $no_count = $no_parts[1];
  18.  
You can also use a more advanced method, regular expressions.
Expand|Select|Wrap|Line Numbers
  1. preg_match('/&countYes=(\d+)&countNo=(\d+)/i', $input, $output);
  2. $yes_count = $output[1];
  3. $no_count = $output[2];
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.
Expand|Select|Wrap|Line Numbers
  1. // Assuming $new_yes and $new_no hold the updated
  2. // values for their respective vote counts.
  3. $new_string = "&countYes={$new_yes}&countNo={$new_no}";
  4.  
  5. $fh = fopen("wb", "/path/to/file.ext");
  6. if($fh) 
  7. {
  8.     fwrite($fh, $new_string);
  9.     fclose($fh);
  10. }
Nov 16 '09 #2
npm
57
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:
Expand|Select|Wrap|Line Numbers
  1. <?
  2.  
  3. $vote = $HTTP_POST_VARS['sender_vote'];
  4.  
  5. $filename = "vote/20091115.txt";
  6.  
  7. $fp = fopen($filename,"r"); 
  8. $Old = fread($fp, 100); 
  9. fclose($fp); 
  10.  
  11. // Split the string into YES and NO parts.
  12. // Note, explode() is preferable to split(). (See the manual for details)
  13. $parts = explode('&', $input);
  14.  
  15. // Because you had a & at the start of the string you will have an empty string before the other two.
  16. // So the YES part will be at index 1 and the NO part will be at index 2.
  17.  
  18. // Split the "countYes=243" and "countNo=31" parts on the "=" char.
  19. $yes_parts = explode('=', $parts[1]);
  20. $no_parts = explode('=', $parts[2]);
  21.  
  22. // Fetch the values from the yes and no part arrays.
  23. $yes_count = $yes_parts[1];
  24. $no_count = $no_parts[1];
  25.  
  26. // Update the vote counts, based on a "YES" vote or a "NO" vote.
  27. if ($vote = "Yes") {
  28.     $new_yes = $yes_count + 1;
  29. }
  30. else {
  31.     $new_yes = $yes_count;
  32. }
  33. if ($vote = "No") {
  34.     $new_no = $no_count + 1;
  35. }
  36. else {
  37.     $new_no = $no_count;
  38. }
  39.  
  40. // Assuming $new_yes and $new_no hold the updated values for their respective vote counts.
  41. $new_string = "&countYes={$new_yes}&countNo={$new_no}";
  42.  
  43. $fh = fopen($filename, "wb");
  44. if ($fh) {
  45.     fwrite($fh, $new_string);
  46.     fclose($fh);
  47. }
  48.  
  49. ?>
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.
Nov 18 '09 #3
Atli
5,058 Expert 4TB
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.
Nov 18 '09 #4
npm
57
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:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $vote = $_POST['sender_vote'];
  3.  
  4. $filename = "vote/20091115.txt";
  5.  
  6. $fp = fopen($filename,"r"); 
  7. $Old = fread($fp, 100); 
  8. fclose($fp); 
  9.  
  10. // Split the string into YES and NO parts.
  11. // Note, explode() is preferable to split(). (See the manual for details)
  12. $parts = explode('&', $Old);
  13.  
  14. // Because you had a & at the start of the string you will have an empty string before the other two.
  15. // So the YES part will be at index 1 and the NO part will be at index 2.
  16.  
  17. // Split the "countYes=243" and "countNo=31" parts on the "=" char.
  18. $yes_parts = explode('=', $parts[1]);
  19. $no_parts = explode('=', $parts[2]);
  20.  
  21. // Fetch the values from the yes and no part arrays.
  22. $yes_count = $yes_parts[1];
  23. $no_count = $no_parts[1];
  24.  
  25. // Update the vote counts, based on a "YES" vote or a "NO" vote.
  26. if ($vote = "Yes") {
  27.     $new_yes = $yes_count + 1;
  28. }
  29. else {
  30.     $new_yes = $yes_count;
  31. }
  32. if ($vote = "No") {
  33.     $new_no = $no_count + 1;
  34. }
  35. else {
  36.     $new_no = $no_count;
  37. }
  38.  
  39. // Assuming $new_yes and $new_no hold the updated values for their respective vote counts.
  40. $new_string = "&countYes={$new_yes}&countNo={$new_no}";
  41.  
  42. $fh = fopen($filename, "wb");
  43. if ($fh) {
  44.     fwrite($fh, $new_string);
  45.     fclose($fh);
  46. }
  47.  
  48. ?>
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.
Dec 2 '09 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

19
by: Lionel B | last post by:
Greetings, I need to read (unformatted text) from stdin up to EOF into a char buffer; of course I cannot allocate my buffer until I know how much text is available, and I do not know how much...
8
by: Eric Lilja | last post by:
Hello, I had what I thought was normal text-file and I needed to locate a string matching a certain pattern in that file and, if found, replace that string. I thought this would be simple but I had...
12
by: Adam J. Schaff | last post by:
I am writing a quick program to edit a binary file that contains file paths (amongst other things). If I look at the files in notepad, they look like: ...
7
stealwings
by: stealwings | last post by:
I have a little problem with my program, or maybe it is not that little, anyway here is the code: #include "stdafx.h" #include <iostream> using namespace std; #include <fstream> using...
5
by: =?Utf-8?B?RGF2aWRF?= | last post by:
Hi, I have to open a text file in c# and read few lines and then I need to update some lines in the middle of the file. I can read and write by using the streamReader or streamWriter but I don't...
4
by: someone28485 | last post by:
hi,i need 2 write code in C#.Net to read a text file with text as 22.13 21.65 25.16 31.22 and so on, i need 2 read this text and then take mean of the 4 values and write that mean result in...
5
by: dm3281 | last post by:
Hello, I have a text report from a mainframe that I need to parse. The report has about a 2580 byte header that contains binary information (garbage for the most part); although there are a...
4
by: Keith G Hicks | last post by:
I'm trying to read a text file and alter the contents of specific lines in the file. I know how to use streamreader to read each line of a file. I'm doing that already to get the data into a...
15
by: pakerly | last post by:
How would i do this, convert a test file to excel? Lets say my text file has fields like this: NUMBER NAME ADDRESS PHONE 11002 Test1 ...
0
Debadatta Mishra
by: Debadatta Mishra | last post by:
Introduction In this article I will provide you an approach to manipulate an image file. This article gives you an insight into some tricks in java so that you can conceal sensitive information...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.