0

i have a little problem that i am trying to solve. Well i have a cart there a user can type in how many goods it wants to buy. But the problem is when the user types in for example "2" i want it to write to the text file the name of the goods meat meat for example, and not the number 2. I have an array there i store the good, price and so on.

here is the array i store:

$filename="shop.txt"; 
$lines = array(); 
$file = fopen($filename, "r"); 

while(!feof($file)) { 

//read file line by line into a new array element 
$lines[] = fgets($file, 4096); 

} 
fclose ($file); 

I got five forms for each good to input.

Meat:   <input type="text" name="cb4" value=""><br> 
        <input type="submit" value="Submit"> 
</form>

This is where i got a for loop that checks the form and writes to textfile.

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
        $result_string = '';
        foreach($_POST as $item) {

            $result_string .= $item."\n";
            $counter++;

        }            
        file_put_contents('kundvagn.txt', $result_string , FILE_APPEND);

    }

If i input 2 in the meat goods, the output is now:

Your shopping cart contains the following: 2 You got total of: 1 goods in your shoppingcart

  • 1
    You are not referencing the `$lines[]` array you created. – VIDesignz Nov 29 '15 at 18:04
  • Where is this array you speak of `I have an array there i store the good, price and so on.` – VIDesignz Nov 29 '15 at 18:06
  • $lines = array(), i read from text file and put it in the array. Text files look like this: – IamnoobatSQL Nov 29 '15 at 18:08
  • meat 2 This meat is good apple 3 This apple is green drinks 5 Very fine veg 3 Fine! nuts 2 Squirrel cathces these yesterday! – IamnoobatSQL Nov 29 '15 at 18:08
  • You could start a session at the top of each page put `session_start();` then make $lines a session array `$_SESSION['lines'][$counter] = fgets($file, 4096);` - load it with a loop sorry no time right now to develop this idea but you can use ` count($_SESSION['lines'])` as the limit for a counter for the while loop to get them out again – Steve Nov 29 '15 at 18:22
  • But how can i write out for this example instead. If the user input 2 in the goods meat, I want only to write out meat one word not two? how is that possible? i dont really understand the session_start() – IamnoobatSQL Nov 29 '15 at 18:24
  • `session_start();` enables you to use session variables which stay from page to page to you can use them on other pages as globals. – Steve Nov 29 '15 at 18:27

1 Answers1

0

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

Community
  • 1
  • 1
Steve
  • 808
  • 1
  • 9
  • 14