我需要制作一份列表来打印月份以及是否已付款。
首先我创建一个包含月份名称的数组:
$month_names = array("January","February","March","April","May","June","July","August","September","October","November","December");
然后我列出了十二个月的列:非付费的付费0的值为1。所以我做了一个变量来知道它是否有偿:
if ($row['month1'] == '1') {
$row['month1'] = 'Paid';
} else $row['month1'] = 'Non-Paid';
有更好的方法吗?
该列表只有两列:"每月付款"和"状态"。
而while或foreach应该是这样的:
echo '
<li><a class="months">'$array[$month_names]=>$value'</a></li>
<li><a class="status">'.$row['month1'].'</a></li>
';
我不确定,我怎么能这样做?
谢谢你!
答案 0 :(得分:2)
如果我理解你,你就有这样的数组:
$rows = array(
array('month' => 1, 'paid' => 0),
array('month' => 2, 'paid' => 1),
array('month' => 4, 'paid' => 0),
array('month' => 5, 'paid' => 1),
array('month' => 7, 'paid' => 1),
array('month' => 8, 'paid' => 0),
);
在这种情况下,你可以做这样的事情
<ul class="rows">
<?php foreach ($rows as $row): ?>
<li><a href="#" class="month"><?php echo $month_names[$row['month']] ?></a></li>
<li><a href="#" class="status"><?php echo $row['paid'] ? 'Paid' : 'Non-Paid' ?></a></li>
<?php endforeach ?>
</ul>
这是理想的,但我注意到你的示例数组有一个month1
键,所以如果你的数组有这个结构:month[0-9] = paid-status
:
$rows = array(
'month1' => 0,
'month2' => 1,
'month4' => 0,
'month5' => 1,
'month7' => 1,
'month8' => 0,
);
然后你应该做这样的事情:
<ul class="rows">
<?php foreach ($rows as $month => $status): ?>
<!-- We replace all non-numeric caracters in the key of the element and then sustract one to match the zero base index -->
<li><a href="#" class="month"><?php echo $month_names[ preg_replace('/[^0-9]/', '', $month) - 1 ] ?></a></li>
<li><a href="#" class="status"><?php echo $status ? 'Paid' : 'Non-Paid' ?></a></li>
<?php endforeach ?>
</ul>
答案 1 :(得分:1)
您走在正确的轨道上,使您的逻辑与演示文稿分开。这是一件好事。对于您的问题,您可以检查所有月份值并将结果存储在另一个数组中(让我们称之为$paid
),并按月份编号索引。然后,您使用$monthNames
数组映射到$paid
中的相应值。这将使您循环遍历$monthNames
数组并创建HTML。
以下将满足您的需求:
<?php
$monthNames = array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November","December");
$paid = array(); //Placeholder for paid months.
$row = $result->fetch_array(); //Assuming MySQLi API (you are using that or PDO...I hope...)
for($i = 1; $i < 13; $i++) {
$month = "month" . $i;
if($row[$month] == 1) {
$paid[] = "Paid";
} else {
$paid[] = "Not Paid";
}
}
//Now make the HTML list
print("<ul>\n");
foreach($monthNames as $key => $month) {
print('<li><a class="months">' . $month . '</a></li>' . "\n");
print('<li><a class="status">' . $paid[$key] .'</a></li>' . "\n");
}
print("</ul>");
?>
输出将沿着(取决于DB数据):
<ul>
<li><a class="months">January</a></li>
<li><a class="status">Paid</a></li>
<li><a class="months">February</a></li>
<li><a class="status">Paid</a></li>
<li><a class="months">March</a></li>
<li><a class="status">Not Paid</a></li>
<li><a class="months">April</a></li>
<li><a class="status">Paid</a></li>
<li><a class="months">May</a></li>
<li><a class="status">Paid</a></li>
<li><a class="months">June</a></li>
<li><a class="status">Paid</a></li>
<li><a class="months">July</a></li>
<li><a class="status">Paid</a></li>
<li><a class="months">August</a></li>
<li><a class="status">Not Paid</a></li>
<li><a class="months">September</a></li>
<li><a class="status">Paid</a></li>
<li><a class="months">October</a></li>
<li><a class="status">Paid</a></li>
<li><a class="months">November</a></li>
<li><a class="status">Not Paid</a></li>
<li><a class="months">December</a></li>
<li><a class="status">Paid</a></li>
</ul>
希望有所帮助!