davidx714 Posted April 6, 2010 Share Posted April 6, 2010 Hello everyone. I am trying to learn PHP and I am stuck. I have a text file that has the numbers 1 through 9, each listed on its own line. I want to read each line of the file, and write it to a new text file UNLESS one of the numbers is passed through the URL. So for example, if the url variable is 8, then it will read the first file and write 1 2 3 4 5 6 7 9 To the second file. Here is what I got: <?php $linknum = $_GET['linknum']; //----This works if $xx = 8; //$xx = 8; //----but it does not work if $xx = $linknum; $xx = $linknum; $data = file ("Source_file.txt"); $b = fopen("New_File.txt", "w+"); foreach ($data as $zz) { if ($zz != $xx){ fwrite($b, $zz); } } ?> Please help me! Quote Link to comment https://forums.phpfreaks.com/topic/197707-variable-does-not-want-to-work/ Share on other sites More sharing options...
Pikachu2000 Posted April 6, 2010 Share Posted April 6, 2010 It would be helpful to see the form you're using to submit the data to this script. Quote Link to comment https://forums.phpfreaks.com/topic/197707-variable-does-not-want-to-work/#findComment-1037571 Share on other sites More sharing options...
Pikachu2000 Posted April 6, 2010 Share Posted April 6, 2010 On second look, nevermind. I see you're simply passing the value in the URL. I just changed it a little to read the values from an an array, and then echo the result instead of writing a file and tested it locally, and it seems to work just fine. Quote Link to comment https://forums.phpfreaks.com/topic/197707-variable-does-not-want-to-work/#findComment-1037577 Share on other sites More sharing options...
davidx714 Posted April 6, 2010 Author Share Posted April 6, 2010 Forgive my ignorance ... I just started learning ... can you show me what you did to get it to work? Quote Link to comment https://forums.phpfreaks.com/topic/197707-variable-does-not-want-to-work/#findComment-1037592 Share on other sites More sharing options...
oni-kun Posted April 6, 2010 Share Posted April 6, 2010 $b = fopen("New_File.txt", "w+"); w+ will clear the entire file and its contents, and place the pointer at the beginning of the file. Do you mean "w"? Quote Link to comment https://forums.phpfreaks.com/topic/197707-variable-does-not-want-to-work/#findComment-1037595 Share on other sites More sharing options...
davidx714 Posted April 6, 2010 Author Share Posted April 6, 2010 I just tried "w" and the same thing happens: It works if $xx = 8; but it does not work if $xx = $linknum; Quote Link to comment https://forums.phpfreaks.com/topic/197707-variable-does-not-want-to-work/#findComment-1037597 Share on other sites More sharing options...
anupamsaha Posted April 6, 2010 Share Posted April 6, 2010 Please try using "rb+" instead of "w". Hope this will help. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/197707-variable-does-not-want-to-work/#findComment-1037646 Share on other sites More sharing options...
frankienrg Posted April 6, 2010 Share Posted April 6, 2010 well well, i thinks it's the way you pass the variable that doesn't work: the url should be: http://www.somedomain.com/index.php?linknum=8 Quote Link to comment https://forums.phpfreaks.com/topic/197707-variable-does-not-want-to-work/#findComment-1037665 Share on other sites More sharing options...
Pikachu2000 Posted April 6, 2010 Share Posted April 6, 2010 I just figured out what is going on here. If you typecast the $xx variable as an integer when you assign its value from $linknum, it works just fine. I also made the code a little more compact by eliminating redundancy in assigning values to variables. <?php $xx = (int) $_GET['linknum']; // typecast as an integer here, now the != comparison works when value is passed via URL. $data = file ("Source_file.txt"); $b = fopen("New_File.txt", "w+"); foreach ($data as $zz) { if ($zz != $xx){ fwrite($b, $zz); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/197707-variable-does-not-want-to-work/#findComment-1037756 Share on other sites More sharing options...
davidx714 Posted April 6, 2010 Author Share Posted April 6, 2010 Pikachu2000! YES! That did it. Thank you very much!!! Quote Link to comment https://forums.phpfreaks.com/topic/197707-variable-does-not-want-to-work/#findComment-1037813 Share on other sites More sharing options...
Pikachu2000 Posted April 6, 2010 Share Posted April 6, 2010 No problem at all . . . Quote Link to comment https://forums.phpfreaks.com/topic/197707-variable-does-not-want-to-work/#findComment-1037817 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.