[英]output associative array from drop down menu in PHP
So I am trying to get the year to be echo'd or printed in the output and then the population. 因此,我试图在输出中然后在人口中回显或打印年份。 So far.
至今。 I am able to have it print out the population when the option is selected... However, when I try to have a foreach command in, it prints out nothing.
选择选项时,我可以将其打印出来。但是,当我尝试输入foreach命令时,它什么也不会打印出来。 What am I am assuming its because it cannot get the year1 into the variable.
我在假设什么,因为它无法将year1放入变量中。 What am I doing wrong in my code?
我的代码在做什么错?
Here is my code: 这是我的代码:
<?php
if (($_SERVER['REQUEST_METHOD'] == 'POST') && isset($_POST['year1'])&& isset ($_POST['year2']))
{
$year1 = $_POST['year1'];
$year2 = $_POST['year2'];
}
function get_years($select)
{
$years=array('1790'=>'3929214','1800'=>5236631,'1810'=>7239881,
'1820'=>9638453,'1830'=>12866020,'1840'=>17069453,
'1850'=>23161876,'1860'=>31443321,'1870'=>38558371,
'1880'=>49371340,'1890'=>62979766,'1900'=>76212168,
'1910'=>92228531,'1920'=>106021568,'1930'=>123202660,
'1940'=>132165129,'1950'=>151325798,'1960'=>179323175,
'1970'=>203211926,'1980'=>226545805,'1990'=>248709873,
'2000'=>281421906,'2010'=>308745538);
$options='';
while(list($k,$v)=each($years))
{
if($select==$v)
{
$options.='<option value="'.$v.'"selected>'.$k.'</option>';
}
else
{
$options.='<option value="'.$v.'">'.$k.'</option>';
}
}
return $options;
}
if(isset($_POST['years']))
{
$selected= $_POST['years'];
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Assignment 5 - kpete7</title>
</head>
<body>
<h1>US Census Population Change Calculator</h1>
<form action="<?= $_SERVER['PHP_SELF'] ?>" method="POST">
<h3>Year 1: <select name="year1" >
<?php echo get_years($selected); ?>
</select></h3>
<h3>Year 2: <select name="year2">
<?php echo get_years($selected); ?>
</select></h3>
<input type="submit" name="submit" id="submit" value="Calculate">
</form>
<?php
if (!empty($year1)) {
if (is_array($year1)) {
foreach ($year1 as $key_value => $key) {
echo $key ;
}
?>
</body>
</html>
Don't really understand how to write it here as text + keep codeblock intact. 不太了解如何在此处将其编写为文本+保持代码块完整。 :)
:)
Is there something similar for php just like jsfiddle? 是否有类似jsfiddle的php类似的东西?
Moved $years array out of function to use it later. 将$ years数组移出函数,以便以后使用。
There were couple variables not set while form was not posted. 没有发布表单时未设置几个变量。
Rewrote your function Used $year1 and $year2 to generate option list and selected values could be different to calcuate population. 重写您的函数使用$ year1和$ year2生成选项列表,并且所选值可能与计算总数不同。
Code indentation was scary. 代码缩进是可怕的。
$years=array(
1790 => 3929214,
1800 => 5236631,
1810 => 7239881,
1820 => 9638453,
1830 => 12866020,
1840 => 17069453,
1850 => 23161876,
1860 => 31443321,
1870 => 38558371,
1880 => 49371340,
1890 => 62979766,
1900 => 76212168,
1910 => 92228531,
1920 => 106021568,
1930 => 123202660,
1940 => 132165129,
1950 => 151325798,
1960 => 179323175,
1970 => 203211926,
1980 => 226545805,
1990 => 248709873,
2000 => 281421906,
2010 => 308745538,
);
if (($_SERVER['REQUEST_METHOD']=='POST') && isset($_POST['year1'], $_POST['year2'])) {
$year1 = (int) $_POST['year1'];
$year2 = (int) $_POST['year2'];
echo '<p>Difference of population between selected years: '.abs($year2 - $year1).'</p>';
}
else {
$year1 = $year2 = reset($years); //default selected
}
function get_years($selected) {
global $years;
$options='';
foreach($years as $k=>$v) {
$options .= '<option value="'.$v.'"'.($selected==$v ? ' selected="selected"' : '').'>'.$k.'</option>';
}
return $options;
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Assignment 5 - kpete7</title>
</head>
<body>
<h1>US Census Population Change Calculator</h1>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<h3>Year 1:
<select name="year1">
<?php echo get_years($year1); ?>
</select>
</h3>
<h3>Year 2:
<select name="year2">
<?php echo get_years($year2); ?>
</select>
</h3>
<input type="submit" name="submit" id="submit" value="Calculate">
</form>
</body>
</html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.