首页 > 解决方案 > 将循环输出存储在变量中

问题描述

我有这个 PHP,它输出一个经理列表,下面是他们的员工和请求。

这现在很好用 - 但是,我想将所有 HTML 存储在每个经理的变量中,而不是在页面上输出。我如何实现这一目标?

<?php 

while($row = $resultt->fetch_assoc())  {
    $arr = explode("@", $row['email'], 2);
    $first = $arr[0];
    $first = str_replace('.','-',$first);
    $email = $row['email'];
    $sql2 = "SELECT * FROM user WHERE manager_email LIKE '%".$email."%'";
    $resultt2 = $con->query($sql2);

    echo '<table class="'.$first.'">';

    $i++;

    while($row2 = $resultt2->fetch_assoc())  {

        echo '<tr><td>'.$row2['email'].':</td>';
        $sql3 = "SELECT * FROM requests WHERE submitted_by = '".$row2['email']."'";
        $resultt3 = $con->query($sql3);
        $count = 0;
        while($row3 = $resultt3->fetch_assoc())  {
            $count++;               
        }
        echo '<td>'.$count.'</td></tr>';
    }
    echo '</table>';
}

标签: php

解决方案


$output = '';
while($row = $resultt->fetch_assoc())  {
    $arr = explode("@", $row['email'], 2);
    $first = $arr[0];
    $first = str_replace('.','-',$first);
    $email = $row['email'];
    $sql2 = "SELECT * FROM user WHERE manager_email LIKE '%".$email."%'";
    $resultt2 = $con->query($sql2);

    $output .=  '<table class="'.$first.'">';

    $i++;

    while($row2 = $resultt2->fetch_assoc())  {

        $output .=  '<tr><td>'.$row2['email'].':</td>';
        $sql3 = "SELECT * FROM requests WHERE submitted_by = '".$row2['email']."'";
        $resultt3 = $con->query($sql3);
        $count = 0;
        while($row3 = $resultt3->fetch_assoc())  {
            $count++;               
        }
        $output .=  '<td>'.$count.'</td></tr>';
    }
    $output .=  '</table>';
}

echo $output;

现在所有输出都存储在$output中


推荐阅读