1

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?

Mag
  • 89
  • 1
  • 11
  • Why don't you just take an array, serialize it to file, then when you need it again unserialize it to array again. – Tschallacka Mar 12 '18 at 19:09
  • Also, in general, you don't want to include the same file multiple times. Put the code that you want to run multiple times within a function, include the file once, and call the function as many times as you need to. – Patrick Q Mar 12 '18 at 19:13
  • @PatrickQ Sorry, it's "blankVoteOB.txt". Also, I already tried what you suggested but only the first value is being read and deleted – Mag Mar 12 '18 at 19:22
  • @Tschallacka But then does that not mean I'll have to loop through the array since the first values would have been deleted once used? – Mag Mar 12 '18 at 19:24

1 Answers1

1

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);
Tschallacka
  • 27,901
  • 14
  • 88
  • 133