Home > Enterprise >  Adding arrays inside an array from input PHP
Adding arrays inside an array from input PHP

Time:11-13

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

CodePudding user response:

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>";

?>

CodePudding user response:

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

  • Related