使用PHP在MySQL查询中根据结果显示图像

时间:2017-12-21 17:30:12

标签: php

我需要根据结果更改值。在这种情况下,如果我的行"状态"是"活跃"我想把这样的东西放在一张桌子里:

  <td>'.$row['register_status'].'</td>
        <td>'
        if($row['status']=='Active')
        {
        echo '<span class="glyphicon glyphicon-time" aria-hidden="true" style="color:orange"></span>';
        }
 '</td>

代码有什么问题?

if(mysql_num_rows($result) > 0)
{
    $number = 1;
    while($row = mysql_fetch_assoc($result))
    {
            $data .= '<tr>
            <td>'.$row['register_status'].'</td>
            <td>'
            if($row['status']=='Active')
            {
            echo '<span class="glyphicon glyphicon-time" aria-hidden="true" style="color:orange"></span>';
            }
            '</td>
        </tr>';
            $number++;
    }
}
else
{
    // records now found 
    $data .= '<tr><td colspan="6">Records not found!</td></tr>';
}

$data .= '</table>';

echo $data;

在日志中,错误是:

PHP Parse错误:语法错误,第32行/var/www/html/form/ajax/readRecords.php中的意外T_IF,引用:http:/ localhostform /

当第32行是:

            if($row['status']=='Active')
  • 更新*

最后我将代码更新为:

    while($row = mysql_fetch_assoc($result))
    {   
        $data .= '<tr>
                        <td>'.$row['register_status'].'</td>
                        <td>';

                        if($row['register_status']=='Active') {
                                $data .= '<span class="glyphicon glyphicon-true" aria-hidden="true" style="color:green"></span>';
                        }
                        else if($row['register_status']=='Inactive')
                        {       
                                $data .= '<span class="glyphicon glyphicon-time-remove" aria-hidden="true" style="color:red"></span>';
                        }
                        else    
                        {       
                                $data .= '<span class="glyphicon glyphicon-time" aria-hidden="true" style="color:orange"></span>';
                        }

                        $data .= '</td>
                        <td>'.$row['first_name'].'</td>
                  </tr>';   
    }
}
else
{
    $data .= '<tr><td colspan="6">Records not found!</td></tr>';
}

$data .= '</table>';

echo $data;

但总是执行最后一个,只将结果打印到

$data .= '<span class="glyphicon glyphicon-time" aria-hidden="true" style="color:orange"></span>';

为什么不根据不同情况设置不同的图标?

3 个答案:

答案 0 :(得分:0)

你在这里错过了一个分号:

$data .= '<tr>
  <td>'.$row['register_status'].'</td>
<td>'

应该是:

$data .= '<tr>
  <td>'.$row['register_status'].'</td>
<td>';

当弹出“语法错误,意外”之类的内容时,请始终查看上一条语句。

答案 1 :(得分:0)

这是更完整的修正

$number = 1;
while($row = mysql_fetch_assoc($result))
{
        $data .= '<tr>
        <td>'.$row['register_status'].'</td>
        <td>';
        if($row['status']=='Active')
        {
            $data .= '<span class="glyphicon glyphicon-time"
         aria-hidden="true" style="color:orange"></span>';
        }
        $data .= '</td></tr>';
        $number++;
}

答案 2 :(得分:0)

您的代码中有一些错误。我修复并评论了错误/建议。试试这个:

注意:使用MySQLi代替MySQL。我认为这里说i MySQLi代表改进就足够了。 MySQL也已弃用,将来的PHP版本将不再可用。

// Assuming you already have $data = '<table>';

if(mysql_num_rows($result) > 0)
{
    $number = 1;
    while($row = mysql_fetch_assoc($result))
    {
        $data .= '<tr>
                      <td>'.$row['register_status'].'</td>
                      <td>';       // Close $data .=, do if, open $data .= again
                          if($row['status']=='Active')
                          {
                               $data .= '<span class="glyphicon glyphicon-time" aria-hidden="true" style="color:orange"></span>';
                          }
                     $data .= '</td>
                  </tr>';
        $number++;          // I don't see a use for $number
    }
}
else
{
    // records now found 
    $data .= '<tr><td colspan="6">Records not found!</td></tr>';
}

$data .= '</table>';

echo $data;