douweng1904 2014-10-01 16:19
浏览 39
已采纳

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!

  • 写回答

2条回答 默认 最新

  • drol55885602 2014-10-01 16:55
    关注

    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!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染