如何将foreach结果除以2 div?

时间:2019-02-05 15:05:35

标签: php css arrays foreach

这就是问题:我有一个foreach循环,可以动态添加输入。我需要将它们的一部分放在一个div中,其余的放在另一个div中。当前代码如下:

$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>
<?  
}
?>

输出将所有内容放入第一格,但未将所有内容放入“ zodiak ar-1”。目标是进入该div的第50个键之后的所有内容。希望能够解释这个问题... 谢谢

1 个答案:

答案 0 :(得分:0)

现在您正在执行此操作:

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

问题在于您在if语句内进行了foreach操作,因此$x < 50总是正确的,因为在您进行$x = 1之前。

现在在两个foreach循环中,您都可以这样做:

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

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

因此,您使用的变量$x每转一圈都会增加,但是您也有一个$key用于检查值是否为<50

所以尝试这样的事情:

$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;