使用php保持表中的值而不使用会话或cookie

I have this assignment and i have been working on it all weekend.

Here is the scope of the assignment,

  1. Products are stored in a multidimensional array
  2. Display the products in a table
  3. Update the product quantity using '+' and '-' button
  4. If quantity > 0, display a '-' button to update quantity as required.
  5. Checkout button to display -- quantities > 0, total prices of products whose quantities are greater than 0.

I am stuck at updating the table because each time i click on a different '+' button on the table, the other table rows reverse back to their original state and i don't want that. I want their values to remain after clicking on another '+' button on the table.

  • I updated product 1 quantity then
  • I updated product 2 quantity and the updated quantity and subtotal price in product 1 is set back to its initial array values.

The logic i want to use is "update whole multidimensional array after each click on various '+' and '-' button and display the 'updated' array back into the table. But I have problem implementing this logic or is their another logic to go about this??

Here is my code so far for the assignment.

    <!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>

    </head>
    <body>
        <?php
        // put your code here
        $ShowForm = True;
        $items = array(
            "Product 1" => array
                (
                "Qty" => 0,
                "Item" => "Product 1",
                "Unit Price" => 5,
                "Subtotal" => 0
            ),
            "Product 2" => array
                (
                "Qty" => 0,
                "Item" => "Product 2",
                "Unit Price" => 2.3,
                "Subtotal" => 0
            ),
            "Product 3" => array
                (
                "Qty" => 0,
                "Item" => "Product 3",
                "Unit Price" => 3.54,
                "Subtotal" => 0
            ),
            "Product 4" => array
                (
                "Qty" => 0,
                "Item" => "Product 4",
                "Unit Price" => 1.67,
                "Subtotal" => 0
            ),
            "Product 5" => array
                (
                "Qty" => 0,
                "Item" => "Product 5",
                "Unit Price" => 3.4,
                "Subtotal" => 0
            )
        );
            ?>
            <h3>Update the products below as required.<br>Then you can click on the checkout button below.</h3>
            <form method="POST" action= "<?php $_SERVER['PHP_SELF']; ?>" > 
                <?php
                echo '<table border = 1 id=\'products\'>';
                echo '<th>Qty</th>';
                echo '<th>Item</th>';
                echo '<th>Unit Price</th>';
                echo '<th>Subtotal</th>';

                $i = count($items);
                foreach ($items as $key => $value) {
                    echo'<tr class=\'' . $key . '\'>';
                    $i--;

                    if ($key === "Product 1") {
                        $product1 = array();
                        foreach ($value as $dim => &$num) {
                            if ($dim === "Qty") {
                                echo '<td><input type=\'submit\' value=\'+\' name=\'add' . $i . '\'>&nbsp;&nbsp;&nbsp;' . $num;
                                if (ISSET($_POST['add4'])) {
                                    $newQty = ($_POST['qty4']) + 1;
                                    echo $newQty;
                                    //echo '<input type=\'submit\' value=\'+\' name=\'add'.$i.'\'>&nbsp;&nbsp;&nbsp;'.$newQty;
                                    echo '<input type=\'hidden\' value=\'' . $newQty . '\' name=\'qty4\'>';
                                    echo '<input type=\'submit\' value=\'-\' name=\'remove4\'>&nbsp;&nbsp;&nbsp;</td>';
                                    $value['Subtotal'] = $newQty * $value['Unit Price'];
                                    $value['Qty'] = $newQty;
                                }
                                if (ISSET($_POST['remove4'])) {
                                    $newQty = ($_POST['qty4']) - 1;
                                    echo $newQty;
                                    //echo '<input type=\'submit\' value=\'+\' name=\'add'.$i.'\'>&nbsp;&nbsp;&nbsp;'.$newQty;
                                    echo '<input type=\'hidden\' value=\'' . $newQty . '\' name=\'qty4\'>';
                                    $value['Subtotal'] = $newQty * $value['Unit Price'];
                                    if ($newQty > 0) {
                                        echo '<input type=\'submit\' value=\'-\' name=\'remove4\'>&nbsp;&nbsp;&nbsp;</td>';
                                    }
                                }
                            } elseif ($dim === "Subtotal") { 
                                echo '<td>'.$value['Subtotal'].'</td>';// . '</td>';
                            }else {

                                echo '<td>' . $num . '</td>';
                            }
                        }
                    }


                    if ($key === "Product 2") {
                        foreach ($value as $dim => &$num) {


                            if ($dim === "Qty") {
                                echo '<td><input type=\'submit\' value=\'+\' name=\'add' . $i . '\'>&nbsp;&nbsp;&nbsp;' . $num;
                                if (ISSET($_POST['add3'])) {
                                    $newQty = ($_POST['qty3']) + 1;
                                    echo $newQty;
                                    //echo '<input type=\'submit\' value=\'+\' name=\'add'.$i.'\'>&nbsp;&nbsp;&nbsp;'.$newQty;
                                    echo '<input type=\'hidden\' value=\'' . $newQty . '\' name=\'qty3\'>';
                                    echo '<input type=\'submit\' value=\'-\' name=\'remove3\'>&nbsp;&nbsp;&nbsp;</td>';
                                    $value['Subtotal'] = $newQty * $value['Unit Price'];
                                    $value['Qty'] = $newQty;        
                                }
                             if (ISSET($_POST['remove3'])) {
                                    $newQty = ($_POST['qty3']) - 1;
                                    echo $newQty;
                                    //echo '<input type=\'submit\' value=\'+\' name=\'add'.$i.'\'>&nbsp;&nbsp;&nbsp;'.$newQty;
                                    echo '<input type=\'hidden\' value=\'' . $newQty . '\' name=\'qty3\'>';
                                    $value['Subtotal'] = $newQty * $value['Unit Price'];
                                    if ($newQty > 0) {
                                        echo '<input type=\'submit\' value=\'-\' name=\'remove3\'>&nbsp;&nbsp;&nbsp;</td>';
                                    }

                                    //
                                }

                            }elseif ($dim === "Subtotal") { 
                                echo '<td>'.$value['Subtotal'].'</td>';// . '</td>';
                            }else {
                                echo '<td>' . $num . '</td>';
                            }
                        }
                    }
                    if ($key === "Product 3") {
                        foreach ($value as $dim => $num) {
                            if ($dim === "Qty") {
                                echo '<td><input type=\'submit\' value=\'+\' name=\'add' . $i . '\'>&nbsp;&nbsp;&nbsp;' . $num;
                                if (ISSET($_POST['add2'])) {
                                    $newQty = ($_POST['qty2']) + 1;
                                    echo $newQty;

                                    //echo '<input type=\'submit\' value=\'+\' name=\'add'.$i.'\'>&nbsp;&nbsp;&nbsp;'.$newQty;
                                    echo '<input type=\'hidden\' value=\'' . $newQty . '\' name=\'qty2\'>';
                                    echo '<input type=\'submit\' value=\'-\' name=\'remove2\'>&nbsp;&nbsp;&nbsp;</td>';
                                    $value['Subtotal'] = $newQty * $value['Unit Price'];
                                }

                                if (ISSET($_POST['remove2'])) {
                                    $newQty = ($_POST['qty2']) - 1;
                                    echo $newQty;
                                    //echo '<input type=\'submit\' value=\'+\' name=\'add'.$i.'\'>&nbsp;&nbsp;&nbsp;'.$newQty;
                                    echo '<input type=\'hidden\' value=\'' . $newQty . '\' name=\'qty2\'>';
                                    $value['Subtotal'] = $newQty * $value['Unit Price'];
                                    if ($newQty > 0) {
                                        echo '<input type=\'submit\' value=\'-\' name=\'remove2\'>&nbsp;&nbsp;&nbsp;</td>';
                                    }
                                }
                            } elseif ($dim === "Subtotal") { 
                                echo '<td>'.$value['Subtotal'].'</td>';// . '</td>';
                            }else {
                                echo '<td>' . $num . '</td>';
                            }
                        }
                    }
                    if ($key === "Product 4") {
                        foreach ($value as $dim => $num) {
                            if ($dim === "Qty") {
                                echo '<td><input type=\'submit\' value=\'+\' name=\'add' . $i . '\'>&nbsp;&nbsp;&nbsp;' . $num;
                                if (ISSET($_POST['add1'])) {
                                    $newQty = ($_POST['qty1']) + 1;
                                    echo $newQty;
                                    //echo '<input type=\'submit\' value=\'+\' name=\'add'.$i.'\'>&nbsp;&nbsp;&nbsp;'.$newQty;
                                    echo '<input type=\'hidden\' value=\'' . $newQty . '\' name=\'qty1\'>';
                                    echo '<input type=\'submit\' value=\'-\' name=\'remove1\'>&nbsp;&nbsp;&nbsp;</td>';
                                    $value['Subtotal'] = $newQty * $value['Unit Price'];
                                }

                                if (ISSET($_POST['remove1'])) {
                                    $newQty = ($_POST['qty1']) - 1;
                                    echo $newQty;
                                    //echo '<input type=\'submit\' value=\'+\' name=\'add'.$i.'\'>&nbsp;&nbsp;&nbsp;'.$newQty;
                                    echo '<input type=\'hidden\' value=\'' . $newQty . '\' name=\'qty1\'>';
                                    $value['Subtotal'] = $newQty * $value['Unit Price'];
                                    if ($newQty > 0) {
                                        echo '<input type=\'submit\' value=\'-\' name=\'remove1\'>&nbsp;&nbsp;&nbsp;</td>';
                                    }
                                }
                            } elseif ($dim === "Subtotal") { 
                                echo '<td>'.$value['Subtotal'].'</td>';// . '</td>';
                            }else {
                                echo '<td>' . $num . '</td>';
                            }
                        }
                    }
                    if ($key === "Product 5") {
                        foreach ($value as $dim => $num) {
                            if ($dim === "Qty") {
                                echo '<td><input type=\'submit\' value=\'+\' name=\'add' . $i . '\'>&nbsp;&nbsp;&nbsp;' . $num;
                                if (ISSET($_POST['add0'])) {
                                    $newQty = ($_POST['qty0']) + 1;
                                    echo $newQty;
                                    //echo '<input type=\'submit\' value=\'+\' name=\'add'.$i.'\'>&nbsp;&nbsp;&nbsp;'.$newQty;
                                    echo '<input type=\'hidden\' value=\'' . $newQty . '\' name=\'qty0\'>';
                                    echo '<input type=\'submit\' value=\'-\' name=\'remove0\'>&nbsp;&nbsp;&nbsp;</td>';
                                    $value['Subtotal'] = $newQty * $value['Unit Price'];
                                }
                                if (ISSET($_POST['remove0'])) {
                                    $newQty = ($_POST['qty0']) - 1;
                                    echo $newQty;
                                    //echo '<input type=\'submit\' value=\'+\' name=\'add'.$i.'\'>&nbsp;&nbsp;&nbsp;'.$newQty;
                                    echo '<input type=\'hidden\' value=\'' . $newQty . '\' name=\'qty0\'>';
                                    $value['Subtotal'] = $newQty * $value['Unit Price'];

                                    if ($newQty > 0) {
                                        echo '<input type=\'submit\' value=\'-\' name=\'remove0\'>&nbsp;&nbsp;&nbsp;</td>';
                                    }
                                }
                            } elseif ($dim === "Subtotal") { 
                                echo '<td>'.$value['Subtotal'].'</td>';// . '</td>';
                            }else {
                                echo '<td>' . $num . '</td>';
                            }
                        }
                    }
                    echo'</tr>';
                }
                ?>


                <p><input type="submit" value="Check Out" name="checkout">
            </form>
    <?php
