【问题标题】:if, else inside whileif, else 在 while
【发布时间】:2015-12-12 09:53:45
【问题描述】:

我有一个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!";
}

我的问题是......我只想在满足子句时才显示第二个表单按钮。 该按钮是“标记为已读”按钮,如果该消息尚未标记为已读,我只想在行上显示它。 我尝试了很多不同的方法,但我想我没有抓住重点。

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

【问题讨论】:

  • 不要让我们猜测这是哪种语言...
  • 对不起兄弟,我忘了提...这是php
  • $row 的哪个属性告诉它是否已经被读取?
  • 您应该向我们提供您的结果集中可用的字段。哪个字段包含有关消息是否标记为“已读”的信息。它可以有什么价值?
  • 从表中隐藏的一个。一个名为“lida”的 sql 行。我希望对普通用户隐藏它

标签: php loops if-statement while-loop


【解决方案1】:

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

您可以在 while 中嵌套 if(等等),例如:

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

【讨论】:

  • 我猜这是最干净的方法!谢谢大家~
【解决方案2】:

很简单。

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" 更改为您用来表示消息是否已读的任何内容。

我希望这会有所帮助。

【讨论】:

    【解决方案3】:

    如果 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; ?>
    

    【讨论】:

      【解决方案4】:

      您可以按如下方式执行此操作,实际上是使用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>";
      }
      

      注意:您不应该在&lt;/tr&gt; 之后添加&lt;br&gt;,这应该发生在&lt;/table&gt; 标记之后。

      【讨论】:

        猜你喜欢
        • 2015-10-23
        • 2023-03-22
        • 1970-01-01
        • 1970-01-01
        • 2014-11-29
        • 2012-01-06
        • 2015-11-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多