dtp0760 2013-05-29 10:10
浏览 45
已采纳

生成foreach html输出变量

I'm having a problem trying to work out the best way of doing this. I'm creating a template/review system which allows users to fill in a template and then search on reviews done. Each review is marked on multiple sections which I need to display.

My problem is that I need to list each separate review and it's section in the 'slidingDiv'. Currently this overwrites the $htm variable so each review & it's respective sections is exactly the same when displayed (each done using a different template therefore should be different). I tried using arrays but these were being overwritten. Visually the idea is to have a table which displays each Review and then when they select the Review the 'slidingDiv' shows the Sections of that Review below the Review table.

I realise I'm quite far off the solution but this was the closest I've been getting. Currently I have the below:

    if (isset($reviews)) {
    include ("templates/default/pheader.php");
    echo "<p>Search results displayed below:</p>";
    echo "<table id=table-search>";
    echo "<tr>";
    echo "<th>ECR</th><th>Consultant</th><th>Reviewer</th><th>Template</th><th>Date</th>";
    echo "</tr>";

    foreach ($reviews as $review) {
        $ecr = $review->get_ecr();
        $member = $review->get_team_member();
        $user = $review->get_user();
        $user_name = $user->get_name();
        $template = $review->get_template();
        $template_name = $template->get_name();
        $member_name = $member->get_name();
        $timestamp = $review->get_timestamp();

        echo "<tr>";
        echo "<td>";
        echo "<a href=\"#\" class=\"show_hide\">$ecr</a>";
        echo "</td>";
        echo "<td>";
        echo "$member_name";
        echo "</td>";
        echo "<td>";
        echo "$user_name";
        echo "</td>";
        echo "<td>";
        echo "$template_name";
        echo "</td>";
        echo "<td>";
        echo "$timestamp";
        echo "</td>";
        echo "</tr>";

        foreach ($review->get_sections() as $section) {
            $section_name = $section->get_name();
            $section_total = $section->get_total();
            $section_mark = $section->get_mark();
            $section_pass = $section->get_pass();
            if ($section_pass == 1) {
                $section_pass = "<font color = \"green\">PASS</font>";
            } else {
                $section_pass = "<font color = \"red\">FAIL</font>";
            }
            $htm .= "<tr><td>$section_name</td>";
            $htm .= "<td>$section_total / $section_mark marks</td>";
            $htm .= "<td>$section_pass</td></tr>";
        }
    }
    echo "</table>";
    echo "<div class=\"slidingDiv\">";
    echo "<table>";
    //List each section per Review
    echo "</table>";
    echo "<p><a href=\"#\" class=\"show_hide\">Hide</a></div></p>";
    echo "<p><a href=\"search.php\">Search Again</a></p>";
}
  • 写回答

1条回答 默认 最新

  • dpgui8229808 2013-05-29 14:18
    关注

    Looking at the code, I'd be tempted to create just the html first with sample content to get the feel of the page and the final result you're aiming for. Then, once you're happy, add the php to the first foreach block and test you're getting the right information using var_dumps or print_r commands.

    Once you're happy, move on to the second foreach block, testing as you go.

    Trying to do the code and html combined in this way will always be a bit daunting, working in bite size chunks like this enables you to be more confident of the end result.

    I hope this helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥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