0

我目前有一个带有行星的 multidim 数组,它是相对重力值。([0][0] = 行星和 [0][1] = 相对重力)。

在我的页面上,我有一个表单,用户可以在其中提交物品的重量以及他们希望看到它的重量所在的星球。我想要做的是把用户输入的重量乘以他们选择的特定行星的相对重力。

我最初的尝试是使用这样的东西:

<?php
$file = fopen("PLANET.txt", "r");
$planets = array();

while(!feof($file)) {
    $line = fgets($file);
    $value = explode(" ", $line);
    array_push($planets, $value);
}



if(isset($_POST['weight']) && isset($_POST['planet'])) {
    while($x=0;x<count($planets);x++) {
        if($_POST['planet'] == $planets[x][0]) {
            $final_weight = $_POST['weight'] * $planets[x][1];
        }
    }
}

?>

但是它似乎不起作用......我是 PHP 的新手,我可能会犯一个愚蠢的错误,但我似乎找不到它,任何帮助将不胜感激。

编辑:

好的,这就是我现在所拥有的,它似乎正在工作,非常感谢你们。犯了这些错误感到很愚蠢!

for($x=0;$x<count($planets);$x++) {
        if($_POST['planet'] == $planets[$x][0]) {
            $final_weight = $_POST['weight'] * $planets[$x][1];
        }
    }
4

1 回答 1

0

您的 while 循环应如下所示(注意附加$符号):

while($x=0;$x<count($planets);x++) {
    if($_POST['planet'] == $planets[$x][0]) {
        $final_weight = $_POST['weight'] * $planets[$x][1];
    }
}

编辑:以上完全是胡说八道。不知道我是怎么回事。当然应该是:

for($x=0;$x<count($planets);x++) {
    if($_POST['planet'] == $planets[$x][0]) {
        $final_weight = $_POST['weight'] * $planets[$x][1];
    }
}
于 2013-09-29T16:37:51.703 回答