我试图显示从当年回到用户出生年份的所有年份。
不是在一行中显示所有年份,而是根据需要返回的年数多次打印用户的信息。
if($_POST['Name'] == NULL || $_POST['Year'] == NULL || $_POST['Address'] == NULL ){
printf("<br><div style='color:red'>Please enter the required fields</div><br>");
} else {
$name = $_POST['Name'];
$year = $_POST['Year'];
$address = $_POST['Address'];
$state = $_POST['State'];
$sex = $_POST['Sex'];
for ($nYear = $year; $nYear <= date('Y'); $nYear++) {
if($sex == "Male"){
printf("<div style='background-color:#A9D0F5'>"
."Name: $name<br>"
."Year: $nYear<br>"
."Address: $address<br>"
."State: $state<br>"
."Sex: $sex<br>"
."</div>");
}else if($sex == "Female"){
printf("<div style='background-color:#F5A9F2'>"
."Name: $name<br>"
."Age: $age<br>"
."Address: $address<br>"
."State: $state<br>"
."Sex: $sex<br>"
."</div>");
}
}
}
答案 0 :(得分:0)
尝试将float放在包含的div中。
答案 1 :(得分:0)
您正在为循环的每次迭代打印。如果您只想将所有行打印在一起,请在循环中连接一个字符串,并在循环结束后打印构建的字符串。
我在我的示例中使用GET而不是POST来进行简单测试,并相应地进行替换。
<?php
if(!isset($_GET['Name']) || !isset($_GET['Year']) || !isset($_GET['Address'])){
printf("<br><div style='color:red'>Please enter the required fields</div> <br>");
}
else {
$name = $_GET['Name'];
$year = $_GET['Year'];
$address = $_GET['Address'];
$state = $_GET['State'];
$sex = $_GET['Sex'];
$yearsText = '';
for ($nYear = $year; $nYear <= date('Y'); $nYear++) {
$yearsText .= $nYear . ' ';
}
$yearsText = trim($yearsText);
echo $yearsText;
}
?>
答案 2 :(得分:0)
请耐心等待。听起来你想在页面上有这样的输出:
name: john name: john name: john
year: 2014 year: 2015 year: 2016
... ... ...
但你得到的是这样的东西:
name: john
year: 2014
...
name: john
year: 2015
...
name: john
year: 2016
...
如果是这种情况,那么你有几个选择。在这两种情况下,您都需要使用CSS。选项一是使用css标签。
...
echo "<style>.info {
float: left;
padding: 10px;
background: #A9D0F5;
#some other stuff to make it pretty
} </style>";
for ($nYear = $year; $nYear <= date('Y'); $nYear++) {
if($sex == "Male"){
printf("<div class='info'>
...
或者您可以将其与其他样式内联:
...
printf("<div style='background-color:#A9D0F5; float: left; padding: 10px;'>"
...