简体   繁体   English

PHP Mysql-添加空行无法弄清楚

[英]PHP Mysql - adding an empty row can't figure it out

Here is the table code - when this is printed out the has one empty row at the bottom. 这是表格代码-打印出来时,表格底部有一个空行。 I am not sure why the last row is added? 我不确定为什么要添加最后一行? Is it perhaps the if else statement? 也许是if陈述吗?

<?php
$row = mysql_fetch_array($result);
if ($row['Price'] == '0.00') {
echo "<TABLE  width='100%'>\n";
echo "<TR><TD id=tblrowsHead2>Item</TD><TD id=tblrowsHead2>Size</TD><TD  
id=tblrowsHead2>Contact</TD></TR>\n";
for ($i = 0; $i < $numofrows; $i++) {
    $row = mysql_fetch_array($result); //get a row from our result set
    if ($i % 2) { //this means if there is a remainder
        echo "<TR bgcolor=\"#E6E6FA\">\n";
    } else { //if there isn't a remainder we will do the else
        echo "<TR bgcolor=\"white\">\n";
    }
    echo "<TD id=Size>" . $row['Item'] . "</TD><TD id=Size>" . $row['Size'] 
. "</TD> 
<TD id=Price>Contact Us</TD>\n";

}
} else {
echo "<TABLE  width='100%'>\n";
echo "<TR><TD id=tblrowsHead2>Item</TD><TD id=tblrowsHead2>Size</TD><TD  
id=tblrowsHead2>Price</TD></TR>\n";
for ($i = 0; $i < $numofrows; $i++) {
    $row = mysql_fetch_array($result); //get a row from our result set
    if ($i % 2) { //this means if there is a remainder
        echo "<TR bgcolor=\"#E6E6FA\">\n";
    } else { //if there isn't a remainder we will do the else
        echo "<TR bgcolor=\"white\">\n";
    }
    echo "<TD id=Size>" . $row['Item'] . "</TD><TD id=Size>" . $row['Size'] 
. "</TD> 
<TD id=Price>$" . $row['Price'] . "</TD>\n";

}
}

//now let's close the table and be done with it
echo "</TABLE>\n";
?>

Try this code instead: 请尝试以下代码:

<?php

$row = mysql_fetch_array($result);
$i = 0;
if ($row['Price'] == '0.00') {
    echo "<TABLE  width='100%'>\n";
    echo "<TR><TD id=tblrowsHead2>Item</TD><TD id=tblrowsHead2>Size</TD><TD id=tblrowsHead2>Contact</TD></TR>\n";
    do {
        if ($i % 2) { //this means if there is a remainder
            echo "<TR bgcolor=\"#E6E6FA\">\n";
        } else { //if there isn't a remainder we will do the else
            echo "<TR bgcolor=\"white\">\n";
        }
        echo '<TD id=Size>'.$row['Item'].'</TD><TD id=Size>'.$row['Size'].'</TD>';
        echo "<TD id=Price>Contact Us</TD></TR>\n";
        $i++;
    } while ($row = mysql_fetch_array($result)); //get a row from our result set
} else {
    do {
        if ($i % 2) { //this means if there is a remainder
            echo "<TR bgcolor=\"#E6E6FA\">\n";
        } else { //if there isn't a remainder we will do the else
            echo "<TR bgcolor=\"white\">\n";
        }
        echo '<TD id=Size>'.$row['Item'].'</TD><TD id=Size>'.$row['Size'].'</TD>';
        echo "<TD id=Price>$" . $row['Price'] . "</TD></TR>\n";
        $i++;
    } while ($row = mysql_fetch_array($result)); //get a row from our result set
}
//now let's close the table and be done with it
echo "</TABLE>\n";

When you using the mysql_fetch_array at the first line of for-loop , you will lost the first record of your data-table, so you will have an empty line at the end for-loop的第一行使用mysql_fetch_array时,您将丢失数据表的第一条记录,因此末尾将有一个空行

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

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