1

My goal: I wanted to make a list system that stores an array that has 3 values...

"Product's Name", 
"Product's Price", 
"Amount of Products".

And I want the user to keep adding products and be able sum it all up by multiplying the price by products and summing it all together if there's two or more products in the array.

Expectations:

"Milk", 
2.99, 
40

"Apples", 
3.00, 
5

My problem is the array input is replacing index 0.

Result:

Milk
2.99
40

I tried adding the other value, it replaces index 0. I need some help to understand this problem.

<html>
<body>
    <input id="inventory" name="p-name" type="text" placeholder="product name">
    <input id="inventory" name="p-price" type="text" placeholder="product name">
    <input id="inventory" name="p-amount" type="text" placeholder="product name">
    
    <input id="inventory" name="s-name" type="text" placeholder="product name">
    <input id="inventory" name="s-price" type="text" placeholder="product name">
    <input id="inventory" name="s-amount" type="text" placeholder="product name">
    
    <input type=submit name=submit[AddToList] value='Add to list'>
    <input type=submit name=submit[ClearAllList] value='Clear All List'>

<?php
    $listOfInventories = array();

    if (isset($_POST["submit"])) { // Checks if user clicked btn.
        $sub = $_POST["submit"];
    
        $time = $_POST["time"];
        $pName = $_POST["p-name"];
        $pPrice = $_POST["p-price"];
        $AmountOfProducts = $_POST["p-amount"];

        $sName = $_POST["s-name"];
        $sPrice = $_POST["s-price"];
        $TimesWorkersLabored = $_POST["s-amount"];
    
        if (isset($sub["AddToList"])) {
            echo " Added to list <br>";
            array_push($listOfInventories, array($pName, $pPrice, $AmountOfProducts)) ;

            foreach ($listOfInventories as $value) {  
                foreach ($value as $x) {
                   echo $x;
                }
            }

            echo count($listOfInventories);

            // Save something;
        } elseif (isset($sub["ClearAllList"])) {
            $listOfInventories = [];
            // Delete something
        }
?>
</body>
</html>

Adding first product Adding first product Adding second product Adding second product

Edit1: Possibly the fault is the initialization array, but where should I put it? It might not work before the array_push.

21
  • Do those <input> element exists inside a <form> element Commented Nov 13, 2021 at 14:51
  • Yes, the input is in a form that uses $_POST global variable. Commented Nov 13, 2021 at 14:52
  • Is the HTML and the PHP code in seperate files? If not where is the <?php tag Commented Nov 13, 2021 at 14:52
  • Oh I forgot the <?php tag Commented Nov 13, 2021 at 14:54
  • 3
    Please make sure the question makes sense and we dont have to point out 20 errors / bits you missed out before getting the the actual issue Commented Nov 13, 2021 at 14:55

2 Answers 2

1

Please use SESSION to do what you want (this is a rather standard way to handle things like "shopping cart")

Hence, try something like $_SESSION["listOfInventories"] = array(); and then array_push($_SESSION["listOfInventories"], array("Apple", 10.5, 1)) ;

Please try to run the following PHP script, and RELOAD to see the effect - it will preserve the data and add more item(s)

<?php
session_start();

if (!isset($_SESSION["listOfInventories"]))
{$_SESSION["listOfInventories"] = array();}

array_push($_SESSION["listOfInventories"], array("Apple", 10.5, 1)) ;

echo count($_SESSION["listOfInventories"]);
echo "....<br>";

array_push($_SESSION["listOfInventories"], array("Orange", 10.5, 1)) ;

echo count($_SESSION["listOfInventories"]);
echo "....<br>";

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

2 Comments

It says: Warning : session_start(): Session cannot be started after headers have already been sent in C:\xampp\htdocs\Test-1\index.php on line 183
Please put the session_start() at the TOP of your script
0

Use method="POST", put your Type="submit" attribute to Submit in Double Quote and Value="".. also put your } at the end of the if statement, add session_start() into the Login file and call only the number of inputs in the Field. use Localhost to store your Timestamp.

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.