duanqiongniu1469 2019-02-05 15:05 采纳率: 100%
浏览 69
已采纳

如何将2个div的foreach结果分开?

Here's the thing: I have a foreach loop that adds inputs dynamically. I need it to place part of them in one div, the rest in another. The current code is the following:

$sqla=mysql_fetch_row($sql);

$x=1;
if($x<50)
{
?>
    <div class="area area-1">
<?  
    foreach($sqla as $key=>$values){
        if ($key == "0") {
            continue;
        }
        $icheck = ($values > 0) ? "icheck" : "";
        $ichecked = ($values > 0) ? "isChecked" : "";

        echo "<label class='label-area label-".$values['num".$x."'][$x]." ".$ichecked."'><input name='data[ar][".$x."][]' type='checkbox' value='".$x."' title='".$x."' class='".$icheck." archeck1'><span class='label-num'>".$x."</span><span class='label-check-mark'></span></label>";

       if ($key == "50") {
           break;
        }
        $x++;

        if ($values > 0) {
            $new_rand_arr[] = $values;
        }
    }
?>
</div>
<?
}else{
?>
    <div class="zodiak ar-1">
<?
    foreach($sqla as $key=>$values){
        if ($key == "50") {
            continue;
        }
        $icheck = ($values > 0) ? "icheck" : "";
        $ichecked = ($values > 0) ? "isChecked" : "";

        echo "<label class='label-area label-".$values['num".$x."'][$x]." ".$ichecked."'><input name='data[ar][".$x."][]' type='checkbox' value='".$x."' title='".$x."' class='".$icheck." archeck1'><span class='label-num'>".$x."</span><span class='label-check-mark'></span></label>";

        if ($key == "62") {
            break;
        }
        $x++;

        if ($values > 0) {
            $new_rand_arr[] = $values;
        }
    }
?>
</div>
<?  
}
?>

The output puts it all in the first div, but none in the "zodiak ar-1" one. The target thing is everything after the 50-th key to go into that div. Hope that managed to explain the issue... Thank you

  • 写回答

1条回答 默认 最新

  • douyu7210 2019-02-05 15:22
    关注

    Right now you are doing this:

    $x=1;
    if($x<50)
    {
     // your code
    } else {
      // your code
    }
    

    The problem is that you do a foreach INSIDE the if statement, so $x < 50 will ALWAYS be true because just before you do $x = 1.

    Now in both foreach loop you do this :

    foreach($sqla as $key=>$values){
       if ($key == "0") {
           continue;
       }
       // your code
    }
    
    foreach($sqla as $key=>$values){
        if ($key == "50") {
            continue;
        }
        // your code
    }
    

    So you use a var $x that you increment each turn but you have a $key that you use too to check if value is <50 or not?

    So try something like this :

    $new_rand_arr = array();
    
    $open_first_div  = false;
    $open_second_div = false;
    
    $html = "";
    
    foreach($sqla as $key=>$values){
       if ($key < "50") { 
    
           // You open your first div one time
           if (!$open_first_div) {
               $html .= "<div class=\"area area-1\">";
               $open_first_div = true;
           }
    
           $icheck = ($values > 0) ? "icheck" : "";
           $ichecked = ($values > 0) ? "isChecked" : "";
    
           html .= "<label class='label-area label-".$values['num".$x."'][$x]." ".$ichecked."'><input name='data[ar][".$x."][]' type='checkbox' value='".$x."' title='".$x."' class='".$icheck." archeck1'><span class='label-num'>".$x."</span><span class='label-check-mark'></span></label>";
    
           if ($values > 0) {
               $new_rand_arr[] = $values;
           }
    
       } else {
            // You close your first div and open the second div
           if (!$open_second_div) {
               $html .= "</div><div class=\"zodiak ar-1\">";
               $open_second_div = true;
           }
    
           $icheck = ($values > 0) ? "icheck" : "";
           $ichecked = ($values > 0) ? "isChecked" : "";
    
           $html .= "<label class='label-area label-".$values['num".$x."'][$x]." ".$ichecked."'><input name='data[ar][".$x."][]' type='checkbox' value='".$x."' title='".$x."' class='".$icheck." archeck1'><span class='label-num'>".$x."</span><span class='label-check-mark'></span></label>";
    
           if ($values > 0) {
               $new_rand_arr[] = $values;
           }
    
       }
    }
    
    // After the foreach your close your div
    $html .= "</div>";
    
    // You display it
    echo $html;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私