Search code examples
phpfile-manipulation

Call a PHP file from another one multiple times (+ file reading)


What I am trying to do is read a value from blankVoteOB.txt, delete the value and repeat this process multiple times.

blankVote.php

global $cur_random;

for ($i=0; $i<2; $i++){
    include 'readWriteRandomBV.php';
    $random[$i]=$cur_random;
    echo $random[$i];
    echo ',';
}

readWriteRandomBV.php (reads current line from file blankVoteOB.txt and then deletes the line)

<?php
    $dir = "blankVoteOB.txt";
    $file = fopen($dir, "r") or exit("Unable to open file!");

    global $lines;
    global $line_no;
    global $all_lines;
    global $writelines;
    global $cur_random;

    $lines = "";
    $line_no=0;

    while(!feof($file)) {
        $line_no++;
        $line = fgets($file);
        $all_lines .= $line."<br>";

        if($line_no > 1)
            $writelines .= $line;
        else{
            $curran = trim($line);
            $cur_random = $curran;
        }
    }
    fclose($file);

    $fh = fopen($dir, 'w') or die("ERROR! Cannot open $file file!");
    fwrite($fh, $writelines);
    fclose($fh);
?>

Before running the PHPs, blankVoteOB.txt looks like this:

313328804459
159078851698
226414688415
380287830671
301815692106
2991355110

After being ran, it becomes:

159078851698
226414688415
380287830671
301815692106
2991355110
226414688415
380287830671
301815692106
2991355110

What I want is:

226414688415
380287830671
301815692106
2991355110

What am I doing wrong here?


Solution

  • I suggest you use an array to store the ballots, and then use array_shift to get the first item from the array.

    I prefer using classes so I made a lottery class which allows you to "draw" the first item in the array.

    If you run the code below and match the text output to the code you can see what it does.

    See it live here: https://ideone.com/T8stdB

    <?php
    namespace Lottery;
    class Lotto {
        protected $lots;
        public function __construct($lots = []) 
        {
            $this->lots = $lots;    
        }
        public function draw() {
            return array_shift($this->lots);
        }
    }
    
    namespace BallotGuy;
    use Lottery\Lotto;
    $lotto = new Lotto([313328804459,
                    159078851698,
                    226414688415,
                    380287830671,
                    301815692106,
                    2991355110,
            ]);
    echo "Lotto status at this point\n";
    echo "===========================================================\n";
    var_dump($lotto);
    echo "===========================================================\n";
    echo "Drawn: " . $lotto->draw()."\n";
    echo "\nLotto status at this point\n";
    echo "===========================================================\n";
    var_dump($lotto);
    echo "===========================================================\n";
    $saved = serialize($lotto);
    //file_put_contents('ballots.txt',$saved);
    
    /**
     * setting to null to emulate script ending 
     */
    $lotto = null;
    echo "Lotto set to null 'script' ends sort to speak here\n";
    echo "\nLotto status at this point\n";
    echo "===========================================================\n";
    var_dump($lotto);
    echo "===========================================================\n";
    echo "Loading lotto from file\n";
    //$saved = file_get_contents('ballots.txt');
    $lotto = unserialize($saved);
    echo "\nLotto status at this point\n";
    echo "===========================================================\n";
    var_dump($lotto);
    echo "===========================================================\n";
    echo "Drawn: ". $lotto->draw()."\n";
    echo "\nLotto status at this point\n";
    echo "===========================================================\n";
    var_dump($lotto);
    echo "===========================================================\n";
    

    A version without the superfluos var_dumping

    See it live https://ideone.com/YNKIM4

    <?php
    namespace Lottery;
    class Lotto {
        protected $lots;
        public function __construct($lots = []) 
        {
            $this->lots = $lots;    
        }
        public function draw() {
            return array_shift($this->lots);
        }
    }
    
    namespace BallotGuy;
    use Lottery\Lotto;
    /**
     * initialize lotto object
     */
    $lotto = new Lotto([313328804459,
                    159078851698,
                    226414688415,
                    380287830671,
                    301815692106,
                    2991355110,
            ]);
    
    echo "Drawn: " . $lotto->draw()."\n";
    echo "Writing lotto to file. Ending script(j/k)\n";
    $saved = serialize($lotto);
    file_put_contents('ballots.txt',$saved);
    /**
     * setting to null to emulate script ending 
     */
    $lotto = null;
    $saved = null;
    echo "Loading lotto from file\n";
    $saved = file_get_contents('ballots.txt');
    $lotto = unserialize($saved);
    
    echo "Drawn: ". $lotto->draw()."\n";
    
    var_dump($lotto);