简体   繁体   English

在回显中包含if语句

[英]having an if statement inside an echo

Can anyone tell me how I can include the if statement inside this echo? 谁能告诉我如何在回显中包含if语句? 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语句以继续产生结果,并且我需要一种有效的方法来将if语句插入div类发布者之后的这段代码中,有人可以提出任何建议吗?

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>
';
}

you would just break up the echo and include the if statement in between eg 您只需分解回波并在之间添加if语句,例如

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

I would not output your HTML using echo . 我不会使用echo输出您的HTML。 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. 由于您需要不断记住不要无意间关闭引号,这使代码更难读写,更不用说浪费CPU周期和内存了。

Instead, break out of php to stream the content directly and only drop back into php for logic and echoing your variables. 取而代之的是,脱离php直接流传输内容,而仅返回php进行逻辑处理并回显您的变量。 In addition, you should use htmlentities to encode what you're echoing out. 此外,您应该使用htmlentities来编码要回显的内容。

I assume $row3 may be further up in your code so I left that alone for keys voted and ip . 我假设$row3可以进一步在你的代码,所以我离开了单独的按键votedip You had an extra closing div in there which I removed. 您在其中删除了一个额外的结束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
}

You just need to "finish" your first echo before the if , and start a new one afterwards. 您只需要在if之前“完成”您的第一个echo ,然后再开始一个新的echo

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. 它应该工作。

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. 但是,如果没有简短的语句和要回显的值,它将降低可读性,因此请明智地使用它。

Take your HTML out of your PHP logic and into a separate template file: 将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>

Then include it from your while loop: 然后从您的while循环中包括它:

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

Alternative syntax for control structures 控制结构的替代语法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM