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 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥15 DruidDataSource一直closing
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签)
  • ¥50 sft下载大文阻塞卡死
  • ¥15 机器人轨迹规划相关问题