如果,否则在里面

时间:2015-12-12 09:53:46

标签: php loops if-statement while-loop

我有一个while循环,其中包含以下代码:

if ($result->num_rows > 0) {
 echo "<font size='2'>";


     echo "<table>";
     echo "<tr><th>Sender</th><th>Message</th></tr>";
 while($row = $result->fetch_assoc()) {

     echo "<tr><td style='width:15%;'>" . $row["sender"]. "</td><td style='width:100%;'>  " . $row["message"]. "</td>
     <td>
        <form action='?delete=" . $row["note_id"]. "' method='post'>
            <input type='hidden' name='del_id' value='" . $row["note_id"]. "'/>
            <button type='submit' name='sell'><font size='1' color='blue'><img src='' height='9' width='9' title='Delete Message'/></font></button></form>

        <form action='?read=" . $row["note_id"]. "' method='post'>
            <input type='hidden' name='read_id' value='" . $row["note_id"]. "'/>
            <button type='submit' name='sell'><font size='1' color='blue'><img src='' height='9' width='9' title='Mark as Read'/></font></button></form>
     </td>

     </tr><br>";
 }
 echo "</table>";
 echo "</font>";
} else {
 echo "No Messages!";
}

我的问题是......如果符合条款,我只想显示第二个表单按钮。 该按钮是&#34; Mark as Read&#34;按钮,我只想在行上显示它,如果该消息没有标记为已读。 我尝试了一堆不同的方法,但我想我错过了这一点。

如果你能引导我朝着正确的方向前进,我将不胜感激。 谢谢!

4 个答案:

答案 0 :(得分:1)

您用什么来表示帖子是否被标记为已阅读?

你可以在whiles内部嵌套ifs(以此类推),如:

while ($row = function()) {
    if ($row['parameter']) {
        echo '';
    }
    echo '';
}

答案 1 :(得分:0)

简单。

if ($result->num_rows > 0) {
 echo "<font size='2'>";
echo "<table>";
echo "<tr><th>Sender</th><th>Message</th></tr>";
 while($row = $result->fetch_assoc()) {

     echo "<tr><td style='width:15%;'>" . $row["sender"]. "</td><td style='width:100%;'>  " . $row["message"]. "</td>
     <td>
        <form action='?delete=" . $row["note_id"]. "' method='post'>
            <input type='hidden' name='del_id' value='" . $row["note_id"]. "'/>
            <button type='submit' name='sell'><font size='1' color='blue'><img src='' height='9' width='9' title='Delete Message'/></font></button></form>";

            // here is the code i added. close the echo of above and put `if` statement.

if($row["read"]!="yes"){ echo "
        <form action='?read=" . $row["note_id"]. "' method='post'>
            <input type='hidden' name='read_id' value='" . $row["note_id"]. "'/>
            <button type='submit' name='sell'><font size='1' color='blue'><img src='' height='9' width='9' title='Mark as Read'/></font></button></form> ";
}   

// edit ends. again put another echo to continue your remaining code

echo "
     </td>

     </tr><br>";
 }
 echo "</table>";
 echo "</font>";
} else {
 echo "No Messages!";
}

只需将$row["read"]!="yes"更改为您用来表示是否读取邮件的任何内容。

我希望这有帮助。

答案 2 :(得分:0)

您可以按照以下方式执行此操作,确实使用if,但请为此中断echo,如下所示:

while($row = $result->fetch_assoc()) {
    echo "<tr><td style='width:15%;'>" . $row["sender"]. 
         "</td><td style='width:100%;'>  " . $row["message"]. "</td>
    <td>
        <form action='?delete=" . $row["note_id"]. "' method='post'>
            <input type='hidden' name='del_id' value='" . $row["note_id"]. "'/>
            <button type='submit' name='sell'>
                <font size='1' color='blue'>
                    <img src='' height='9' width='9' title='Delete Message'/>
                </font>
            </button>
        </form>";
    if ($row['lida'] == 0) {                    
        echo "      
        <form action='?read=" . $row["note_id"]. "' method='post'>
            <input type='hidden' name='read_id' value='" . $row["note_id"]. "'/>
            <button type='submit' name='sell'>
                <font size='1' color='blue'>
                    <img src='' height='9' width='9' title='Mark as Read'/>
                </font>
            </button>
        </form>";
    }
    echo "
    </td>
    </tr>";
}

注意:您不应在<br>之后添加</tr>,这应该在</table>代码之后添加。

答案 3 :(得分:0)

如果HTML和php没有分开,很难维护代码。 看我的解决方案如何分开。

您应该根据实际情况替换$condition

<?php if ($result->num_rows > 0) : ?>
<font size='2'>
<table>
    <tr>
        <th>Sender</th>
        <th>Message</th>
    </tr>

    <?php while($row = $result->fetch_assoc()) : ?>
    <tr>
        <td style='width:15%;'> <?php echo $row["sender"]; ?></td>
        <td style='width:100%;'> <?php echo $row["message"]; ?></td>
        <td>
            <?php if($condition): ?>
            <form action='?delete=<?php echo $row["note_id"]; ?>' method='post'>
                <input type='hidden' name='del_id' value='<?php echo $row["note_id"]; ?>'/>
                <button type='submit' name='sell'>
                    <font size='1' color='blue'><img src='' height='9' width='9' title='Delete Message'/></font>
                </button>
            </form>
            <?php endif; ?>

            <?php if(!$condition): ?>
            <form action='?read=<?php echo $row["note_id"]; ?>' method='post'>
                <input type='hidden' name='read_id' value='<?php echo $row["note_id"]; ?>'/>
                <button type='submit' name='sell'>
                    <font size='1' color='blue'><img src='' height='9' width='9' title='Mark as Read'/></font>
                </button>
            </form>
            <?php endif; ?>
        </td>
    </tr>
    <?php endwhile; ?>
</table>
</font>

<?php else: ?>
No Messages!
<?php endif; ?>