duanqiongniu1469
2019-02-05 15:05
浏览 67
已采纳

如何将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;
    
    打赏 评论

相关推荐 更多相似问题