duandianzhong8315 2014-02-25 15:01
浏览 39
已采纳

如果两个条件中的任何一个为false,则执行php echo命令[关闭]

I have a php file that basically serves up random text. the random text can have its vote incremented or decremented by 1 (one). if the net vote is negative, the text is excluded from view, else show the text. Now i want ONLY text posted within the last week to be considered, else, echo "nothing to show now!"

This is the code i have:

$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>";        
        }
    }
}

edit: code now shortened to most relevant parts.

  • 写回答

2条回答 默认 最新

  • donglou1866 2014-02-25 15:21
    关注

    From what I understand, you're wanting to know if there were rows returned from your query (since the query already checks to see if there were entires from the last 7 days) and if so, display them, and if not, display your message.

    To achieve this, you'll need something like this:

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

    Once the statement is successfully executed, it attempts to fetch the first row. If it's successful, it falls into a do{}while loop, with the same logic you had before. The difference is, if no row is fetched, then it will display your "Nothing to show!" message.

    I also moved some stuff around to be a little more logical with the new code. Full code:

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

    EDIT: updated code with the else statement

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 蓝桥oj3931,请问我错在哪里
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染