继续使用 mysql_fetch_assoc

发布于 2024-12-17 13:13:16 字数 445 浏览 0 评论 0原文

我有几行代码,但找不到正确使用它的正确方法。

$cc = 0;
$tt = 50;

while ($row = mysql_fetch_assoc($result)) {

//building array with values from DB.

    if (++$cc < $tt)
        continue;

    for ($i = 0; $i < $tt; $i++) {

    //Work with the array

    }
}

假设我在数据库中有 133 个结果。它将获得第一个 50 - 在 for 循环中执行某些操作,然后再执行 50 个,将再次执行 for 循环并停止。 最后 33 个结果将保持不变。 它会得到它们,但因为无法达到 50 将停止,并且它们不会经历 for 循环。

我的问题是如何将它们“发送”到那里的循环中?

I have a few lines of code, but can't find the right way to use it properly.

$cc = 0;
$tt = 50;

while ($row = mysql_fetch_assoc($result)) {

//building array with values from DB.

    if (++$cc < $tt)
        continue;

    for ($i = 0; $i < $tt; $i++) {

    //Work with the array

    }
}

Let's say I have 133 results in DB. It'll get first 50 - do something in the for loop, then 50 more, will go thru the for loop again and will stop.
The last 33 results will be untouched.
It'll get them, but cause can't reach 50 will stop and they won't go through the for loop.

My problems is how to "send" them in the loop down there?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

江心雾 2024-12-24 13:13:16

将 for 循环移到函数中并在 while 循环之后调用它:

$cc = 0;
$tt = 50;
while ($row = mysql_fetch_assoc($result)) {

//building array with values from DB.

    if (++$cc < $tt) continue;

    work_with_array($array);
}
if($cc) work_with_array($array);

function work_with_array($array) {
    for ($i = 0; $i < count($array); $i++) {
        //Work with the array
    }
}

Move the for loop in a function and call it after the while loop:

$cc = 0;
$tt = 50;
while ($row = mysql_fetch_assoc($result)) {

//building array with values from DB.

    if (++$cc < $tt) continue;

    work_with_array($array);
}
if($cc) work_with_array($array);

function work_with_array($array) {
    for ($i = 0; $i < count($array); $i++) {
        //Work with the array
    }
}
墨离汐 2024-12-24 13:13:16

试试这个:

$i = 0
while ($row = mysql_fetch_assoc($result)):
  if($i < 50):
   //Code when $i is less than 50
   //Insert code here

  elseif($i > 50 && $i < 100):
   //Code when $i is more than 50 but less than 100
   //Insert code here

  elseif($i > 100):
   //Code when $i is more than 100
   //Insert code here

  endif;
 $i++;
endwhile;

所以所有结果都会经历这个循环。如果 $i 小于 50(或者如果结果小于 50),则执行一些代码,或者如果 $i 大于 50 但小于 100,则执行一些不同的代码。最后,如果 $i 大于 100,则执行一些其他代码。

你明白吗?

Try this:

$i = 0
while ($row = mysql_fetch_assoc($result)):
  if($i < 50):
   //Code when $i is less than 50
   //Insert code here

  elseif($i > 50 && $i < 100):
   //Code when $i is more than 50 but less than 100
   //Insert code here

  elseif($i > 100):
   //Code when $i is more than 100
   //Insert code here

  endif;
 $i++;
endwhile;

So all results are going through this loop. If $i is less than 50 (or if the result is less than 50) then some code is executed, or if $i is more than 50 but less than 100 then some different code is executed. Finally if $i is more than 100 then some other code is executed.

Do you understand?

兰花执着 2024-12-24 13:13:16

你可以尝试:

$i = 0
while ($row = mysql_fetch_assoc($result) && $i < 100):
  if($i < 50):
   //Code when $i is less than 50
  else:
   //Code more than or equal to 50
  endif;
 $i++;

endwhile;

You could try:

$i = 0
while ($row = mysql_fetch_assoc($result) && $i < 100):
  if($i < 50):
   //Code when $i is less than 50
  else:
   //Code more than or equal to 50
  endif;
 $i++;

endwhile;
晨与橙与城 2024-12-24 13:13:16

循环内的所有继续似乎都是不必要的。如果您只是尝试处理整个结果集并分块执行某些操作,那么您可以这样做,

$cc = 0;
$tt = 50;
$result_array = array();

// this will chunk your array into blocks
while ($row = mysql_fetch_assoc($result)) {

    //building array with values from DB.
    $result_array[intval($cc++/$tt)] = $row;

}

// at this point you should have result_array with indexes:0,1,2 and have subarrays with 50, 50, 33 entries each.


foreach ($result_array as $k=>$sub_array) {
    //Work with your sub array
    foreach ($sub_array as $one_row)
    {
       // do something with the row
    }
}

但我确实同意@Col.Shrapnel。为什么要在 while 循环内创建另一个数组,只是为了一次一行地遍历该数组来执行某些操作?如果您一次发送一批数据(当然,就像批量插入数据库一样),但再次循环似乎很奇怪,这是有意义的。为什么你不能在 while 循环中做同样的事情

All the continues inside the loop seem unneccesary. If you're simply trying to process the entire result set and do something in chunks, you can do this

$cc = 0;
$tt = 50;
$result_array = array();

// this will chunk your array into blocks
while ($row = mysql_fetch_assoc($result)) {

    //building array with values from DB.
    $result_array[intval($cc++/$tt)] = $row;

}

// at this point you should have result_array with indexes:0,1,2 and have subarrays with 50, 50, 33 entries each.


foreach ($result_array as $k=>$sub_array) {
    //Work with your sub array
    foreach ($sub_array as $one_row)
    {
       // do something with the row
    }
}

I do agree with @Col.Shrapnel though. Why are you creating another array inside the while loop just to go through that array one row at a time to do something? It would've made sense if you send out a batch of data at once (like bulk insert into db, sure) but to loop through again seems odd. Why can't you do the same right in the while loop

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文