如果两个条件之一为false,则执行php echo命令

时间:2014-02-25 15:01:31

标签: php

我有一个基本上提供随机文本的php文件。随机文本可以使其投票增加或减少1(一)。如果净投票为负,则文本将从视图中排除,否则显示文本。现在我只想在上周内发布的文字被考虑,否则,回声“现在没什么可显示的!”

这是我的代码:

$db = new PDO('mysql:host=localhost;dbname=db;charset=utf8', 'user', 'pass');

$stmt = $db->prepare('SELECT vote FROM voting where item = ? AND timestamp > DATE_SUB(NOW() , INTERVAL 7 DAY)');
($stmt->execute(array($item)));
($row = $stmt->fetch(PDO::FETCH_ASSOC));
if ($row['vote'] < 0) {
    $stmt = $db->prepare('SELECT id,username,tag,message,timestamp FROM mybq_post_txt where id = ?');
    if ($stmt->execute(array($id))) {
        while($row = $stmt->fetch(PDO::FETCH_ASSOC))       
        {
            echo "<p>", "<span class='tagtext'>author: </span>", ($row['username']), " ", "<span class='tagtext'>timestamp: </span>", ($row['timestamp']), "<br>"; 
            echo "<span class='tagtext'>tag: </span>", ($row['tag']), "<br>";                                                                   
        }
    }
    echo "<div class='loginreq'>This Post has been Voted Out because it achieved a negative vote balance. It can be voted back in by achieving a positive vote balance.</div>";
    echo "<div class='loginreq'>NOTE: This post may have been voted out because it was misleading or offensive. vote-in at your own risk.</div>";
    $stmt = $db->prepare('SELECT id,username,tag,message FROM mybq_post_txt where id = ?');
    if ($stmt->execute(array($id))) {
        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
            echo "<div class='vot_updown2' id=" . 'vt_' .$table.($row['id']). "></div>";    
        }
    }
}
else
{               
    $stmt = $db->prepare('SELECT id,username,tag,message,timestamp FROM mybq_post_txt where id = ? AND timestamp > DATE_SUB(NOW() , INTERVAL 7 DAY)');
    if ($stmt->execute(array($id))) {
        while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
        {
            echo "<p>", "<span class='tagtext'>author: </span>", ($row['username']), " ", "<span class='tagtext'>timestamp: </span>", ($row['timestamp']), "<br>";
            echo "<span class='tagtext'>tag: </span>", ($row['tag']), "<br>";
            echo "<p class='image_container'>", ($row['message']), "</p>";
            echo "<div class='vot_updown2' id=" . 'vt_' .$table.($row['id']). "></div><br>";        
        }
    }
}

编辑:现在代码缩短为大多数相关部分。

2 个答案:

答案 0 :(得分:1)

根据我的理解,您想知道查询是否返回了行(因为查询已经检查过去7天是否有entires),如果是,则显示它们,如果没有,显示你的信息。

要实现这一目标,你需要这样的东西:

if($stmt->execute(array($id))){
    if ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
        do {
          echo "<p>", "<span class='tagtext'>author: </span>", ($row['username']), " ", "<span class='tagtext'>timestamp: </span>", ($row['timestamp']), "<br>"; 
            echo "<span class='tagtext'>tag: </span>", ($row['tag']), "<br>";
        } while ($row = $stmt->fetch(PDO::FETCH_ASSOC));
    } else {
        echo 'Nothing to show now!';
    }
}

成功执行语句后,它会尝试获取第一行。如果成功,它将进入do{}while循环,具有与之前相同的逻辑。不同之处在于,如果没有获取任何行,那么它将显示您的“无需显示!”消息。

我还使用新代码移动了一些更合乎逻辑的东西。完整代码:

<?php
$db = new PDO('mysql:host=localhost;dbname=db;charset=utf8', 'user', 'pass');


$stmt = $db->prepare('SELECT vote FROM voting where item = ? AND timestamp > DATE_SUB(NOW() , INTERVAL 7 DAY)');
($stmt->execute(array($item)));
($row = $stmt->fetch(PDO::FETCH_ASSOC));
if ($row['vote'] < 0) {
    $stmt = $db->prepare('SELECT id,username,tag,message,timestamp FROM mybq_post_txt where id = ?');
    if($stmt->execute(array($id))){
        // if there is a row to fetch, get it
        if ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
            do {
                echo "<p>", "<span class='tagtext'>author: </span>", ($row['username']), " ", "<span class='tagtext'>timestamp: </span>", ($row['timestamp']), "<br>"; 
                echo "<span class='tagtext'>tag: </span>", ($row['tag']), "<br>";
                // moved these echos in here so that it won't display if there are no rows
                echo "<div class='loginreq'>This Post has been Voted Out because it achieved a negative vote balance. It can be voted back in by achieving a positive vote balance.</div>";
                echo "<div class='loginreq'>NOTE: This post may have been voted out because it was misleading or offensive. vote-in at your own risk.</div>";
                // also moved this in here so it won't display if there are no rows.
                $stmt = $db->prepare('SELECT id,username,tag,message FROM mybq_post_txt where id = ?');
                if($stmt->execute(array($id))){
                    if ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
                        do {
                          echo "<div class='vot_updown2' id=" . 'vt_' .$table.($row['id']). "></div>";
                        } while ($row = $stmt->fetch(PDO::FETCH_ASSOC));
                    }
                }
            } while ($row = $stmt->fetch(PDO::FETCH_ASSOC));
        // if no rows, show this message
        } else {
            echo 'Nothing to show now!';
        }
    }
} else {
    $stmt = $db->prepare('SELECT id,username,tag,message,timestamp FROM mybq_post_txt where id = ? AND timestamp > DATE_SUB(NOW() , INTERVAL 7 DAY)');
    if($stmt->execute(array($id))){
        if ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
            do {
                echo "<p>", "<span class='tagtext'>author: </span>", ($row['username']), " ", "<span class='tagtext'>timestamp: </span>", ($row['timestamp']), "<br>";
                echo "<span class='tagtext'>tag: </span>", ($row['tag']), "<br>";
                echo "<p class='image_container'>", ($row['message']), "</p>";
                echo "<div class='vot_updown2' id=" . 'vt_' .$table.($row['id']). "></div><br>";
            } while ($row = $stmt->fetch(PDO::FETCH_ASSOC));
        // if no rows, show this message
        } else {
            echo 'Nothing to show now!';
        }
    }
}

?>

编辑:使用else语句更新代码

答案 1 :(得分:0)

我猜你必须在没有会话时重定向到登录页面,如果是会话,则没有用户名。

首先检查会话是否已设置,那是否为false,然后检查myusername是否存在,那就是false。

if (!isset($_SESSION['myusername']) && $_SESSION['myusername'] == '')