Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如果字段不是空的,如何显示一次带有表头的表,后跟多个数据库字段内容?_Php_Mysql_Html Table - Fatal编程技术网

Php 如果字段不是空的,如何显示一次带有表头的表,后跟多个数据库字段内容?

Php 如果字段不是空的,如何显示一次带有表头的表,后跟多个数据库字段内容?,php,mysql,html-table,Php,Mysql,Html Table,我想得到: 引用: 引用1 报价来源1 引用2 报价来源2 我目前得到: 引用: 引用1 报价来源1 引用: 引用2 报价来源2 对我有效的最终代码: echo ("<table border='1'>"); $header_printed = false; while($row = mysql_fetch_array($rs)) { if ($row['quote']) { if ($header_printed === false) { echo

我想得到:

引用:
引用1
报价来源1

引用2
报价来源2

我目前得到:

引用:
引用1
报价来源1

引用:
引用2
报价来源2

对我有效的最终代码:

  echo ("<table border='1'>");

$header_printed = false;

while($row = mysql_fetch_array($rs)) {
if ($row['quote']) {
    if ($header_printed === false) {
        echo "   

            <tr>
                <th>Quotes:</th>
            </tr>";
        $header_printed = true;
    }

    echo "
        <tr>
            <td>".$row['quote']."</td>
            <td>".$row['quote_source']."</td>
        </tr>";
}

}

echo ("</table>");
echo(“”);
$header_printed=false;
while($row=mysql\u fetch\u数组($rs)){
如果($row['quote'])){
如果($header_printed===false){
回声“
引用:
";
$header_printed=true;
}
回声“
“$row['quote']”
“$row['quote_source']”
";
}
}
回声(“”);
echo“引号:”;
while($row=mysql\u fetch\u数组($rs)){
如果($row['quote'])){
回显“$row['quote']”。$row['quote_source']”;
}
}
回声“;

将标题从循环中取出:

$header_printed = false;

while($row = mysql_fetch_array($rs)) {
    if ($row['quote']) {
        if ($header_printed === false) {
            echo "   
                <table border='1'>
                <tr>
                    <th>Quotes:</th>
                </tr>";
            $header_printed = true;
        }

        echo "
            <tr>
                <td>".$row['quote']."</td>
                <td>".$row['quote_source']."</td>
            </tr>";
    }

}
$header\u printed=false;
while($row=mysql\u fetch\u数组($rs)){
如果($row['quote'])){
如果($header_printed===false){
回声“
引用:
";
$header_printed=true;
}
回声“
“$row['quote']”
“$row['quote_source']”
";
}
}

不是为每个报价打开一个新表,而是在循环之前打开一次,并且在每个循环步骤中只写入一个新的TR元素?谢谢您的帮助!唯一剩下的问题是,即使$row['quote']字段中没有数据,它仍然会回显引号。对不起,我知道我的问题不清楚。我只想在($row['quote'])中有数据的情况下使标题显示一次,因此没有数据,没有标题。但是如果有数据,标题只显示一次。编辑答案,这样脚本只有在有数据显示时才会打印标题。这很好,谢谢
$header_printed = false;

while($row = mysql_fetch_array($rs)) {
    if ($row['quote']) {
        if ($header_printed === false) {
            echo "   
                <table border='1'>
                <tr>
                    <th>Quotes:</th>
                </tr>";
            $header_printed = true;
        }

        echo "
            <tr>
                <td>".$row['quote']."</td>
                <td>".$row['quote_source']."</td>
            </tr>";
    }

}