0

我想让以下代码成为一个 for 循环,以使所有内容都更好地阅读,但我似乎无法正确引用并最终得到一个空白页

if ($_POST['week'])
{
  $week = $_POST['week'];
}
//or check for a value submitted by the week menu
elseif ($_POST["user_week1"] == "week1") {
  $week = "1";
}
elseif ($_POST["user_week2"] == "week2") {
  $week = "2";
}
else if ($_POST["user_week3"] == "week3") {
  $week = "3";
}
else if ($_POST["user_week4"] == "week4") {
  $week = "4";
}
else if ($_POST["user_week5"] == "week5") {
  $week = "5";
}
else if ($_POST["user_week6"] == "week6") {
  $week = "6";
}
else {
  $week = "1";
}

我试着做:

if ($_POST['week'])
{
  $week = $_POST['week'];
}
for ($i = 1; $i<7; $i++)
{
  else if ($_POST["user_week'.$i.'"] == "week'.$i.'") {
    $week = $i;
}
}
else {
  $week = "1";
}

但这效果不太好,我尝试在变量周围使用双引号而不是单引号,加上 '" 和 "' 无济于事。

任何人都可以帮助解决这个问题,或者为我指出一个关于变量单引号和双引号的好资源?

4

2 回答 2

1

你们都忽略了else if不能存在的事实吗!?

你应该做的是以下

$week = "1";
if ($_POST['week']) {
  $week = $_POST['week'];
} else {
  for ($i = 1; $i < 7; $i++) {
    if ($_POST["user_week" . $i] == "week" . $i) {
      $week = $i;
      break;
    }
  }
}
于 2013-07-03T00:26:18.210 回答
-1
  elseif ($_POST['user_week' . $i] == 'week' . $i) 

以上更正应该有效

于 2013-07-03T00:19:37.143 回答