【问题标题】:PHP if statement in html-table outputs errorhtml-table中的PHP if语句输出错误
【发布时间】:2017-05-07 08:03:58
【问题描述】:

我是 HTML5 和 PHP 的新手,我正在尝试在表数据中输出特定值,如果数据库检索值是每个条件。

我的代码:

<table class="scroll">
    <thead style="background-color: #99E1D9; color: #705D56;">
        <tr>
            <th>ID</th>
            <th>Name Client</th>
            <th>Last Update</th>
            <th style="padding-left: 30%;">Status</th>
        </tr>
    </thead>
        <tbody id="hoverTable">
                 <?php

                    $connection = mysql_connect('localhost', 'root', ''); 

                    mysql_select_db('patientdb');

                    $query = "SELECT id, name, date FROM clients";

                    $result = mysql_query($query);

                    //status waarden uit
                    $status = "SELECT status FROM clients";
                    $status_ = mysql_query($status);

                    while($row = mysql_fetch_array($result)){   //Loop through results
                    echo "<tr> 

                            <td>" . $row['id'] . "</td> 
                            <td>" . $row['name'] . "</td> 
                            <td>" . $row['date'] . "</td>
                            <td style='padding-left: 30%;'>" . 

                                if ($status_ > 60){echo "red";
                                } elseif ($status_ > 50){echo "yellow";
                                } else{echo "green";}

                                . "</td>

                         </tr>"; 
                    }
                    mysql_close(); 
                ?>
</tbody>
</table> 

错误输出

解析错误:语法错误,意外的 T_IF 在 /test/composition/login/portal/portal.php 在第 204 行

解决这个问题的正确方法是什么?

编辑

我当前的代码:

<table class="scroll">
    <thead style="background-color: #99E1D9; color: #705D56;">
        <tr>
            <th>Naam Client</th>
            <th>Laatste Update</th>
            <th style="margin-left: 40%; padding-left: 0%;">Status</th>
        </tr>
    </thead>
    <tbody id="hoverTable" style="font-size: 11pt;">

<?php


    $connection = mysql_connect('localhost', 'root', ''); 
     mysql_select_db('patientdb');

    $query = "SELECT id, naam, datum FROM clients";
    $result = mysql_query($query);

    $query2 = "SELECT status FROM clients";
    $result2 = mysql_query($query2);

    if (!empty ($result2)) {
    while ($row2 = mysql_fetch_assoc($result2)) {
    echo $row2['status'] . "<br />";
    }
    }

    while($row = mysql_fetch_array($result)){   //Loop through results
    echo "<tr> 

            <td>" . $row['id'] . "</td> 
            <td>" . $row['naam'] . "</td> 
            <td>" . $row['datum'] . "</td>
            <td style='padding-left: 30%;'>";

                if ($results2 > 60 && $results2 < 70) {
                    echo "red";
                } elseif ($results2 > 50 && $results2 < 60) { 
                    echo "yellow";
                } else { 
                    echo "green";
                }

                echo "</td>

         </tr>"; 
    }
    mysql_close(); 
?>

    </tbody>
</table>

输出正确的数据。但一部分在桌子外面,一部分在桌子里面。

【问题讨论】:

标签: php html css mysql


【解决方案1】:

试试:

$status = "green";
if ($status > 50)
{
    $status="yellow";
} 
elseif($status>60)
{
    $status="red";
}
echo "<tr> 
<td>" . $row['id'] . "</td> 
<td>" . $row['name'] . "</td> 
<td>" . $row['date'] . "</td>
<td style='padding-left: 30%;'>" .$status. "</td>
</tr>";

您不能将条件语句附加到字符串,例如首先分配给变量(就像我发布的那样)

【讨论】:

  • 你能看看我的编辑。你可以很好地工作。我刚刚发现我的 mysql-retrived-data 的输出不是应该的。它应该输出为单独的整数。
【解决方案2】:

这部分不在正确的位置:

if ($status_ > 60){echo "red";
                            } elseif ($status_ > 50){echo "yellow";
                            } else{echo "green";}

应该是:

