doude1917 2012-03-06 02:40
浏览 112
已采纳

Foreach循环和If语句

I have this foreach statement that is supposed to cycle through an array, but instead of outputting:

[checkbox] Something Here

It will instead output this:

[checkbox] [checkbox] Something here

Basically, the if statement isn't being skipped over, but the foreach loop is looking at the values in my array (in this case I only have 2 for testing purposes) and processing them at the same time instead of separately. I've done some echo tests and that is what is happening, but why?

$blogID will have array values 56,57 so because the loop is processing both values at the same time, then both options in the if statement become true because 56 equals 56, but then 56 doesn't equal 57 on its 2nd pass so it will process the 2nd half of the if statement as true. This is weird and I've never had this happen before.

Here is the code:

$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'/>
";
    } else {
            echo "<input type='checkbox' name='ids[]' value='{$row['id']}'/>
";
    }

    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>
";
}
  • 写回答

2条回答 默认 最新

  • donglue1886 2012-03-08 07:19
    关注

    edited. try this and let me know:

    $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'/>
    ";
                } else {
                    echo "<input type='checkbox' name='ids[]' value='{$value['id']}'/>
    ";
                }
    
                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>
    ";
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?