dtp0760 2013-05-29 02: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 06: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.

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

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部