在PHP代码中应用HTML代码-语法错误

时间:2019-01-24 16:38:44

标签: php html mysql database

早上好

当我尝试执行以下代码时遇到语法问题错误:

while($row = mysqli_fetch_array($result))
{
    $output .= '
        <tr>
            <td>'.$row["reqname"].'</td>
            <td>'.$row["month"]."/".$row["day"]."/".$row["year"].'</td>
            <td>'.$row["empname"].'</td>
            <td>'.$row["position"].'</td>
            <td>'.$row["account"].'</td>
            <td>'.$row["platform"].'</td>
            <td>'.$row["processor"].'</td>
            <td>'.$row["ram"].'</td>
            <td>'.$row["monitor"].'</td>
            <td>'.$row["phone"].'</td>
            <td>'.$row["phonetype"].'</td>
            <td>'.$row["headset"].'</td>
            <?php'. if ($row['status']) : .'?>
            <td>'.$row["status"].'</td>
            <?php else : ?>
            <td>
                <form method="post" action="update-request-status.php">
                    <input type="hidden" name="reqnumber" value="<?php echo $row_message['reqnumber']; ?>" />
                    <button class="button" type="submit" name="completed" value=""><span>New Request!</span></button>
                </form>
            </td>
            <td><a href="records.php?reqnumber='.$row['reqnumber'] .'"><i class="fa fa-edit" style="color: black; font-size: 25px;"></i></a></td>
            <td><a href="delete.php?reqnumber='.$row['reqnumber'] .'"><i class="fa fa-trash" style="color: red; font-size: 25px;"></i></a></td>
        </tr>
    ';
}
echo $output;

我认为我在以下方面有问题:

<?php'. if ($row['status']) : .'?>
                <td>'.$row["status"].'</td>
                <?php else : ?>
                <td>
                    <form method="post" action="update-request-status.php">
                        <input type="hidden" name="reqnumber" value="<?php echo $row_message['reqnumber']; ?>" />
                        <button class="button" type="submit" name="completed" value=""><span>New Request!</span></button>
                    </form>
                </td>

如您所见,我尝试添加'..'来模仿其他代码,但是由于某些原因,我仍然遇到错误。

PS:对不起,如果我的代码不干净,我在粘贴到此处之前尝试对其进行清理,但是由于某种原因,它不遵循该格式。

编辑:

<tr>
    <td><?php echo $row_message['reqname']; ?></td>
    <td><?php echo $row_message['month']."/".$row_message['day']."/".$row_message['year']; ?></td>
    <td><?php echo $row_message['empname']; ?></td>
    <td><?php echo $row_message['position']; ?></td>
    <td><?php echo $row_message['account']; ?></td>
    <td><?php echo $row_message['platform']; ?></td>
    <td><?php echo $row_message['processor']; ?></td>
    <td><?php echo $row_message['ram']; ?></td>
    <td><?php echo $row_message['monitor']; ?></td>
    <td><?php echo $row_message['phone']; ?></td>
    <td><?php echo $row_message['phonetype']; ?></td>
    <td><?php echo $row_message['headset']; ?></td>
    <?php if ($row_message['status']) : ?>
    <td><?php echo $row_message['status']; ?></td>
    <?php else : ?>
    <td>
        <form method="post" action="update-request-status.php">
            <input type="hidden" name="reqnumber" value="<?php echo $row_message['reqnumber']; ?>" />
            <button class="button" type="submit" name="completed" value=""><span>New Request!</span></button>
        </form>
    </td>
    <?php endif ; ?>
    <td><a href="records.php?reqnumber=<?php echo $row_message['reqnumber']; ?>"><i class="fa fa-edit" style="color: black; font-size: 25px;"></i></a></td>
    <td><a href="delete.php?reqnumber=<?php echo $row_message['reqnumber']; ?>"><i class="fa fa-trash" style="color: red; font-size: 25px;"></i></a></td>
</tr>

2 个答案:

答案 0 :(得分:1)

您可以打印带有回显的HTML,但不能打印PHP,因为这是由浏览器解释的

您的代码应为