if (ISSET($_POST['checkout'])) {
    echo $newQty;
}
?>
    </body>

</html>

P.S: I'm not asking for code handout, just explanations and code snippets (examples) is fine. Thanks.

Here are the key items that you need to work on. See what this will produce and you should get an idea of what to do. You will have to apply logic to get your other effects, but you can see how to kind of go about it.

$item = array(
        array
            (
            "Qty" => 0,
            "Item" => "Product 1",
            "Unit_Price" => 5,
            "Subtotal" => 0
        ),
       array
            (
            "Qty" => 0,
            "Item" => "Product 2",
            "Unit_Price" => 2.3,
            "Subtotal" => 0
        ),
        array
            (
            "Qty" => 0,
            "Item" => "Product 3",
            "Unit_Price" => 3.54,
            "Subtotal" => 0
        ),
        array
            (
            "Qty" => 0,
            "Item" => "Product 4",
            "Unit_Price" => 1.67,
            "Subtotal" => 0
        ),
        array
            (
            "Qty" => 0,
            "Item" => "Product 5",
            "Unit_Price" => 3.4,
            "Subtotal" => 0
        )
    );


    function ReOrganizeArray($array) {
            $i = 0;
            foreach($array['Qty'] as $category => $rows) {
                    $items[$i]['Qty']           =   $array['Qty'][$i];
                    $items[$i]['Item']          =   $array['Item'][$i];
                    $items[$i]['Unit_Price']    =   $array['Unit_Price'][$i];
                    $items[$i]['Subtotal']      =   $array['Subtotal'][$i];

                    $items[$i]['add']           =   (isset($array['add'][$i]))? true:false;
                    $items[$i]['remove']        =   (isset($array['remove'][$i]))? true:false;
                    $i++;
                }

            return $items;
        }

    $items  =   (isset($_POST) && !empty($_POST))? ReOrganizeArray($_POST):$item; ?>

            <h3>Update the products below as required.<br>Then you can click on the checkout button below.</h3>
            <form method="POST" action= "<?php $_SERVER['PHP_SELF']; ?>" > 
            <table>
                <?php foreach($items as $i => $rows) { ?>
                <tr>
                    <td><input type="submit" name="add[<?php echo $i; ?>]" value="+" /></td>
                    <td><input type="submit" name="remove[<?php echo $i; ?>]" value="-" /></td>
                    <td><input type="text" name="Qty[<?php echo $i; ?>]" value="<?php echo $rows['Qty']; ?>" /></td>
                    <td><input type="text" name="Item[<?php echo $i; ?>]" value="<?php echo $rows['Item']; ?>" /></td>
                    <td><input type="text" name="Subtotal[<?php echo $i; ?>]" value="<?php echo $rows['Subtotal']; ?>" /></td>
                    <td><input type="text" name="Unit Price[<?php echo $i; ?>]" value="<?php echo $rows['Unit Price']; ?>" /></td>
                </tr>
                <?php
                } ?>
            </table>
            </form>

The print_r array is giving out this output.

Array
    (
        [add] => Array
            (
                [0] => +
            )

        [Qty] => Array
            (
                [0] => 0
                [1] => 0
                [2] => 0
                [3] => 0
                [4] => 0
            )

        [Item] => Array
            (
                [0] => Product 1
                [1] => Product 2
                [2] => Product 3
                [3] => Product 4
                [4] => Product 5
            )

        [Unit_Price] => Array
            (
                [0] => 5
                [1] => 2.3
                [2] => 3.54
                [3] => 1.67
                [4] => 3.4
            )

        [Subtotal] => Array
            (
                [0] => 0
                [1] => 0
                [2] => 0
                [3] => 0
                [4] => 0
            )

    )

Why is it doing that?? the grouping of each key into the $items array??