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条)

报告相同问题?

悬赏问题

  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题
  • ¥15 谁会P4语言啊,我想请教一下
  • ¥15 哪个tomcat中startup一直一闪而过 找不出问题
  • ¥15 这个怎么改成直流激励源给加热电阻提供5a电流呀
  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成