假设我有一个动态创建的表,其中填充了来自SELECT
句子的MySQL数据库中的数据。
现在,我需要从外部访问在表内的if
子句内定义的变量。为了实现这一点,我使用了一些$_SESSION
值,但是当我尝试将这种颜色应用于整个行时,它会将所有表格而不是存储会话值的单个行进行着色。我猜我还需要保存$RowCt
值,但不知道怎么做。
我已经解决了这个问题很长一段时间了,看起来我连续看了这段代码后有点盲目。任何帮助表示赞赏。
代码:
<?php include("../includes/header.php")?>
<div class="content" align="center">
<?php
function SQLResultTable($Query)
{
$link = mysql_connect("localhost", "...", "...") or die('Could not connect: ' . mysql_error());
mysql_select_db("...") or die('Could not select database');
$Table = "";
$Table.= "<table border='0' align='center'>";
$Result = mysql_query($Query); //Execute the query
if(mysql_error())
{
$Table.= "<tr><td>MySQL ERROR: " . mysql_error() . "</td></tr>";
}
else
{
//Header Row with Field Names
$NumFields = mysql_num_fields($Result);
$Table.= "<tr style=\"background-color: #D00000; float:center; font-size:14px; color: #FFFFFF;\" align='center'>";
for ($i=0; $i < $NumFields; $i++)
{
if ((mysql_field_name($Result, $i))=="a1") { $Table.= "<th>a1</th>";
} else if ((mysql_field_name($Result, $i))=="a2") { $Table.= "<th>a2</th>";
} else if ((mysql_field_name($Result, $i))=="a3") { $Table.= "<th>a3</th>";
} else if ((mysql_field_name($Result, $i))=="a4") { $Table.= "<th>a4</th>";
} else if ((mysql_field_name($Result, $i))=="a5") { $Table.= "<th>a5</th>";
} else if ((mysql_field_name($Result, $i))=="a6") { $Table.= "<th>a6</th>";
} else if ((mysql_field_name($Result, $i))=="a7") { $Table.= "<th>a7</th>";
} else if ((mysql_field_name($Result, $i))=="a8") { $Table.= "<th>a8</th>";
} else if ((mysql_field_name($Result, $i))=="a9") { $Table.= "<th>a9</th>";
} else if ((mysql_field_name($Result, $i))=="a10") { $Table.= "<th>a10</th>";
} else if ((mysql_field_name($Result, $i))=="a11") { $Table.= "<th>a11</th>";
} else if ((mysql_field_name($Result, $i))=="a12") { $Table.= "<th>a12</th>";
} else { $Table.= "<th>" . mysql_field_name($Result, $i) . "</th>"; }
}
$Table.= "</tr>";
$RowCt = 0;
while($Row = mysql_fetch_assoc($Result))
{
$timezone = new DateTimeZone( "Europe/London" );
$date = new DateTime();
$date->setTimezone( $timezone );
$now = $date->format( 'H:i' );
if (!isset($_SESSION['b2'])) {
$Table.="<tr style='background-color:red;'>";
} else if (!isset($_SESSION['b1'])) {
$Table.="<tr style='background-color:yellow;'>";
} else {
$Table.="<tr style='background-color:white;'>";
}
foreach($Row as $field => $value) {
if ($field == 'b1') {
if ($value > $now) {
$_SESSION['b1'] = $b1;
$Table.= "<td style='font-size:14px;' align='center'>$value</td>";
} else {
$Table.= "<td style='font-size:14px;' align='center'>$value</td>";
}
} else if ($field == 'b2') {
if ($value > $now) {
$_SESSION['b2'] = $b2;
$Table.= "<td style='font-size:14px;' align='center'>$value</td>";
} else {
$Table.= "<td style='font-size:14px;' align='center'>$value</td>";
}
} else {
$Table.= "<td style='font-size:14px;' align='center'>$value</td>";
}
}
$Table.= "</tr>";
}
$Table.= "<tr style=\"background-color: #FFFFFF; font-size:10px; color: #000000;\"><td colspan='$NumFields'><br />Query returned " . mysql_num_rows($Result) . " result(s).</td></tr>";
}
$Table.= "</table>";
return $Table;
}
?>
<?php
echo "<div align='center'><u><strong><a href='index.php'>Query Results</a></strong></u></div>";
echo SQLResultTable("SELECT a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12 FROM test_db ORDER BY a6");
?>
</div>
现在,$now
值返回实际小时,即08:30;字段$value
和b1
上的b2
返回与上一个表单存储的格式相似的值。
交易是这样的:当它迟到时(字段b1上的值大于现在),它将黄色的行着色。如果它很晚(字段b2上的值大于现在的值),它会将红色的行着色。否则,该行采用白色背景色。
使用上面的代码,它会为整个表格着色。并且无法找到如何撤消这一点,因此它将只是存储会话值的行,也就是迟到或很晚的行。
感谢任何帮助,询问您是否需要更多数据。
答案 0 :(得分:1)
不要迂腐,但有可能更容易,更少有头痛的方式来写这个!但简而言之,我会说你的代码几乎就在那里。您的桌子是否以一种颜色显示,取决于一天中的时间?我说它与您的会话变量有关,只是 设置 ,而且 未设置 。
这样的事情就足够了:
...
while($Row = mysql_fetch_assoc($Result))
{
$timezone = new DateTimeZone( "Europe/London" );
$date = new DateTime();
$date->setTimezone( $timezone );
$now = $date->format( 'H:i' );
foreach($Row as $field => $value) {
if ($field == 'b1') {
if ($value > $now) {
$Table.= "<td style='font-size:14px; background-color:YELLOW;' align='center'>$value</td>";
} else {
$Table.= "<td style='font-size:14px; background-color:WHITE;' align='center'>$value</td>";
}
} else if ($field == 'b2') {
if ($value > $now) {
$Table.= "<td style='font-size:14px; background-color:RED;' align='center'>$value</td>";
} else {
$Table.= "<td style='font-size:14px; background-color:WHITE;' align='center'>$value</td>";
}
} else {
$Table.= "<td style='font-size:14px; background-color:WHITE;' align='center'>$value</td>";
}
}
$Table.= "</tr>";
}
...
很抱歉,如果我混淆了颜色! ;)希望这有助于指出你正确的轨道!
修改强> 的
检查下面注释的PHP代码,找出每3行切换颜色的解决方案(希望我说得对 - 写得非常快!)
...
// so this is my understanding of what I think this code must do
// based on what your original code looks like and what your original
// question was highlighting...
// based on how you're calling the rows and checking the fields for
// values 'b1' and 'b2', I'm making an assumption here that the fields
// 'b1' and 'b2' will always both exist in every row you parse. Maybe you
// shouldn't be checking the '$field' variable for equivalence to 'b1'
// or 'b2', but rather the '$value' variable? Regardless, I'm throwing down
// this code for the '$RowCt' question you had, it will alternate between the
// 3 colours...I hope it helps...
$RowCt = 0;
while($Row = mysql_fetch_assoc($Result))
{
// get the time, that's all working ok...
$timezone = new DateTimeZone( "Europe/London" );
$date = new DateTime();
$date->setTimezone( $timezone );
$now = $date->format( 'H:i' );
// increase the counter, work out the colour...
$RowCt++;
$background_color = 'white';
if ($RowCt % 3 == 1)
$background_color = 'red';
if ($RowCt % 3 == 2)
$background_color = 'yellow';
$Table.="<tr style='background-color:". $background_color .";'>";
foreach($Row as $field => $value)
$Table.= "<td style='font-size:14px;' align='center'>$value</td>";
$Table.= "</tr>";
}
...