1

I want to append a txt file with for loop data using php. Here is my code

$post_array = array('hdnItemTypeID', 'hdnItemType', 'hdnItemCode',  'hdnCost', 'txtDescription');
        foreach ($post_array as $pos) {
            foreach ($_POST[$pos] as $id => $row) {
                $_POST[$pos][$id] = $row;
            }
        }
$ids = $_POST['hdnItemTypeID'];
        $ItemTypeID = $_POST['hdnItemTypeID'];
        $ItemType = $_POST['hdnItemType'];
        $ItemCode = $_POST['hdnItemCode'];
        //$Discount = $_POST['txtDiscount'];
        $Discount = '0';
        $UnitCost = $_POST['hdnCost'];
        //$Amount = $UnitPrice + $Discount;
        $Description = $_POST['txtDescription'];
        $size = count($ids);
 for ($i = 0; $i < $size; $i++) {
            // Check for part id
            if (empty($ids[$i])) {
                continue;
            }
            $items = array(
                ':ItemTypeID' => $ItemTypeID[$i],
                ':ItemType' => $ItemType[$i],
                ':ItemCode' => $ItemCode[$i],
                ':Description' => $Description[$i],
                ':UnitCost' => $UnitCost[$i],
                ':Amount' => $UnitCost[$i] +$Discount,
                ':Discount' => $Discount
            );
            file_put_contents('Check.txt', print_r($items,true));
        }

But only single array is getting is getting in the file. How to get althe array values in it?

1

2 Answers 2

2

file_put_contents by default overwrites everything. Use:

file_put_contents('Check.txt', $items, FILE_APPEND);

REFERENCE

Sign up to request clarification or add additional context in comments.

Comments

0

I think you cant use

file_put_contents('Check.txt', print_r($items,true), FILE_APPEND);

See http://php.net/manual/fr/function.file-put-contents.php

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.