Php while循环逐个检查值

Php while循环逐个检查值,php,foreach,while-loop,Php,Foreach,While Loop,不知道如何正确命名这个问题,所以如果需要,请随意编辑 我每月有12个盒子,每个盒子都有: 1-根据来自数据库的信息,显示“已售出”或“可用”之类的消息。 2-月份和年份的名称,如果月份早于当前月份,则年份将增加一个 来自数据库的信息有几个用|符号分隔的值,要检查的相关值是las值,$row['custom']的示例值是:93 | super | New Port | 2012年8月 我的问题是,我需要用每个框的状态更新每个框,并且使用当前脚本,只有数据库中的las条目用于更新框,因此,如果我知道

不知道如何正确命名这个问题,所以如果需要,请随意编辑

我每月有12个盒子,每个盒子都有: 1-根据来自数据库的信息,显示“已售出”或“可用”之类的消息。 2-月份和年份的名称,如果月份早于当前月份,则年份将增加一个

来自数据库的信息有几个用|符号分隔的值,要检查的相关值是las值,$row['custom']的示例值是:93 | super | New Port | 2012年8月

我的问题是,我需要用每个框的状态更新每个框,并且使用当前脚本,只有数据库中的las条目用于更新框,因此,如果我知道有两个或更多的框应该显示消息SALL,则只有最近的框才是更新的

如何修改以下脚本以逐个更新框? 问题是我查询数据库的方式还是其他什么

提前感谢您的帮助或建议

代码只适用于12个月中的2个月,以缩短时间

<?php
$month = date("m"); // Current month
$year = date("Y"); // Current year
$next = strtotime(date("d-m-Y", strtotime($now)) . "+1 year");
$nextyear = date("Y", $next); // Next year

// Check if this month is gone or not, if gone replace current year with next year
$january = "01";
$february = "02";

if ($month > $january) { 
  $jan = 'January - ' . $nextyear; } else { $jan = 'January - ' . $year;}

if ($month > $february) { 
  $feb = 'February - ' . $nextyear; } else { $feb = 'January - ' . $year;}

//Get info from Database

$query = "SELECT `custom` FROM `payments` WHERE `status` = 'Pending' OR `status` = 'Completed'"; 
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)) {
  $check_custom = explode(" | ", $row['custom']);
  $month_sold = $check_custom[3];
}

// Check if month is sold or not
if ($month_sold == $jan) { $jan_status = 'Sold';} else { $jan_status = 'Available';}
if ($month_sold == $feb) { $feb_status = 'Sold';} else { $feb_status = 'Available';}

//Output the months and their status
?>

<div class="month">
  <div class="mname"><?php echo $jan;?></div>
  <div class="<?php echo $jan_status;?>">
    <?php echo $jan_status;?>
  </div>
  <?php if($jan_status == 'Available') { 
      echo 'This month is ' . $jan_status . '.';
    } else { 
      echo 'This month has been ' . $jan_status . '.';} ?>
</div>

<div class="month">
  <div class="mname"><?php echo $feb;?></div>
  <div class="<?php echo $feb_status;?>">
    <?php echo $feb_status;?>
  </div>
  <?php if($feb_status == 'Available') { 
      echo 'This month is ' . $feb_status . '.';
    } else { 
      echo 'This month has been ' . $feb_status . '.';} ?>
</div>
每次运行$row=mysql\u fetch\u数组时,它都会用数据库中的下一行替换$row的内容。看看while循环:它将值分配给$check\u custom和$month\u salled,但在用新值覆盖它们之前不做任何处理。您需要在循环中移动所有用于解析数据库输出和生成给定月份信息的代码。然后输出或保存您希望在下一个月显示的信息


为了使代码更简单、更易于维护,您可以做很多事情。例如,我将为月份创建一个数组并对其进行迭代,而不是为每个月创建一组单独的变量和单独的输出代码。另外,您现在使用自定义列所做的是使用数据库表中的一个字段来存储另一个小表。这会产生很多问题-如果您将数据分成多个列,您只需查询相应的月份和年份即可

将状态检查移动到循环中,以便它对数据库中的每一行进行操作。否则,您只能引用mysql\u fetch\u数组最后一次调用返回的行。不过,为了可读性,我建议使用switch语句

while($row = mysql_fetch_array($result)) {
    $check_custom = explode(" | ", $row['custom']);
    $month_sold = $check_custom[3];

    // Check if month is sold or not
    if ($month_sold == $jan) { $jan_status = 'Sold';}
    else { $jan_status = 'Available';}

    if ($month_sold == $feb) { $feb_status = 'Sold';}
    else { $feb_status = 'Available';}
}

您将while的结束括号放错了位置,并将“$jan\u status='Available”从while循环内部移动到它的正上方;这是修改后的代码

    <?php
    $month = date("m"); // Current month
    $year = date("Y"); // Current year
    $next = strtotime(date("d-m-Y", strtotime($now)) . "+1 year");
    $nextyear = date("Y", $next); // Next year

    // Check if this month is gone or not, if gone replace current year with next year
    $january = "01";
    $february = "02";

    if ($month > $january) { 
      $jan = 'January - ' . $nextyear; } else { $jan = 'January - ' . $year;}

    if ($month > $february) { 
      $feb = 'February - ' . $nextyear; } else { $feb = 'January - ' . $year;}

    //Get info from Database

    $query = "SELECT `custom` FROM `payments` WHERE `status` = 'Pending' OR `status` = 'Completed'"; 
    $result = mysql_query($query) or die(mysql_error());
$jan_status = 'Available';
$feb_status = 'Available';
    while($row = mysql_fetch_array($result)) {
      $check_custom = explode(" | ", $row['custom']);
      $month_sold = $check_custom[3];


    // Check if month is sold or not
    if ($month_sold == $jan) { $jan_status = 'Sold';} 
    if ($month_sold == $feb) { $feb_status = 'Sold';}
    }//th closing bracket should be here;
    //Output the months and their status
    ?>

    <div class="month">
      <div class="mname"><?php echo $jan;?></div>
      <div class="<?php echo $jan_status;?>">
        <?php echo $jan_status;?>
      </div>
      <?php if($jan_status == 'Available') { 
          echo 'This month is ' . $jan_status . '.';
        } else { 
          echo 'This month has been ' . $jan_status . '.';} ?>
    </div>

    <div class="month">
      <div class="mname"><?php echo $feb;?></div>
      <div class="<?php echo $feb_status;?>">
        <?php echo $feb_status;?>
      </div>
      <?php if($feb_status == 'Available') { 
          echo 'This month is ' . $feb_status . '.';
        } else { 
          echo 'This month has been ' . $feb_status . '.';} ?>
    </div>

丁丁的回答是正确的。我误读了您的代码,并认为您正在对其他自定义字段进行操作。如果每个月需要设置的唯一信息是一个状态变量,那么只需将用于设置这些变量的代码移动到while循环中即可。是的!,感谢您的帮助,+1感谢您抽出时间,我才刚刚开始,所以到目前为止我是如何制作脚本的,但我接受了您的建议。@user983248很高兴这很有帮助!我的建议可能比我想的更突然——我本想在第二段后面加上一些代码来帮助你实现你想要的东西,但是现实生活闯入了我,我不得不逃跑。