-1

事情是这样的:我有一个动态添加输入的 foreach 循环。我需要将它们的一部分放在一个 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>
<?  
}
?>

输出将其全部放在第一个 div 中,但没有放在“zodiak ar-1”中。目标是进入该 div 的第 50 个键之后的所有内容。希望设法解释这个问题......谢谢

4

1 回答 1

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
}

因此,您使用一个 var$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;
于 2019-02-05T15:22:42.187 回答