php - 将循环输出存储在变量中
问题描述
我有这个 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>';
}
解决方案
$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中
推荐阅读
- excel - 复制一个值并粘贴相同的值,直到它到达特定的单元格
- c# - 如何使用 MSBuild 构建项目?
- sql - 多行问题
- ansible - Ansible 错误:对 smtp.office365.com:587 的身份验证失败,请检查您的用户名和/或密码
- php - PHP中的断线未显示
- javascript - 如何将文件上传到 Firebase 存储?
- nuxt.js - 如何使用外部 css 在 nuxtJS 中创建 amp 页面
- react-native - 如何在 React Native Expo 中将 API 响应数据加载到 DropDown 中?
- angular - 用于 Angular 6 项目的 gitlab-ci 文件
- reactjs - 无法让 NewsApi 数据显示在 React 中