如何从多个关联数组中访问和打印值?

时间:2017-04-24 05:08:04

标签: php post associative-array

我有2个关联数组,“饮料”和“零食”。 每个都以表格形式显示为下拉/选择列表,一旦做出选择,用户点击提交按钮。这段代码似乎工作正常。 (如下所示)

// create an an associative array for drinks & snacks
$drinks=array("Water"=>2, "Tea"=>3, "Coffee"=>4);
$snacks=array("Nuts"=>4, "Chips"=>3, "Crackers"=>2);

// initialise variables
$drinksCost = 0;
$snacksCost = 0;
$sum = 0;
?>

<!-- create a dropdown form & submit button -->
<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF'])?>" method="POST">
    <h4>Drinks</h4>     
    <select>
    <?php   
        foreach($drinks as $key => $value) {
            echo '<option value ="'.$key. '">' .$key. '</option>';
        }   
    ?>  
    </select>   

    <h4>Snacks</h4>     
    <select>
    <?php   
        foreach($snacks as $key => $value) {
            echo '<option value ="'.$key. '">' .$key. '</option>';
        }   
    ?>  
    </select>   

    <br><br>
    <input type='submit' name='submit' value='submit'>
</form>

我遇到了下一步的问题,我需要:

  1. 显示每个阵列中的选定项目及其价格(所以我应该有一个饮料项目和一个零食项目);然后

  2. 添加两个价格并显示总金额。

  3. 问题代码在下面,这只是打印出每个项目,然后添加每个数组中最后一项的价格。我将非常感谢任何帮助!

    <?php
        if(isset($_POST['submit'])){
    
            foreach ($drinks as $key => $value){
                $drinksCost = $value;
                echo "<br>Drink order: ".$key .". Cost: $" .$value ."<br>";
            }
    
            foreach ($snacks as $key => $value){
                $snacksCost = $value;
                echo "<br>Drink order: ".$key .". Cost: $" .$value ."<br>";
            }
    
            $sum = $drinksCost + $snacksCost;
    
            echo "<br>Total Cost is: $ " .$sum;
        }
    ?>
    

2 个答案:

答案 0 :(得分:1)

1)名称属性必须。除非您的输入值未发布到目标页面

<select name="drinks">
<?php   
    foreach($drinks as $key => $value) {
        echo '<option value ="'.$key. '">' .$key. '</option>';
    }   
?>  
</select> 

2)不需要foreach只使用post值作为键直接访问值。就像这个$drinks[$_POST['drinks']];

    <?php

    $drinks=array("Water"=>2, "Tea"=>3, "Coffee"=>4);
    $snacks=array("Nuts"=>4, "Chips"=>3, "Crackers"=>2);

        if(isset($_POST['submit'])){

            $total =0;
            if(isset($_POST['drinks']) && !empty($_POST['drinks'])){

                $drinksCost = $drinks[$_POST['drinks']];

                $total+= $drinksCost;
                echo "<br>Drink order: ".$_POST['drinks'] .". Cost: $" .$_POST['drinks'] ."<br>";
            }

           if(isset($_POST['snacks']) && !empty($_POST['snacks'])){

                $snacksCost = $snacks[$_POST['snacks']];
               $total+= $snacksCost ;
                echo "<br>snacks order: ".$_POST['snacks'] .". Cost: $" .$_POST['snacks'] ."<br>";
            }

            echo "<br>Total Cost is: $ " .$total;
       }

    ?>

答案 1 :(得分:0)

    <?php
$sum = 0;

        if(isset($_POST['submit'])){

            foreach ($drinks as $key => $value){
                $drinksCost = $value;
$sum += $drinksCost;
                echo "<br>Drink order: ".$key .". Cost: $" .$value ."<br>";
            }

            foreach ($snacks as $key => $value){
                $snacksCost = $value;
$sum += $drinksCost;
                echo "<br>Drink order: ".$key .". Cost: $" .$value ."<br>";
            }

            echo "<br>Total Cost is: $ " .$sum;
        }
    ?>