dongmin1166 2018-09-21 17:53
浏览 57

PHP以表格格式和错误值循环数组数据

I have the following code. How can I get my results ($x) for the foreach function to print into the table at the top, but in columns instead of a straight horizontal row? Is there a way to do this without just inserting each individual value into the HTML table? I need to do the same for my $employee['name'] variable but am not sure how I could get these values inserted into a table format without going one by one and entering the value myself.

Also, one value for $x at the end stays an integer and does not echo the string variable specified by the foreach function, is there a way I could fix this?

 <!--4.3--> 
     <table>
        <tr>
            <td>Employee name</td><td></td><td></td><td></td><td></td><td></td>
            <td>Type of Paying</td><td></td><td></td><td></td><td></td><td></td>
        </tr>
        <tr>
            <td></td><td></td><td></td><td></td><td></td><td></td>
            <td><?php echo $x;?></td><td></td><td></td><td></td><td></td><td></td>
        </tr> 
     </table>
</html>

<?php
foreach ($employees as $employee) {
    $x = ($employee['wage'] * $employee['hrs']) * 4;
    if ( 3000 <= $x ) {
        echo "High paying";
    } elseif (2000 <= $x && $x <= 2999) {
        echo "Good paying";
    } else {
        echo "Low paying";
    }
}
print_r ($x); 
  • 写回答

1条回答 默认 最新

  • dosin84644 2018-09-21 18:21
    关注

    I inserted the foreach function into the table where I needed the results printed, then added
    tags within the ifelse statement after each parameter, and it worked like a charm.

    My code for that table now looks like this:

    <html>
     <table>
        <tr>
            <td>Employee name</td><td></td><td></td><td></td><td></td><td></td>
            <td>Type of Paying</td><td></td><td></td><td></td><td></td><td></td>
        </tr>
        <tr>
            <td></td><td></td><td></td><td></td><td></td><td></td>
            <td> <?php
    foreach ($employees as $employee) {
        $x = ($employee['wage'] * $employee['hrs']) * 4;
        if ( 3000 <= $x ) {
            echo "High paying"; echo "<br/>";
        } elseif (2000 <= $x && $x <= 2999) {
            echo "Good paying"; echo "<br/>";
        } else {
            echo "Low paying"; echo "<br/>"; 
        }
    }  echo "<br/>" ; ?>
    </td><td></td><td></td><td></td><td></td><td></td>
        </tr> 
     </table>
    

    The other section above the foreach function will hold employee names and is not yet filled out to match.

    评论

报告相同问题?

悬赏问题

  • ¥60 pb数据库修改或者求完整pb库存系统,需为pb自带数据库
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路