douli7841 2014-11-02 18:19
浏览 32
已采纳

使用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.

  • 写回答

2条回答 默认 最新

  • douhuan9289 2014-11-02 19:32
    关注

    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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