while($row = mysqli_fetch_array($result))
{
    $output .= '<tr>
                <td>'.$row["reqname"].'</td>
                <td>'.$row["month"]."/".$row["day"]."/".$row["year"].'</td>
                <td>'.$row["empname"].'</td>
                <td>'.$row["position"].'</td>
                <td>'.$row["account"].'</td>
                <td>'.$row["platform"].'</td>
                <td>'.$row["processor"].'</td>
                <td>'.$row["ram"].'</td>
                <td>'.$row["monitor"].'</td>
                <td>'.$row["phone"].'</td>
                <td>'.$row["phonetype"].'</td>
                <td>'.$row["headset"].'</td>';
    if ($row['status']) :
        $output .= '<td>'.$row["status"].'</td> ';
    else:
        $output .= '
                <td>
                    <form method="post" action="update-request-status.php">
                        <input type="hidden" name="reqnumber" value="'.$row_message['reqnumber'].'" />
                        <button class="button" type="submit" name="completed" value=""><span>New Request!</span></button>
                    </form>
                </td>
                <td><a href="records.php?reqnumber='.$row['reqnumber'] .'"><i class="fa fa-edit" style="color: black; font-size: 25px;"></i></a></td>
                <td><a href="delete.php?reqnumber='.$row['reqnumber'] .'"><i class="fa fa-trash" style="color: red; font-size: 25px;"></i></a></td>
            </tr>
        ';
     endif;
}  
echo $output;

对不起,我的英语不好。

答案 1 :(得分:0)

您的输出中有一个未关闭的php标记,并且您错过了endif

那应该是以下代码:

while($row = mysqli_fetch_array($result)) {
    $output .= '
        <tr>
            <td>'.$row["reqname"].'</td>
            <td>'.$row["month"]."/".$row["day"]."/".$row["year"].'</td>
            <td>'.$row["empname"].'</td>
            <td>'.$row["position"].'</td>
            <td>'.$row["account"].'</td>
            <td>'.$row["platform"].'</td>
            <td>'.$row["processor"].'</td>
            <td>'.$row["ram"].'</td>
            <td>'.$row["monitor"].'</td>
            <td>'.$row["phone"].'</td>
            <td>'.$row["phonetype"].'</td>
            <td>'.$row["headset"].'</td>';

    if ($row['status']) :
        $output .= '
            <td>'.$row["status"].'</td>';
    else : 
        $output .= '
            <td>
                <form method="post" action="update-request-status.php">
                    <input type="hidden" name="reqnumber" value="' . $row_message['reqnumber'] . '" />
                    <button class="button" type="submit" name="completed" value=""><span>New Request!</span></button>
                </form>
            </td>
            <td><a href="records.php?reqnumber='.$row['reqnumber'] .'"><i class="fa fa-edit" style="color: black; font-size: 25px;"></i></a></td>
            <td><a href="delete.php?reqnumber='.$row['reqnumber'] .'"><i class="fa fa-trash" style="color: red; font-size: 25px;"></i></a></td>
        </tr>';
    endif;
}

echo $output;

但是您应该查看the Heredoc syntax,使用这种语法,您的代码将是:

while($row = mysqli_fetch_array($result)) {
    $output .= <<<HTML
        <tr>
            <td>{$row['reqname']}</td>
            <td>{$row['month']}/{$row['day']}/{$row['year']}</td>
            <td>{$row['empname']}</td>
            <td>{$row['position']}</td>
            <td>{$row['account']}</td>
            <td>{$row['platform']}</td>
            <td>{$row['processor']}</td>
            <td>{$row['ram']}</td>
            <td>{$row['monitor']}</td>
            <td>{$row['phone']}</td>
            <td>{$row['phonetype']}</td>
            <td>{$row['headset']}</td>
HTML;

    if ($row['status']) {
        $output .= '
            <td>' . $row['status'] . '</td>';
    } else {
        $output .= <<<HTML
            <td>
                <form method="post" action="update-request-status.php">
                    <input type="hidden" name="reqnumber" value="{$row_message['reqnumber']}" />
                    <button class="button" type="submit" name="completed" value=""><span>New Request!</span></button>
                </form>
            </td>
            <td><a href="records.php?reqnumber={$row['reqnumber']}"><i class="fa fa-edit" style="color: black; font-size: 25px;"></i></a></td>
            <td><a href="delete.php?reqnumber={$row['reqnumber']}"><i class="fa fa-trash" style="color: red; font-size: 25px;"></i></a></td>
        </tr>
HTML;
    }
}

echo $output;

更具可读性,减少了出错的风险,...