简体   繁体   English

如何在WHILE循环中使用FOREACH循环创建几列

[英]How can I use a FOREACH loop inside of a WHILE loop to create several columns

PROBLEM 问题

I am pulling data from my database...using a WHILE loop & I want the returned data to be coupled into "scalable in size" groups. 我正在使用WHILE循环从数据库中提取数据,并且希望将返回的数据耦合到“规模可伸缩”组中。

I thought that by using a FOREACH LOOP inside of the while loop, I could accomplish this, apparently I thought wrong..ARRG! 我以为通过在while循环内使用FOREACH LOOP可以实现此目的,显然我认为错了。

LINK 链接

View problem here: http://sis-cr.com/NEWSTORE/store.php?cur_str=cel 在此处查看问题: http : //sis-cr.com/NEWSTORE/store.php?cur_str=cel

CODE

$queryCatz = mysqli_query($cxn, "SELECT DISTINCT idBrand FROM Products ");
while($list_of_xtras = mysqli_fetch_array($queryCatz)) {     

    $cur = 0;
    $rowNum = 1;
    foreach($list_of_xtras as $key => $value){

        if($cur == 0){
            echo '<ul style="border:2px solid purple;" class="theROW' . $rowNum . '">';
        }

        echo '    <li style="list-style:none;">' . $value. '</li>';

        if($cur == 2)
        {
            echo '</ul>';
            $cur = 0;
            $rowNum++;
        }
        else
        {
            $cur++;
        }
    }


}       

After 3 hours of trying to fix this, and at the risk of exposing my very obvious lack on understanding, I humbly bow before the collective minds of SO....and shout...HELP, I've coded and can't get up! 经过三个小时的尝试来解决这个问题,冒着暴露出我非常明显的理解不足的风险, 我谦虚地向SO的集体思想鞠躬。然后大喊大叫……帮助,我已经编码并且无法起床!

I might be off track. 我可能偏离了轨道。 I think you want: 我想你要:

$queryCatz = mysqli_query($cxn, "SELECT DISTINCT idBrand FROM Products ");
while($list_of_xtras = mysqli_fetch_array($queryCatz)) {     
  $rowNum = 1;
  echo '<ul style="border:2px solid purple;" class="theROW' . $rowNum . '">\r\n';
  for($curr=0; $curr<2; $curr++){
    echo '\t<li style="list-style:none;">' . $list_of_xtras[$curr] . '</li>\r\n';
  }
  echo '</ul>';
  $rowNum++;
}

Seems like you're just looking for 2 results, so I don't know if FOREACH is best here. 似乎您只是在寻找2个结果,所以我不知道FOREACH是否最好。 Maybe just a FOR loop. 也许只是一个FOR循环。

Edit, after I saw your comment. 在看到您的评论后进行编辑。 Sounds like you want to wrap 10 results in a UL. 听起来您想在UL中包装10个结果。 To do that it would be something like: 为此,它类似于:

$rowNum = 1;
echo '<ul style="border:2px solid purple;" class="theROW' . $rowNum . '">\r\n';
$queryCatz = mysqli_query($cxn, "SELECT DISTINCT idBrand FROM Products ");
while($list_of_xtras = mysqli_fetch_array($queryCatz)) {     
  if($rowNum % 10 == 0){
    echo '</ul><ul style="border:2px solid purple;" class="theROW' . $rowNum . '">\r\n';
  }
  echo '\t<li style="list-style:none;">' . $list_of_xtras[0] . '</li>\r\n';
  $rowNum++;
}
echo "</ul>\r\n";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM