如何使用PHP和html修复表的显示
我有一个代码,它使用foreach循环和if语句在表中显示提取的数据,以检查字段是否为空,但问题是表在每一行上都显示字段名 如何修复显示器?如果我知道错误在foreach循环中,我将非常感谢任何帮助 在使用杜梅尼克的答案之后 代码:如何使用PHP和html修复表的显示,php,mysql,foreach,html-table,Php,Mysql,Foreach,Html Table,我有一个代码,它使用foreach循环和if语句在表中显示提取的数据,以检查字段是否为空,但问题是表在每一行上都显示字段名 如何修复显示器?如果我知道错误在foreach循环中,我将非常感谢任何帮助 在使用杜梅尼克的答案之后 代码: **已删除提供新信息的原因* 基本上,我将th的创建移到for each前面,否则您将创建此行的每个循环。所以这个将首先创建行,然后在每个数据行的内部创建。你可以看到在for each的正下方,我在循环的末尾打开并关闭它。一切结束后,我关上桌子 编辑3: 这是一个
**已删除提供新信息的原因* 基本上,我将th的创建移到for each前面,否则您将创建此行的每个循环。所以这个将首先创建行,然后在每个数据行的内部创建。你可以看到在for each的正下方,我在循环的末尾打开并关闭它。一切结束后,我关上桌子 编辑3: 这是一个解决你的问题的方法,因为我没有太多的时间,这是一个快速而肮脏的方法。改进它的潜力很大,但这将取决于您
echo "<table class='t1' width='30%'> ";
echo "<tr>";
echo "<th>Site ID</th>";
echo "<th>Site Name</th>";
//First we gonna check which columns contain at least 1 value
$latitude = false;
$longitude = false;
$ownerCONTACT = false;
$equipmentTYPE = false;
$height = false;
$subcontractorCONTACT = false;
$subcontractorCOMPANY = false;
foreach ($query_submit as $header_obj) {
//If only one cel in the column has an value we put his variable to true
if(!empty($header_obj->latitude)) {
$latitude = true;
}
if(!empty($header_obj->longitude)) {
$longitude = true;
}
if(!empty($header_obj->ownerCONTACT)) {
$ownerCONTACT = true;
}
if(!empty($header_obj->equipmentTYPE)) {
$equipmentTYPE = true;
}
if(!empty($header_obj->height)) {
$height = true;
}
if(!empty($header_obj->subcontractorCONTACT)) {
$subcontractorCONTACT = true;
}
if(!empty($header_obj->subcontractorCOMPANY)) {
$subcontractorCOMPANY = true;
}
}
//Now we check and if there is content we place the th
if($latitude === true) {
echo "<th>Lattitude</th>";
}
if($longitude === true){
echo "<th>Longitude</th>";
}
echo "<th>Owner Name</th>";
if($ownerCONTACT === true) {
echo "<th>Owner Contact</th>";
}
echo "<th>Company Name</th>";
if($equipmentTYPE === true){
echo "<th>Equipment Type</th>";
}
if($height === true) {
echo "<th>Height</th>";
}
if($subcontractorCONTACT === true){
echo "<th>Sub Contact</th>";
}
if($subcontractorCOMPANY === true) {
echo "<th>Sub company Name</th>";
}
echo "</tr>";
//Here we loop again trough the obj, we write now every row.
//If we had an empty column (so also no head) we will skip that value.
foreach ($query_submit as $obj) {
echo "<tr>";
echo "<td>".$obj->siteID."</td>";
echo "<td>".$obj->siteNAME."</td>";
if($latitude === true) {
//In this situation the column exists but we dont know if the specific cell has
// a value so we check this here
if(!empty($obj->latitude)) {
echo "<td>".$obj->latitude."</td>";
} else {
//If it doesnt has one we make at least an empty cell so the layout gets not broken
echo "<td></td>";
}
}
if($longitude === true){
if(!empty($obj->longitude)) {
echo "<td>".$obj->longitude."</td>";
} else {
echo "<td></td>";
}
}
echo "<td>".$obj->ownerNAME."</td>";
if($ownerCONTACT === true) {
if(!empty($obj->ownerCONTACT)) {
echo "<td>".$obj->ownerCONTACT."</td>";
} else {
echo '<td></td>';
}
}
echo "<td>".$obj->companyNAME."</td>";
if($equipmentTYPE === true){
if(!empty ($obj->equipmentTYPE)){
echo "<td>".$obj->equipmentTYPE."</td>";
} else {
echo '<td></td>';
}
}
if($height === true) {
if(!empty($obj->height)){
echo "<td>".$obj->height."</td>";
} else { echo '<td></td>'; }
}
if($subcontractorCONTACT === true){
if(!empty($obj->subcontractorCONTACT)){
echo "<td>".$obj->subcontractorCONTACT."</td>";
} else {
echo '<td></td>';
}
}
if($subcontractorCOMPANY === true) {
if(!empty($obj->subcontractorCOMPANY)){
echo "<td>".$obj->subcontractorCOMPANY."</td>";
} else {
echo '<td></td>';
}
}
echo "</tr>";
}
echo "</table>";
**已删除提供新信息的原因* 基本上,我将th的创建移到for each前面,否则您将创建此行的每个循环。所以这个将首先创建行,然后在每个数据行的内部创建。你可以看到在for each的正下方,我在循环的末尾打开并关闭它。一切结束后,我关上桌子 编辑3: 这是一个解决你的问题的方法,因为我没有太多的时间,这是一个快速而肮脏的方法。改进它的潜力很大,但这将取决于您
echo "<table class='t1' width='30%'> ";
echo "<tr>";
echo "<th>Site ID</th>";
echo "<th>Site Name</th>";
//First we gonna check which columns contain at least 1 value
$latitude = false;
$longitude = false;
$ownerCONTACT = false;
$equipmentTYPE = false;
$height = false;
$subcontractorCONTACT = false;
$subcontractorCOMPANY = false;
foreach ($query_submit as $header_obj) {
//If only one cel in the column has an value we put his variable to true
if(!empty($header_obj->latitude)) {
$latitude = true;
}
if(!empty($header_obj->longitude)) {
$longitude = true;
}
if(!empty($header_obj->ownerCONTACT)) {
$ownerCONTACT = true;
}
if(!empty($header_obj->equipmentTYPE)) {
$equipmentTYPE = true;
}
if(!empty($header_obj->height)) {
$height = true;
}
if(!empty($header_obj->subcontractorCONTACT)) {
$subcontractorCONTACT = true;
}
if(!empty($header_obj->subcontractorCOMPANY)) {
$subcontractorCOMPANY = true;
}
}
//Now we check and if there is content we place the th
if($latitude === true) {
echo "<th>Lattitude</th>";
}
if($longitude === true){
echo "<th>Longitude</th>";
}
echo "<th>Owner Name</th>";
if($ownerCONTACT === true) {
echo "<th>Owner Contact</th>";
}
echo "<th>Company Name</th>";
if($equipmentTYPE === true){
echo "<th>Equipment Type</th>";
}
if($height === true) {
echo "<th>Height</th>";
}
if($subcontractorCONTACT === true){
echo "<th>Sub Contact</th>";
}
if($subcontractorCOMPANY === true) {
echo "<th>Sub company Name</th>";
}
echo "</tr>";
//Here we loop again trough the obj, we write now every row.
//If we had an empty column (so also no head) we will skip that value.
foreach ($query_submit as $obj) {
echo "<tr>";
echo "<td>".$obj->siteID."</td>";
echo "<td>".$obj->siteNAME."</td>";
if($latitude === true) {
//In this situation the column exists but we dont know if the specific cell has
// a value so we check this here
if(!empty($obj->latitude)) {
echo "<td>".$obj->latitude."</td>";
} else {
//If it doesnt has one we make at least an empty cell so the layout gets not broken
echo "<td></td>";
}
}
if($longitude === true){
if(!empty($obj->longitude)) {
echo "<td>".$obj->longitude."</td>";
} else {
echo "<td></td>";
}
}
echo "<td>".$obj->ownerNAME."</td>";
if($ownerCONTACT === true) {
if(!empty($obj->ownerCONTACT)) {
echo "<td>".$obj->ownerCONTACT."</td>";
} else {
echo '<td></td>';
}
}
echo "<td>".$obj->companyNAME."</td>";
if($equipmentTYPE === true){
if(!empty ($obj->equipmentTYPE)){
echo "<td>".$obj->equipmentTYPE."</td>";
} else {
echo '<td></td>';
}
}
if($height === true) {
if(!empty($obj->height)){
echo "<td>".$obj->height."</td>";
} else { echo '<td></td>'; }
}
if($subcontractorCONTACT === true){
if(!empty($obj->subcontractorCONTACT)){
echo "<td>".$obj->subcontractorCONTACT."</td>";
} else {
echo '<td></td>';
}
}
if($subcontractorCOMPANY === true) {
if(!empty($obj->subcontractorCOMPANY)){
echo "<td>".$obj->subcontractorCOMPANY."</td>";
} else {
echo '<td></td>';
}
}
echo "</tr>";
}
echo "</table>";
看看这里,问题在于您在循环中迭代头,而它们只应该在表标记的开头调用一次。
也就是说,只有标记应该循环/迭代,并从for each中取出头标记。看看这里,问题在于您在循环中迭代头,而它们只应该在表标记的开头调用一次。
也就是说,只应循环/迭代标记,并从for each中取出标题标记。您的th标记取决于$obj,在循环中每次都可能不同。 因此,要克服这个问题,您可以做的是使表头保持静态,并且表行值将为空或值取决于$obj变量。 所以你需要搬出去
<table class='t1' width='30%'>
<tr>
<th>Site ID</th>
<th>Site Name</th>
// all th tags
</tr>
<?php foreach ($query_submit as $obj) { // then foreach
echo "<tr>";
echo "<td>".$obj->siteID."</td>";
echo "<td>".$obj->siteNAME."</td>";
?><td><?php if(!empty($obj->latitude))
{ echo $obj->latitude;} ?></tr> // your td will be fixed contain of td will depend on your value.
echo "<td>".$obj->siteID."</td>"; // and so on
这里是一个使用foreach循环的基本html表示例。
希望这对你有帮助
<table>
<tr>
<th>heading 1</th>
<th>heading 2</th>
<th>heading 3</th>
<th>heading 4</th>
<th>heading 5</th>
</tr>
<?php foreach($array as $data){ ?>
<tr>
<td><?php if(!empty($data->data1)) { echo $data->data1; } ?></td>
<td><?php if(!empty($data->data2)) { echo $data->data2; } ?></td>
<td><?php if(!empty($data->data3)) { echo $data->data3; } ?></td>
<td><?php if(!empty($data->data4)) { echo $data->data4; } ?></td>
<td><?php if(!empty($data->data5)) { echo $data->data5; } ?></td>
</tr>
<?php } ?>
您的th标记取决于$obj,它在循环中每次都可能不同。 因此,要克服这个问题,您可以做的是使表头保持静态,并且表行值将为空或值取决于$obj变量。 所以你需要搬出去
<table class='t1' width='30%'>
<tr>
<th>Site ID</th>
<th>Site Name</th>
// all th tags
</tr>
<?php foreach ($query_submit as $obj) { // then foreach
echo "<tr>";
echo "<td>".$obj->siteID."</td>";
echo "<td>".$obj->siteNAME."</td>";
?><td><?php if(!empty($obj->latitude))
{ echo $obj->latitude;} ?></tr> // your td will be fixed contain of td will depend on your value.
echo "<td>".$obj->siteID."</td>"; // and so on
这里是一个使用foreach循环的基本html表示例。
希望这对你有帮助
<table>
<tr>
<th>heading 1</th>
<th>heading 2</th>
<th>heading 3</th>
<th>heading 4</th>
<th>heading 5</th>
</tr>
<?php foreach($array as $data){ ?>
<tr>
<td><?php if(!empty($data->data1)) { echo $data->data1; } ?></td>
<td><?php if(!empty($data->data2)) { echo $data->data2; } ?></td>
<td><?php if(!empty($data->data3)) { echo $data->data3; } ?></td>
<td><?php if(!empty($data->data4)) { echo $data->data4; } ?></td>
<td><?php if(!empty($data->data5)) { echo $data->data5; } ?></td>
</tr>
<?php } ?>
我试过你的答案,结果不起作用,只是在表格中显示第一个结果,其余的结果都出来了是的,在看到你的图像后,我理解了你的问题,给我一点时间,我需要检查一下桌面是否没有价值,我不想显示。现在我又困惑了,你能告诉我们它应该是什么样子吗。你想每排有一个人头吗?仅在第一行或无标题?我只需要第一次显示字段名,如站点id,然后在其下显示所有选择的值,而不必每次显示字段名我尝试了你的答案,但不起作用,它只在表中显示第一个结果,其余的都显示出来。是的,在看到你的图像后,我理解了你的答案问题给我一点时间我需要检查桌面是否没有值我不想显示现在我又困惑了,你能告诉我们它应该是什么样子吗。你想每排有一个人头吗?仅在第一行或无标题?我只需要第一次显示字段名,如站点id,然后在其下显示所有选择的值,而不必每次显示字段名。我知道我正在迭代标题,但我需要它,因为不是所有标题都有值,所以我不想显示空标题。啊,好的,我请看,然后您必须调试并检查为什么那些if语句仍在添加头,可能值对您来说是空的,但实际上不是,例如,可能会返回一个空字符串,并且在if语句中会作为true传递,因为它的长度是1,我知道我在迭代报头,但我需要它,因为不是所有的报头都有值,所以我不想显示空的报头。好吧,我明白了,然后你必须调试并检查为什么那些if语句仍然在添加报头,也许这个值对你来说似乎是空的,但实际上不是,例如,可能会返回一个空字符串,该字符串在if语句中实际传递为true,因为它的长度为1