dongxianghui3709
dongxianghui3709
2014-07-02 23:12

在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 douwen0647 7年前

    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
    }
    
    点赞 评论 复制链接分享
  • duanhuancong1969 duanhuancong1969 7年前

    Take your HTML out of your PHP logic and into a separate template file:

    <div class="wrapper">
        <div class="submissions">
            <div class="logo-logo">
                <h2>Question.</h2>
    
                <div class="checkboxes">
                    <?php echo $row['formtype']; ?>
                </div>
            </div>
    
            <div class="top-submit">
                &#8220<?php echo $row["actual_quote"]; ?>&#8221;
            </div>
    
            <div class="poster">
                - <?php echo $row["poster"]; ?>
                <?php echo if(isset($row3['voted'])): ?>
                    <?php echo if(isset($row3['ip'])): ?>
                        You have already voted for this.
                    <?php endif; ?>
                <?php else: ?>
                    <form action = '' method = 'post'> <input type = 'submit' name = 'like' value = 'like'>
                        <input type = 'submit' name = 'dislike' value = 'dislike'>
                    </form>
                <?php endif; ?>
    
                <div class = "like">
    
                </div>
    
                <div class = "dislike">
    
                </div>
            </div>
            <!-- use select to get the items to stay on the page-->
    
        </div>
    </div>
    

    Then include it from your while loop:

    while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
       include __DIR__ . "/form.template.phtml";
    }
    

    Alternative syntax for control structures

    点赞 评论 复制链接分享
  • dongliyan7318 dongliyan7318 7年前

    Another way (approach) would be to use Ternary operator this way:

    echo ('if statement') ? 'value on true' : 'value on false';
    

    But it will reduce readability if you don't have short statement and values to echo, so use it wisely.

    点赞 评论 复制链接分享
  • dongluan5740 dongluan5740 7年前

    You just need to "finish" your first echo before the if, and start a new one afterwards.

    Replace:

    <div class="poster">- '. $row["poster"].'
    

    with:

    <div class="poster">- '. $row["poster"];
    

    And

    <div class = "like">
    

    with:

    echo '<div class = "like">
    

    and it should work.

    点赞 评论 复制链接分享
  • dsa80833 dsa80833 7年前

    you would just break up the echo and include the if statement in between e.g.

    echo 'sample content';
    if($x=1) 'if statement content';
    echo 'more sample content';
    
    点赞 评论 复制链接分享

相关推荐