不确定如何正确命名此问题,因此如果需要,可随意编辑。
我每月有12个盒子,每个盒子显示: 1 - “已售出”或“可用”之类的消息,具体取决于来自数据库的信息。 2 - 月份和年份的名称,如果月份大于当前月份,则年份将增加1。
来自数据库的信息有一些以|分隔的值符号,要检查的相关的是las,$ row ['custom']的示例值是:93 |晚餐|新港| 2012年8月
我的问题是我需要用它们的“状态”更新每个盒子,并且使用当前脚本只使用数据库上的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>
答案 0 :(得分: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>
答案 1 :(得分:2)
每次运行$row = mysql_fetch_array()
时,它都会将$ row的内容替换为数据库中的下一行。查看while循环:它将值分配给$ check_custom和$ month_sold,但在用新值覆盖它们之前不对它们执行任何操作。您需要移动所有代码以解析数据库输出并生成给定月份内部循环的信息。然后输出或保存您想要显示的月份信息,然后再转到下一个月。
您可以做很多事情来使您的代码更简单,更易于维护。例如,我会为这几个月创建一个数组并迭代它,而不是为每个月创建一组单独的变量和单独的输出代码。此外,您现在正在使用自定义列进行的操作是使用数据库表中的字段来存储另一个小表。这会产生很多问题 - 如果您将数据分成多个列,您可以只查询相应的月份和年份。
答案 2 :(得分: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';}
}