表可以在php循环中运行吗?

时间:2015-04-14 16:19:13

标签: php html

我正在尝试创建一个不断通过查询从数据库中提取数据的表,但是当我把它放在我需要不断获取数据的while循环中时,我无法显示任何表格。我只是对我的代码做错了或者这不可能吗?我将发布css和html 编辑2:这是变量的计算,如$ diff

$str5npos = ($str5npos /100);
$str5npos = $str5npos +12.015;
$str5nposgraph = (($str5npos/40) *100);

$str5spos = ($str5spos /100);
$str5spos = $str5spos +12.015;
$str5sposgraph = (($str5spos/40) *100);
$total5= round($str5npos + $str5spos); 

$diff5 = round($str5npos - $str5spos);
$direction5 = 'North';
if ($diff5 < 0) {
    $direction5 = 'South';
} 

编辑:这是我的问题

$result = mysql_query("SELECT * FROM p4updates WHERE DB4PrimeKey = 4");

// Fetch live values from data base
while($row = mysql_fetch_array($result))
    {
    $str1npos = $row['str1npos'];$str1spos = $row['str1spos'];$str2npos = $row['str2npos'];$str2spos = $row['str2spos'];$str3npos = $row['str3npos'];
    $str3spos = $row['str3spos'];$str4npos = $row['str4npos'];$str4spos = $row['str4spos']; $str5npos = $row['str5npos'];
    $str5spos = $row['str5spos'];$str6npos = $row['str6npos'];$str6spos = $row['str6spos'];$str7npos = $row['str7npos'];
    $str7spos = $row['str7spos'];$wrnpos = $row['wrnpos'];$wrspos = $row['wrspos'];

    $strout1 = $row['strout1'];$strout2 = $row['strout2'];$strout3 = $row['strout3'];$strout4 = $row['strout4'];$strout5 = $row['strout5'];
    $strout6 = $row['strout6'];$strout7 = $row['strout7'];$wrout4 = $row['wrout4'];

    $strfbk1 = $row['strfbk1'];$strfbk2 = $row['strfbk2'];$strfbk3 = $row['strfbk3'];$strfbk4 = $row['strfbk4'];$strfbk5 = $row['strfbk5'];
    $strfbk6 = $row['strfbk6'];$strfbk7 = $row['strfbk7'];$wrfbk3 = $row['wrfbk3'];

    $stralm1 = $row['stralm1'];$stralm2 = $row['stralm2'];$stralm3 = $row['stralm3'];$stralm4 = $row['stralm4'];$stralm5 = $row['stralm5'];


    }

CSS

