如果url存储在mySQL

时间:2015-07-31 06:54:21

标签: mysql url if-statement iframe youtube

我创建了一个表单,我们可以在帖子中注册youtube embed url。 我只是在网址已注册此帖子时才显示iframe。

我制作了此代码以显示报告信息:

<?php

$req = $db->query('SELECT id, title, author, category, date_event, country, city, content, tag, youtube FROM report ORDER BY date_creation DESC LIMIT 0, 10');

while ($data = $req->fetch())
{
?>

<div class="news">
      <h3><?php echo htmlspecialchars($data['title']); ?></h3>
      <p>Author: <?php echo htmlspecialchars($data['author']); ?></p>
            <p>Category : <?php echo htmlspecialchars($data['category']); ?></p>
            <p>Date of the event : <?php echo htmlspecialchars($data['date_event']); ?></p>
            <p>Country : <?php echo htmlspecialchars($data['country']); ?></p>
            <p>City : <?php echo htmlspecialchars($data['city']); ?></p>
            <p class="display_list"><?php echo html_entity_decode($data['content'], ENT_HTML5 , 'UTF-8'); ?></p>
            <p>Tag : <?php echo htmlspecialchars($data['tag']); ?></p>
        <p>Youtube : <?php echo htmlspecialchars($data['youtube']); ?></p>
        <p>
            <?php
            $youtube=$data['youtube'];

            if($youtube==1){
                echo "<p>display iframe</p>";
            }else{
            {
                    echo "<p>don't display iframe</p>";
                }
            }        
            ?>
        </p>

显示网址​​的请求正常工作。如果它们包含在行中,则显示URL。

但条件代码会显示所有帖子的相同答案:&#34;不显示iframe&#34;。

$ youtube变量的条件声明当然不对,但我无法弄清楚这个问题。

1 个答案:

答案 0 :(得分:0)

you have assigned $youtube=1; instead of $youtube=$data['youtube']; in the code

try this 

<?php

$req = $db->query('SELECT id, title, author, category, date_event, country, city, content, tag, youtube FROM report ORDER BY date_creation DESC LIMIT 0, 10');

while ($data = $req->fetch())
{
?>

<div class="news">
      <h3><?php echo htmlspecialchars($data['title']); ?></h3>
      <p>Author: <?php echo htmlspecialchars($data['author']); ?></p>
            <p>Category : <?php echo htmlspecialchars($data['category']); ?></p>
            <p>Date of the event : <?php echo htmlspecialchars($data['date_event']); ?></p>
            <p>Country : <?php echo htmlspecialchars($data['country']); ?></p>
            <p>City : <?php echo htmlspecialchars($data['city']); ?></p>
            <p class="display_list"><?php echo html_entity_decode($data['content'], ENT_HTML5 , 'UTF-8'); ?></p>
            <p>Tag : <?php echo htmlspecialchars($data['tag']); ?></p>
            <p>

            <?php
$youtube=$data['youtube'];

if($youtube==1){
    echo "<p>display iframe</p>";
}else{
{
        echo "<p>don't display iframe</p>";
    }
}        
?>
</p>