php mysql打印月份和表格内容

I need to make a list that prints the months and if they're paid or not.

So first I make an array with month names:

$month_names = array("January","February","March","April","May","June","July","August","September","October","November","December");

Then I have columns for the twelve months: values 1 for paid 0 for non-paid. So I made a variable to know if it's paid or not:

if ($row['month1'] == '1') {
    $row['month1'] = 'Paid';
} else $row['month1'] = 'Non-Paid';

Is there a better way to do that?

The list will have only two columns: "monthly payments" and "status".

And the while or foreach should be something like:

echo '
    <li><a class="months">'$array[$month_names]=>$value'</a></li>
    <li><a class="status">'.$row['month1'].'</a></li>
    ';

I'm not really sure, how can I do this?

Thank YOU!

You are on the right track keeping your logic separate from the presentation. That is a good thing. For your problem, you can check all of your month values and store the results in another array (let's call it $paid), indexed by the number of the month. Then you use your $monthNames array to map to the corresponding value in $paid. That will set you up to loop through the $monthNames array and create your HTML.

The following will do what you need:

<?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>
");
foreach($monthNames as $key => $month) {
    print('<li><a class="months">' . $month . '</a></li>' . "
");
    print('<li><a class="status">' . $paid[$key] .'</a></li>' . "
");
}
print("</ul>");

?>

The output will be along the lines of (depending on the DB data):

<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>

Hope that help!

If I understood you right you have an array like this:

$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),
);

In this case you can do something like this

<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>

That would be ideal, but I noticed that your example array has a month1 key, so if your array has this structure: month[0-9] = paid-status:

$rows = array(
    'month1' => 0,
    'month2' => 1,
    'month4' => 0,
    'month5' => 1,
    'month7' => 1,
    'month8' => 0,
);

Then you should do something like this:

<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>