echo "<tr>
        <td>" . $row['id'] . "</td> 
        <td>" . $row['name'] . "</td> 
        <td>" . $row['date'] . "</td>
        <td style='padding-left: 30%;'>";
if ($status_ > 60){
   echo "red";
} elseif ($status_ > 50){
   echo "yellow";
} else{
   echo "green";
}
echo "</td></tr>";

【讨论】:

  • 你能看看我的编辑。你可以很好地工作。我刚刚发现我的 mysql-retrived-data 的输出不是应该的。它应该输出为单独的整数。
【解决方案3】:

status_ 肯定不会返回一个数字,而是一个数组。

$status_ = mysql_query($status);

在不知道返回什么数据的情况下,很难提供帮助。

mysql_query

【讨论】:

  • 它返回整数,如 40、45、50、55、60
  • 我不明白它如何返回一个单数,而不是 1,以表明查询有效,或者如果查询失败则什么也没有。我想了解如何?
  • 我的想法是上下文。它无法解析 $status_ 因为它是一个数组,而不是单数。但我承认我可能是错的,只是从未见过它提供高于 1 的值。
  • @BadAddy 你是对的!我检查它不会将我检索到的数据库数据输出为单独的整数。知道如何获得这个吗?
  • @BadAddy 我做了一个编辑:我在其中显示了使用的 mysql/php 代码行和输出。我不知道如何解决这个问题,以便得到清晰、分离的整数。
【解决方案4】:

您不能在像echo 这样的另一个语句中间有一个if 语句(或任何其他语句)。如果要根据变量连接不同的字符串,可以使用条件(AKA “三元”)运算符。

               echo "<tr> 

                        <td>" . $row['id'] . "</td> 
                        <td>" . $row['name'] . "</td> 
                        <td>" . $row['date'] . "</td>
                        <td style='padding-left: 30%;'>" . 
                            $status_ > 60 ? "red" : ($status_ > 50 ? "yellow" : "green" )
                            . "</td>

                     </tr>"; 

【讨论】:

  • 我已经尝试过您的方法,但仍有一些问题,您可以看看我的question吗?
【解决方案5】:

您必须从 echo 中删除 if 语句才能消除错误试试这个:

<table class="scroll">
    <thead style="background-color: #99E1D9; color: #705D56;">
        <tr>
            <th>ID</th>
            <th>Name Client</th>
            <th>Last Update</th>
            <th style="padding-left: 30%;">Status</th>
        </tr>
    </thead>
        <tbody id="hoverTable">
                 <?php

                    $connection = mysql_connect('localhost', 'root', ''); 

                    mysql_select_db('patientdb');

                    $query = "SELECT id, name, date FROM clients";

                    $result = mysql_query($query);

                    //status waarden uit
                    $status = "SELECT status FROM clients";
                    $status_ = mysql_query($status);

                    while($row = mysql_fetch_array($result)){   //Loop through results
                    echo "<tr> 

                            <td>" . $row['id'] . "</td> 
                            <td>" . $row['name'] . "</td> 
                            <td>" . $row['date'] . "</td>
                            <td style='padding-left: 30%;'>";

                                if ($status_ > 60) {
                                    echo "red";
                                } elseif ($status_ > 50) { 
                                    echo "yellow";
                                } else { 
                                    echo "green";
                                }

                                echo "</td>

                         </tr>"; 
                    }
                    mysql_close(); 
                ?>
</tbody>
</table>

【讨论】:

  • @user7186749 你能看看我的回答吗
  • 嘿伙计,非常好。我更改了获取我的数据库代码的 php 部分。因为我的 php 没有正确输出我的数据库数据。你的代码是可以理解的。虽然我的输出在我的桌子内外都没有。为什么?你能看看我的编辑吗?
  • 你有什么版本的php?
  • 我哥,我修好了。感谢您的回复!
  • 我已经尝试了你的方法,但仍然有一些问题,你可以看看我的question吗?
猜你喜欢
  • 1970-01-01
  • 2022-12-08
  • 2022-12-22
  • 1970-01-01
  • 2018-03-01
  • 2016-07-21
  • 2018-03-29
  • 1970-01-01
  • 2015-07-07
相关资源
最近更新 更多