在echo中有一个if语句

时间:2014-07-02 23:12:20

标签: php database if-statement pdo

有谁能告诉我如何在这个回声中加入if语句?我需要我的while语句来继续产生结果,我需要一种有效的方法在div类海报之后将if语句插入到此代码中,有人可以给出任何建议吗?

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 个答案:

答案 0 :(得分:1)

你只需要分解回声并在if之间包含if语句。

echo 'sample content';
if($x=1) 'if statement content';
echo 'more sample content';

答案 1 :(得分:1)

我不会使用echo输出您的HTML。它使代码更难以读写,因为你需要经常记住不要不小心关闭你的引用,更不用说它(小)浪费CPU周期和内存。

相反,打破php直接流内容,只返回php进行逻辑并回显你的变量。此外,您应该使用htmlentities对您回复的内容进行编码。

我认为$row3可能会在您的代码中更进一步,因此我将其留给了密钥votedip。你在那里有一个额外的关闭div,我删除了。

<?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
}

答案 2 :(得分:0)

您只需要在echo之前“完成”您的第一个if,然后再开始新的<div class="poster">- '. $row["poster"].'

替换:

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

使用:

<div class = "like">

echo '<div class = "like">

使用:

{{1}}

它应该有用。

答案 3 :(得分:0)

另一种方法(方法)是以这种方式使用三元运算符:

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

但是如果你没有简短的语句和值来回复它会降低可读性,所以明智地使用它。

答案 4 :(得分:0)

将您的HTML从PHP逻辑中移除到单独的模板文件中:

<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>

然后从你的while循环中包含它:

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

Alternative syntax for control structures