This should load the lines - no output to browser must happen before session_start();
The counter is incremented at the top of the loop so that it will start at 1
<?php session_start(); ?>
<?php
$filename="shop.txt";
$_SESSION['lines'] = array();
$file = fopen($filename, "r");
$counter = 0;
while(!feof($file)) {
$counter++;
//read file line by line into a new array element
$_SESSION['lines'][$counter] = fgets($file, 500);
}
fclose ($file);
?>
This should also break up the file into an array without the loop
$file = fread($file, filesize($file)); // Get file contents
$_SESSION['list'] = explode ("\n", $file); // Split all the rows
You may need to have a blank line at the top of your file to occupy position 0.
And this should get them out
<?php session_start(); ?>
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$result_string = '';
foreach($_POST as $item) {
$result_string .= $_SESSION['list'][$item] . "\n";
$counter++;
}
file_put_contents('kundvagn.txt', $result_string , FILE_APPEND);
}
?>
This is a good description of how to use sessions:
http://www.w3schools.com/php/php_sessions.asp
This shows another method of getting the lines from the file - effectively it is already an array if it is read into a variable - it can then be split by the linefeeds.
Parse txt files and turn them into static html files
If they are putting the quantity in the box and not the item number they want then this should work
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$result_string = '';
foreach($_POST as $item) {
$result_string .= $item . " " . $_SESSION['list'][$counter] . "\n";
$counter++;
}
file_put_contents('kundvagn.txt', $result_string , FILE_APPEND);
}
?>
This will record even those with no text entry so you would need to think about cutting out the "0" ones:
// only list items greater than 0
if($item > 0){
$result_string .= $item . " " . $_SESSION['list'][$counter] . "\n";
}
As far as I can see the counter should be counting each POST item submitted whether it is set or not. I have not been able to check that.
You would need to put the loading counter at the bottom of the loading loop - this was put above before, on the assumption that there would be no item value "0" so there could be no array position "0" that would be relevant - other than if you wanted a message to say "You didn't pick.....". If you use the explode method that will tally OK and is probably the most efficient method anyway. You may also need to check that your POST array is in the order you expect it to be.
This might be handy for exploring your POST
array:
Print $_POST variable name along with value