.MainTable { float: left;width:100%;  background-color:#CEF7CE;border-collapse:collapse;  font-size: 20px; }
.MainTable th { background-color:#197D19;color:white; font-size: 15px; }
.MainTable td, .MainTable th {  padding:1px;border:1px solid #000; font-size: 20px; }

HTML     

  <col width="10%">
  <col width="9%">
  <col width="9%">
  <col width="9%">
  <col width="9%">
  <col width="9%">
  <col width="9%">
  <col width="9%">
  <col width="9%">


<tr>    


<?php
while($row = mysql_fetch_array($result))
{   
// Row one Entrys 

echo "<td height ='54' align='center'>" . $diff1= row['diff1'].''. $direction1 = row['direction1']. "</td>" ;
echo "<td height ='54' align='center'>" . $diff2 = row['diff2'] .''.$direction2 = row['direction2']. "</td>";
echo "<td height ='54' align='center'>" . $diff3= row['diff3'] .''.$direction3= row['direction3'] . "</td>"; 
echo "<td height ='54' align='center'>" . $wrdiff= row['wrdiff'] .''.$wrdirection= row['wrdirection'] . "</td>"; 
echo "<td height ='54' align='center'>" . $diff4= row['diff4'].''. $direction4= row['direction4'] . "</td>";
echo "<td height ='54' align='center'>" . $diff5= row['diff5'] .''.$direction5= row['direction5']  ."</td>"; 
echo "<td height ='54' align='center'>" . $diff6= row['diff6'] .''.$direction6= row['direction6'] ."</td>"; 
echo "<td height ='54' align='center'>" . $diff7= row['diff7'].''. $direction7= row['direction7'] .  "</td>"; 
}
?>
</tr>   
<!-- Row two Entrys --> 
<tr> <?php
while($row = mysql_fetch_array($result))
{   

echo "<td height ='54' align='center'>" .$strout1= row['strout1'] .'%'."</td>";
echo "<td height ='54' align='center'>" . $strout2= row['strout2']. '%'."</td>";
echo "<td height ='54' align='center'>"  .$strout3= row['strout3'] .'%'."</td>";
echo "<td height ='54' align='center'>" . $wrout4= row['wrout4']. '%'."</td>";
echo "<td height ='54' align='center'>" .$strout4= row['strout4'] .'%'."</td>";
echo "<td height ='54' align='center'>" . $strout5= row['strout5'] .'%'."</td>";
echo "<td height ='54' align='center'>"  .$strout6= row['strout6']. '%'."</td>";
echo "<td height ='54' align='center'>"  .$strout7= row['strout7'] .'%'."</td>";
} ?>
</tr>
<!-- Row three Entrys -->
<tr><?php
while($row = mysql_fetch_array($result))
{   
echo "<td height ='54' align='center'>". $strfbk1= row['strfbk1']. '%'."</td>";
echo "<td height ='54' align='center'>" . $strfbk2= row['strfbk2']. '%'."</td>";
echo "<td height ='54' align='center'>" . $strfbk3= row['strfbk3']. '%'."</td>";
echo "<td height ='54' align='center'>" . $wrfbk3= row['wrfbk3']. '%'."</td>";
echo "<td height ='54' align='center'>". $strfbk4= row['strfbk4'] .'%'."</td>";
echo "<td height ='54' align='center'>" . $strfbk5= row['strfbk5']. '%'."</td>";
echo "<td height ='54' align='center'>" . $strfbk6= row['strfbk6']. '%'."</td>";
echo "<td height ='54' align='center'>" . $strfbk7= row['strfbk7']. '%'."</td>";
} ?> 
</tr>
<!--  Row four Entrys -->
<tr><?php
while($row = mysql_fetch_array($result))
{   

echo "<td height ='54' align='center'>". $stralm1= row['stralm1']. '%'."</td>";
echo "<td height ='54' align='center'>" . $stralm2= row['stralm2'] .'%'."</td>";
echo "<td height ='54' align='center'>" . $stralm3= row['stralm3']. '%'."</td>";
echo "<td height ='54' align='center'>" . '---'."</td>";
echo "<td height ='54' align='center'>". $stralm4= row['stralm4'] .'%'."</td>";
echo "<td height ='54' align='center'>" . $stralm5= row['stralm5'] .'%'."</td>";
echo "<td height ='54' align='center'>" . '---'."</td>";
echo "<td height ='54' align='center'>" . '---'."</td>";
}
?> 
</tr>

</tr>
</table>

2 个答案:

答案 0 :(得分:0)

是: 回声&#34;&#34; 。 $ diff1 = row [&#39; diff1&#39;]。&#39;&#39;。 $ direction1 = row [&#39; direction1&#39;]。 &#34;&#34; ;

尝试: 回声&#34;&#34; 。 $ diff1 = $ row [&#39; diff1&#39;]。&#39;&#39;。 $ direction1 = $ row [&#39; direction1&#39;]。 &#34;&#34; ;

你在while循环中的每个echo语句中都缺少$ row []前面的$

答案 1 :(得分:0)

在循环中尝试这个。 (我假设您的表中包含具有这些特定名称的行。如果没有,请删除类似$row['direction']的数字,并让您的循环抓住每个变量。)

编辑:我删除了变量定义,因为它覆盖并重新定义了上面所做的事情。从我所看到的情况来看,$diff变量不是来自上面的数据库,而是来自数据库中的其他变量。假设这些是正确计算的并且该部分没有错误,请尝试以下(使用您在上面提供的示例)。注意,如果变量不是来自某一行,则不希望使用row

echo '<td height ="54" align="center">' . $diff5.' '. $direction5. '</td>' ;