Foreach循环和If语句

时间:2012-03-06 02:40:07

标签: php

我有这个foreach语句应该在数组中循环,而不是输出:

[复选框]这里的东西

它将改为输出:

[复选框] [复选框]这里的东西

基本上,if语句没有被跳过,但是foreach循环正在查看我的数组中的值(在这种情况下我只有2用于测试目的)并且同时处理它们而不是单独处理它们。我做了一些回声测试,这就是发生了什么,但为什么呢?

$ blogID将具有数组值56,57,因为循环同时处理两个值,然后if语句中的两个选项都变为true,因为56等于56,但是56在其第二个上不等于57传递所以它会将if语句的后半部分处理为true。这很奇怪,我以前从来没有这样做过。

以下是代码:

$getblog = mysql_query("SELECT * FROM content WHERE blogID = '{$_REQUEST['id']}' AND type = '5' ORDER BY `order` ASC");
while ($row = mysql_fetch_assoc($getblog)){
$blogID = $row['id'];    
$tempData[$blogID][] = $row;

$data = $_REQUEST['blogIDS'];
$ids = explode(",", $data);

foreach($ids as $blogIDS) {
    $getblogids = mysql_query("SELECT * FROM content WHERE id = '$blogIDS' AND type = '5' ORDER BY `order` ASC");
    while ($row2 = mysql_fetch_assoc($getblogids)){
        $blogIDS = $row2['id'];    
        $tempData2[$blogIDS][] = $row2;
    }
}
    if ($row['id'] == $blogIDS) {
            echo "<input type='checkbox' name='ids[]' value='{$row['id']}' checked='yes'/>\n";
    } else {
            echo "<input type='checkbox' name='ids[]' value='{$row['id']}'/>\n";
    }

    echo "<a class='heriyah_text1' href='manage_blogposts_add.php?resize=1&edit=1&id={$row['id']}&blogID={$_REQUEST['id']}&pageID={$_REQUEST['pageID']}&div={$_REQUEST['div']}'>{$row['title']}</a><p></p>\n";
}

2 个答案:

答案 0 :(得分:1)

你可以这样做:

$getblog = mysql_query("SELECT * FROM content WHERE blogID = '{$_REQUEST['id']}' AND 
                     type = '5' ORDER BY `order` ASC");
while ($row = mysql_fetch_assoc($getblog)){
    $tempData[] = $row;
}

$data = $_REQUEST['blogIDS'];
$ids = explode(",", $data);

foreach($ids as $blogID) {
   foreach($tempData as $key => $value){
      if($value['id'] == $blogID){
         echo "<input type='checkbox' name='ids[]' value='{$value['id']}' checked='yes'/>\n";
      }else{
         echo "<input type='checkbox' name='ids[]' value='{$value['id']}'/>\n";
      }

      echo "<a class='heriyah_text1' href='manage_blogposts_add.php?resize=1&edit=1&id={$value['id']}&blogID={$_REQUEST['id']}&pageID={$_REQUEST['pageID']}&div={$_REQUEST['div']}'>{$value['title']}</a><p></p>\n";
   }
}

答案 1 :(得分:1)

编辑。试试这个让我知道:

$getblog = mysql_query("SELECT * FROM content WHERE blogID = '{$_REQUEST['id']}' AND 
                         type = '5' ORDER BY `order` ASC");
while ($row = mysql_fetch_assoc($getblog)){
    $blogID = $row['blogID'];    
    $tempData[$blogID][] = $row;
}

$data = $_REQUEST['blogIDS'];
$ids = explode(",", $data);

foreach($ids as $blogID) {
    if (array_key_exists($blogID, $tempData)) {
        foreach($tempData[$blogID] as $key => $value) {
            if ($value['blogID'] == $blogID) {
                echo "<input type='checkbox' name='ids[]' value='{$value['id']}' checked='yes'/>\n";
            } else {
                echo "<input type='checkbox' name='ids[]' value='{$value['id']}'/>\n";
            }

            echo "<a class='heriyah_text1' href='manage_blogposts_add.php?resize=1&edit=1&id={$value['id']}&blogID={$_REQUEST['id']}&pageID={$_REQUEST['pageID']}&div={$_REQUEST['div']}'>{$value['title']}</a><p></p>\n";
        }
    }
}