dongxianghui3709 2014-07-02 23:12
浏览 140
已采纳

在echo中有一个if语句

Can anyone tell me how I can include the if statement inside this echo? I need my while statement to keep producing results and I need an efficient way to insert the if statement into this code after div class poster, can someone give any suggestions?

while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
echo ' 
<div class="wrapper">
<div class="submissions">
<div class="logo-logo"><h2>Question.</h2>
<div class="checkboxes">'.$row['formtype'].'
</div>

</div>
<div class="top-submit">
&#8220'. $row["actual_quote"] . '&#8221;
</div>
<div class="poster">- '. $row["poster"].'
if(isset($row3['voted'])){
if(isset($row3['ip'])){

    echo "You have already voted for this.";
}
}
else{

    echo "<form action = '' method = 'post'> <input type = 'submit' name = 'like' value = 'like'> <input type = 'submit' name = 'dislike' value = 'dislike'></form>";

}
<div class = "like">

</div>
<div class = "dislike">
</div>
</div>
<!-- use select to get the items to stay on the page-->

</div>
</div>
</div>
';
}
  • 写回答

5条回答 默认 最新

  • douwen0647 2014-07-03 01:12
    关注

    I would not output your HTML using echo. It makes the code harder to read and write due to you needing to constantly remember to not accidentally close your quote, not to mention it's a (small) waste of CPU cycles and memory.

    Instead, break out of php to stream the content directly and only drop back into php for logic and echoing your variables. In addition, you should use htmlentities to encode what you're echoing out.

    I assume $row3 may be further up in your code so I left that alone for keys voted and ip. You had an extra closing div in there which I removed.

    <?php
    while($row = $stmt->fetch(PDO::FETCH_ASSOC))
    {
        ?>
    <div class="wrapper">
        <div class="submissions">
            <div class="logo-logo"><h2>Question.</h2>
                <div class="checkboxes"><?php echo htmlentities($row["formtype"]); ?></div>
            </div>
            <div class="top-submit">
                &#8220;<?php echo htmlentities($row["actual_quote"]); ?>&#8221;
            </div>
            <div class="poster">- <?php echo htmlentities($row["poster"]);
        if(isset($row3["voted"]) && isset($row3["ip"]))
        {
            echo "You have already voted for this.";
        }
        else
        {
            ?>
                <form action="" method="post"> <input type="submit" name="like" value="like" /> <input type="submit" name="dislike" value="dislike" /></form>
            <?php
        }
        ?>
                <div class = "like"></div>
                <div class = "dislike"></div>
            </div>
            <!-- use select to get the items to stay on the page-->
    
        </div>
    </div>
        <?php
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥20 为什么我写出来的绘图程序是这样的,有没有lao哥改一下
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥200 关于#c++#的问题,请各位专家解答!网站的邀请码
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号