Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write to text file will not work with the first item clicked

I have an odd question and I have no idea what's wrong. I am coding an interactive map of the United States. The user clicks a state and the click is recording in a text file. Then the number of clicks total is shown on the map. It's basically a quick workaround around a full database.

The code works. Each time you click a state it is added to the text file. If the state does not exist yet, an entry is made for it. If it does, then the number of clicks is simply updated. Here is the file:

<?php 
    // get the input from AJAX
    @$state = $_GET['state'];
    // get the txt file where all of the states are 
    $file = 'state_count.txt';
        //if state_count.txt exists 
        if($fopen = fopen($file, 'c')){ 
            //open it and check if the name of the state is recorded or not 
            if($strpos= strpos(file_get_contents($file), $state)){
                //if so, add 1 to the value after the state's name
                // in the formate State:#
                //cut the text file into an array by lines 
                $lines = file($file);
                //foreach line, parse the text 
                foreach($lines as $l => $k){ 
                    // create a new array $strings where each key is the STATE NAME and each value is the # of clicks 
                    $strings[explode(':', $k)[0]] = explode(':', $k)[1];
                } 
                // add 1 to the # of clicks for the state that was clicked
                $strings[$state] = $strings[$state]+1;  
                // move cursor to the end of the state's name and add 1 to accomodate the : 
                fseek($fopen, $strpos+strlen($state)+1, SEEK_SET); 
                // overwrite number in file
                fwrite($fopen, $strings[$state]); 

                // debug print($strings[$state]);

            }
            //if not, add it with the value 1
            else{ 
                file_put_contents($file, $state.":1\n", FILE_APPEND); 
            } 
        }   
        //if does not exist
        else{ 
            die('Cannot create or open file.'); 
        } 

?>

The problem I have is that the code works for all states except the FIRST state that is clicked (i.e. the text file is empty, user clicks one state, that state is the first state). In this case, it never updates the initially clicked state, it simply creates a bunch of individual entries for it. It ends up looking like this (assuming I clicked Pennsylvania first):

Pennsylvania:1
Pennsylvania:1
Utah:1
Colorado:1
Kansas:1
Nebraska:1
Wyoming:1
Indiana:1
Ohio:3
Virginia:1
West Virginia:2
Kentucky:8
Tennessee:1
Georgia:1
Alabama:2
Mississippi:1
Pennsylvania:1
Pennsylvania:1
Pennsylvania:1
Pennsylvania:1
Pennsylvania:1

I'm not sure why it's doing this, so I'm hoping a pair of fresh eyes can point out something obvious... I have a feeling it has to do with the line if statement in if($strpos= strpos(file_get_contents($file), $state)){, but I can't be sure. It seems like an odd problem for the code to work 100% correct for everything BUT the first state you click. I know it is the first state only because I've tried it many times with different states as the first.

Any ideas or suggestions?

like image 510
SanguineEpitaph Avatar asked Jul 27 '15 17:07

SanguineEpitaph


1 Answers

Note that when you use strpos to see if string exists, you should check against boolean:

if (strpos(....) !== false) { ... }

Else you will have false negative when your strpos returns 0 as in your case.

In your code you can approach like:

$strpos= strpos(file_get_contents($file), $state);
if ($strpos !== false) {...
like image 188
bksi Avatar answered Sep 23 '22 23:09

bksi