简体   繁体   English

交替的行颜色我缺少什么

[英]Alternating row colors what am I missing

I get no errors but no table is displayed, I know its not on prepared statements but there is no interaction its just a display so there is no chance of SQL injection 我没有错误,但是没有显示任何表,我知道它不在准备好的语句中,但是没有交互,只是一个显示,所以没有SQL注入的机会

what Am I missing 我想念什么

Hello I have edited the long version 您好,我已经编辑了长版

     <?php
echo "<table border='10' style='border-collapse: collapse;border-color: #0099FF;'>";
echo "<tr style='font-weight: bold;'>";
echo "<td width='200' align='center'>NAme</td><td width='200' align='center'>Headhot</td><td width='200' align='center'>Date</td><td width='200' align='center'>Age</td>";
echo "</tr>";
include("dbopent.php");
$result = mysql_query("
SELECT N, Limg, Lpage, F, Age
FROM (
SELECT N, Limg, Lpage, F, Age
FROM (
SELECT N, Limg, Lpage, F, CURDATE( ) , (
YEAR( CURDATE( ) ) - YEAR( F )
) - ( RIGHT( CURDATE( ) , 5 ) < RIGHT( F, 5 ) ) AS Age
FROM ACT)alias
WHERE Age BETWEEN 30 AND 39)ALIAS
");


while($row=mysql_fetch_array($result))
$color="1";
{
  if($color==1){
echo "<tr bgcolor='#000000'>";

echo "<td align='center' width='10'>" . $row['N'] . "</td>";
echo "<td align='center' width='10'>" . "<a href=\"{$row['Lpage']}\"><img src=\"{$row['Limg']}\">" . "</td>";
echo "<td align='center' width='10'>" . $row['F'] . "</td>";
echo "<td align='center' width='10'>" . $row['Age'] . "</td>";

echo "</tr>";
}
else {
echo "<tr bgcolor='#FFFFFF'>";

echo "<td align='center' width='10'>" . $row['N'] . "</td>";
echo "<td align='center' width='10'>" . "<a href=\"{$row['Lpage']}\"><img src=\"{$row['Limg']}\">" . "</td>";
echo "<td align='center' width='10'>" . $row['F'] . "</td>";
echo "<td align='center' width='10'>" . $row['Age'] . "</td>";

echo "</tr>";

$color="1";
}
   }
echo '</table>';
?>

You write "SELECT SELECT". 您输入“ SELECT SELECT”。

PS And it is bad practice to mix PHP and HTML code. PS并且混合PHP和HTML代码是不好的做法。 Look towards the MVC. 展望MVC。

First issue is that you don't have the FROM clause in your SQL statement. 第一个问题是您的SQL语句中没有FROM子句。 A valid query is as follows: 有效查询如下:

SELECT * FROM Table WHERE condition = something . SELECT * FROM Table WHERE condition = something

You can define integers without the quotes: $color = "1"; 您可以定义不带引号的整数: $color = "1"; has to be $color = 1; 必须是$color = 1; Also, you are supposed to add it after the open curly bracket. 另外,应该将其添加到大括号后。

while($row=mysql_fetch_array($result))
{
   $color = 1;
   // rest of the code
}

Also please put error_reporting(E_ALL); 还请放error_reporting(E_ALL); on top of your script after the begin tag so it will display any errors on the screen. 在开始标记之后的脚本顶部,因此它将在屏幕上显示任何错误。

You should also explain your issue in a better way, it's unclear what you want. 您还应该以更好的方式解释您的问题,目前尚不清楚您想要什么。

Define color to 1... increase on each loop. 将颜色定义为1 ...在每个循环中增加。 do a modulus base 2 to see if there is a remainder and if there is it's odd then otherwise it's even. 做一个以2为底的模数,看是否有余数,如果是奇数,则为偶数。

$color = 1;
while($row=mysql_fetch_array($result))
{
$color++;
if ($color % 2 == 0){
    $outputColor = "#7b7b7b";
} else {
    $outputColor = "#fff";
}
echo "<tr bgcolor='".$outputColor."'>";

echo "<td align='center' width='10'>" . $row['Name'] . "</td>";
echo "<td align='center' width='10'>" . "<a href=\"{$row['page']}\"><img src=\"{$row['img']}\">" . "</td>";
echo "<td align='center' width='10'>" . $row['date'] . "</td>";
echo "<td align='center' width='10'>" . $row['Age'] . "</td>";

echo "</tr>";
}

this doesn't repeat all that code that is unnecessary.. rather define the color and output it once... 这不会重复所有不必要的代码。而是定义颜色并将其输出一次...

However you should REALLY be using a css style since i didn't think all browsers support a TR with a background color. 但是,您应该真正使用CSS样式,因为我不认为所有浏览器都支持具有背景色的TR。

Something like echo "<tr class='".$outputColor."'>"; 类似echo "<tr class='".$outputColor."'>"; and instead setting $outputColor to 'odd' or 'even' 而是将$ outputColor设置为'odd'或'even'

then .odd td {background-color:#7b7b7b} .even td {background-color:#fff} 然后.odd td {background-color:#7b7b7b} .even td {background-color:#fff}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM