PHP 和关联数组帮助

发布于 2024-09-04 12:23:20 字数 1303 浏览 10 评论 0原文

我必须根据用户的输入和选择字段中的选择在 php 中做一个简单的计算器,如下所示:

<?php
$a = $_GET['a'];
$b = $_GET['b'];

$array = array( "option1" => 0.1,
                    "option2" => 0.15,
                    "option3" => 0.3,
                    "option4" => 3,
                    "option5" => 3,
                    "option6" => 16,
                    "option7" => 16,
                    "option8" => 16
                    );

echo "<form action='calc.php' method='get'>";
echo "<input type='text' name='a' value='".$a."'> of ";
echo "<select name='b'>";
foreach ($array as $k => $v) {
    echo "<option value='".$v."'>".$k."</option>";
}
echo "</select> ";
echo "<input type='submit' value='='> ";

$total_volume = $a * $b;

echo $total_volume;

echo "</form>";
?>

好吧,现在一切正常,但想法是在用户提交表单后,页面会重新加载输入字段中发送的金额以及用户实际选择的选项...

第一件事很简单:我只是将 value="a" 放入输入字段中,但我不确定如何在 <选择> 字段???

我从这个开始:

foreach ($array as $k => $v) {
        echo "<option value='".$v."'";
        if ($b == $v) {
            echo " selected ";
        }
        echo ">".$k."</option>";
    }

...但这显然没有按预期工作...请帮助我解决这个简单的问题:)

谢谢!

I have to do a simple calculator in php based on user's input and choice from select field, something like this:

<?php
$a = $_GET['a'];
$b = $_GET['b'];

$array = array( "option1" => 0.1,
                    "option2" => 0.15,
                    "option3" => 0.3,
                    "option4" => 3,
                    "option5" => 3,
                    "option6" => 16,
                    "option7" => 16,
                    "option8" => 16
                    );

echo "<form action='calc.php' method='get'>";
echo "<input type='text' name='a' value='".$a."'> of ";
echo "<select name='b'>";
foreach ($array as $k => $v) {
    echo "<option value='".$v."'>".$k."</option>";
}
echo "</select> ";
echo "<input type='submit' value='='> ";

$total_volume = $a * $b;

echo $total_volume;

echo "</form>";
?>

Well, for now everything works fine, but the idea is that after user submits form, the page reloads with sent amount in input field and selected option which user actually selected...

First thing is easy: I just put value="a" in my input field, but I'm not sure how to make a selected option in <select> field???

I started with this:

foreach ($array as $k => $v) {
        echo "<option value='".$v."'";
        if ($b == $v) {
            echo " selected ";
        }
        echo ">".$k."</option>";
    }

...but this is obviously not working as expected... Please help me with this easy one :)

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦屿孤独相伴 2024-09-11 12:23:20

您需要回显类似 'selected="selected"' 的内容。其余的代码对我来说似乎很好。

再想一想,存在结构性错误,因为多个选项返回相同的值,导致提交表单后无法选择正确的选项。

您需要发送 $k 作为循环中 select 中的值,并且对于计算,您只需使用 $array[$b] 而不是 $b

<?php
$a = $_GET['a'];
$b = $_GET['b'];

$array = array( "option1" => 0.1,
                    "option2" => 0.15,
                    "option3" => 0.3,
                    "option4" => 3,
                    "option5" => 3,
                    "option6" => 16,
                    "option7" => 16,
                    "option8" => 16
                    );

echo "<form action='calc.php' method='get'>";
echo "<input type='text' name='a' value='".$a."'> of ";
echo "<select name='b'>";
foreach ($array as $k => $v) {
        echo "<option value='".$k."'";
        if ($b == $k) {
            echo ' selected="selected"';
        }
        echo ">".$k."</option>";     // or $v if you want to show the number
}
echo "</select> ";
echo "<input type='submit' value='='> ";

$total_volume = $a * $array[$b];

echo $total_volume;

echo "</form>";
?>

You need to echo something like 'selected="selected"'. The rest of the code seems fine to me.

On second thought there is something structurally wrong as multiple options return the same value, making it impossible to select the right one after submitting the form.

You will need to send $k as the value in the select in your loop and for your calculations you just use $array[$b] instead of $b.

<?php
$a = $_GET['a'];
$b = $_GET['b'];

$array = array( "option1" => 0.1,
                    "option2" => 0.15,
                    "option3" => 0.3,
                    "option4" => 3,
                    "option5" => 3,
                    "option6" => 16,
                    "option7" => 16,
                    "option8" => 16
                    );

echo "<form action='calc.php' method='get'>";
echo "<input type='text' name='a' value='".$a."'> of ";
echo "<select name='b'>";
foreach ($array as $k => $v) {
        echo "<option value='".$k."'";
        if ($b == $k) {
            echo ' selected="selected"';
        }
        echo ">".$k."</option>";     // or $v if you want to show the number
}
echo "</select> ";
echo "<input type='submit' value='='> ";

$total_volume = $a * $array[$b];

echo $total_volume;

echo "</form>";
?>
霊感 2024-09-11 12:23:20

试试这个:

foreach ($array as $k => $v) {
   $selected= ($b == $v) ? 'selected="selected"' : '';
   echo "<option value='$v' $selected>$k</option>\n";
}

Try this:

foreach ($array as $k => $v) {
   $selected= ($b == $v) ? 'selected="selected"' : '';
   echo "<option value='$v' $selected>$k</option>\n";
}
追星践月 2024-09-11 12:23:20

您必须捕获 $_GET 中的数据。
尝试

foreach ($_GET as $k => $v) {
        echo "<option value='".$v."'";
        if ($b == $v) {
            echo " selected ";
        }
        echo ">".$k."</option>";
    }

编辑:

的转储

        <form action='' method='get'>
    <input type='text' name='a' value='121'> of <select name='b'>
<option value='0.1'>option1</option>
<option value='0.15'>option2</option>
<option value='0.3'>option3</option>
<option value='3'>option4</option>
<option value='3'>option5</option><option value='16'>option6</option><option value='16'>option7</option><option value='16'>option8</option></select> <input type='submit' value='='> 363
    </form> 

在我的计算机上,它在这里工作是 GET The Url

http:// localhost/test.php?a=121&b=3

和参数

a 121
b 3

You have to catch the data in $_GET.
Try

foreach ($_GET as $k => $v) {
        echo "<option value='".$v."'";
        if ($b == $v) {
            echo " selected ";
        }
        echo ">".$k."</option>";
    }

EDIT:

On my Computer it worked here is the dump of GET

        <form action='' method='get'>
    <input type='text' name='a' value='121'> of <select name='b'>
<option value='0.1'>option1</option>
<option value='0.15'>option2</option>
<option value='0.3'>option3</option>
<option value='3'>option4</option>
<option value='3'>option5</option><option value='16'>option6</option><option value='16'>option7</option><option value='16'>option8</option></select> <input type='submit' value='='> 363
    </form> 

The Url

http://localhost/test.php?a=121&b=3

And parameters

a 121
b 3

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文