在Foreach中嵌套If语句

时间:2014-10-29 10:13:34

标签: php mysql

我正在尝试自动排除我的博客中有负投票(< 0)的条目。下面是我正在使用的代码,但它不排除,继续显示负面投票的博客。 (现在在公共系统上,所以可能直到稍后才会回复,但是,感谢您的帮助!)

$id = $_GET['id']; // get initial query from index

            $table = 'txt';
            $item = 'vt_' .$table.($_GET['id']);

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

$sql = $db->query('SELECT * from txt ORDER BY ID DESC LIMIT 10 OFFSET '.$browse);


$stmt = $db->prepare('SELECT vote FROM voting where item = ?');
($stmt->execute(array($item)));
($vrow = $stmt->fetch(PDO::FETCH_ASSOC));

            foreach ($sql as $row) {
            if ($vrow['vote'] < 0){
              echo "<b>Update:</b><br>";
              echo "<font color = 'red'><i>", "This Update,", "</i></font>", "<br>";
              echo "By: ", ($row['username']), "<br>"; 
              echo "At: ", ($row['timestamp']), "<br>";
              echo "<font color = 'red'><i>Has been</i> <b>Voted Out</b></font>", "<br>";
              echo "<span class='loginnotreq'>", "<b>", ($row['edited']), "</b>", "&nbsp;", "&nbsp;", "<span 

class='loginreq'>", "<b>", ($row['locked']), "</b>", "</span>", "</span>";
              echo "&nbsp;", "&nbsp;";
              if($row['locked'] !='locked'){
              echo "&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 

&nbsp;&nbsp;<a href ='edit-post-login.php?pid=".$row['id']."&login=true&edit=true' method='post' id ='form-id' 

data-ajax='false' '><b><span class = 'neutral'>edit post</span></b></a>";}

              echo "&nbsp;", "&nbsp;";


              echo "<div class='vot_updown2' id=" . 'vt_' .$table.($row['id']). "></div><br>";              
              }
              else {
              echo "<b>Update:</b><br>";
              echo ($row['message']), "<br>";
              echo "By: ", ($row['username']), "<br>"; 
              echo "At: ", ($row['timestamp']), "<br>";              
              echo "<span class='loginnotreq'>", "<b>", ($row['edited']), "</b>", "&nbsp;", "&nbsp;", "<span 

class='loginreq'>", "<b>", ($row['locked']), "</b>", "</span>", "</span>";
              echo "&nbsp;", "&nbsp;";
              if($row['locked'] !='locked'){
              echo "&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 

&nbsp;&nbsp;<a href ='edit-post-login.php?pid=".$row['id']."&login=true&edit=true' method='post' id ='form-id' 

data-ajax='false' '><b><span class = 'neutral'>edit post</span></b></a>";}

              echo "&nbsp;", "&nbsp;";


              echo "<div class='vot_updown2' id=" . 'vt_' .$table.($row['id']). "></div><br>";
              }

              }

3 个答案:

答案 0 :(得分:1)

您只需将查询更改为仅返回投票等于或大于零的行:

例如:

SELECT vote FROM voting where item = ? and vote >= 0

这样你就不需要循环中的条件了。您只需要处理大于或等于零的投票的部分。

答案 1 :(得分:0)

最简单的答案(无需复制和粘贴所有代码):

foreach($row as $post) {
  if($post['rating'] >= 0) {
    //do something
  }
}

答案 2 :(得分:0)

将值解析为 INT ,因为它来自数据库,通常采用字符串的形式:

 if ( (int)$vrow['vote'] < 0){ ...