duan62819774 2017-02-24 01:31
浏览 6
已采纳

我需要计算从下拉列表PHP中选择的两年之间的人口差异?

So I have 2 drop down lists, I need to chose a year from them. Then I need to show the chosen year and the number of population for that year. For example:

For year 1800: the population is : 3,929,214
For year 1900: the population is : 76,212,168
Population increased by: 72,282,954.

So here is my code until now:

<html>
    <head>
        <meta charset="UTF-8">
        <title>Calculator</title>
    </head>
    <body>
        <h1>Population Change Calculator</h1>
        <?php
        $population=[3929214,5236631,7239881,9638453,12866020,17069453,23191876,31443321];
        $year=(range(1790, 1860, 10));
        $array= array_combine($year, $population);
        ?>
        <form method="post">
            <p><label for="year1">Year 1:</label>
                <select name="year1">
                    <option value=""></option>
                    <?php
                        foreach ($array as $year1=>$population){ ?>
                    <option value="<?php echo $year1; ?>"><?php echo $year1; ?></option>       
                    <?php    }
                    ?>
                </select>
            </p>
            <p>
                <label for="year2">Year 2:</label>
                <select name="year2">
                    <option value=""></option>
                    <?php
                        foreach ($array as $year2=>$population){ ?>
                    <option value="<?php echo $year2; ?>"><?php echo $year2; ?></option>       
                    <?php    }
                    ?>
                </select>
            </p>
            <input type="submit" name="submit" value="Submit">
            <br>

        </form>
        <?php
        $ini_set = ini_set('display_errors', 1);
    error_reporting(E_ALL);

        $empty=true;
        if(isset($_POST['submit'])){
            if(empty($_POST['year1'])){
                $empty=FALSE;
                print "<p>Please choose Year 1.</p>";
            }

            if(empty($_POST['year2'])){
                $empty=false;
                print "<p>Please choose Year 2.</p>";
            } 
}

I cant figure out how to print the chosen year and the population for that year.
I am new to PHP, thank you in advance.

  • 写回答

1条回答 默认 最新

  • dongpangzan6425 2017-02-24 03:00
    关注

    It seems weird that you are setting the value of your 'year' options to the population. Perhaps this is a mistake..?

    You should change them to use the year:

    <option value="<?php echo $year1; ?>"><?php echo $year1; ?></option>
    

    Once you do that, you can print your desired values by doing something like this:

    if (isset($_POST['submit'])) {
        if (empty($_POST['year1'])) {
            $empty = FALSE;
            print "<p>Please choose Year 1.</p>";
        } else {
            print "<h1>Year 1: {$_POST['year1']}</h1>";
            print "<p>Population: {$array[$_POST['year1']]}</p>";
        }
    
        if (empty($_POST['year2'])) {
            $empty = false;
            print "<p>Please choose Year 2.</p>";
        } else {
            print "<h1>Year 2: {$_POST['year2']}</h1>";
            print "<p>Population: {$array[$_POST['year2']]}</p>";
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用S7-200 SIM 2.0,导入程序时出现了这个问题(关键词-子程序)
  • ¥15 pip安装pystan显示No module named 'Cython'的问题
  • ¥15 Fortran代码segment fault
  • ¥15 求一下解题思路,需完整的步骤和代码。
  • ¥15 苹果CMS搭建的视频网站首页打开为什么会自动跳转到色情页面?
  • ¥15 php,mysql的问题
  • ¥50 dac adc的检定规程
  • ¥20 MIT控制器能控制不稳定系统吗
  • ¥15 公司代码X对业务伙伴X无效,处理方法?
  • ¥15 微信内链接跳转到浏览器打开怎么实现