0

不确定如何正确命名这个问题,所以如果需要,请随时编辑。

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

来自数据库的信息有几个由 | 分隔的值。符号,要检查的相关符号是 las 符号,$row['custom'] 的示例值为: 93 | 晚餐 | 新港 | 8 月 - 2012 年

我的问题是我需要用它们的“状态”更新每个框,并且使用当前脚本只有数据库上的 las 条目用于更新框,所以如果我知道有两个或多个框应该显示消息“已售出”,只有最新的是更新的。

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

提前感谢您的任何帮助或建议。

仅为 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>
4

3 回答 3

4

您放错了 while 的右括号并将 `$jan_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>
于 2012-05-08T18:39:42.147 回答
2

每次运行时$row = mysql_fetch_array(),它都会将 $row 的内容替换为数据库中的下一行。查看 while 循环:它将值分配给 $check_custom 和 $month_sold,但在用新值覆盖它们之前不对它们做任何事情。您需要移动所有代码以解析数据库输出并在循环内生成给定月份的信息。然后输出或保存该月要显示的信息,然后再继续下一个。

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

于 2012-05-08T18:38:14.817 回答
1

将您的状态检查移到循环中,以便它对数据库中的每一行进行操作。否则,您只会引用最后一次调用返回的行mysql_fetch_array。不过,我建议使用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';}
}
于 2012-05-08T18:39:17.957 回答