【问题标题】:Continue in while with mysql_fetch_assoc继续使用 mysql_fetch_assoc
【发布时间】:2012-01-03 18:26:22
【问题描述】:

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

$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 循环。

我的问题是如何在下面的循环中“发送”它们?

【问题讨论】:

  • 这些循环是什么?我有一种强烈的感觉,它们非常相似,你实际上只需要一个。
  • 没有。而循环是从数据库中获取记录并构建数组。使用 for 循环,我运行“线程”(多线程 [curl_multi_getcontent 等])。所以我两个都需要。

标签: php arrays loops while-loop continue


【解决方案1】:

你可以试试:

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

【讨论】:

  • 这里无法理解逻辑。你能解释一下吗?我正在努力保持代码的结构。
  • 这里你基本上是在告诉脚本如果 $i 小于 100 然后加载循环。因此将显示前 100 个结果。然后 if 语句说如果 $i 小于则运行此代码,否则(如果 $i 大于 50)运行此代码。最后我添加了 $i++;所以在每个循环结束时,$i 的价值都会上升。当 $i 达到 100 时,循环将停止。
  • 所以当它从 DB 获得 100 个结果时代码会停止,对吧?我不想要这个。我想从数据库中获取所有记录,构建一个数组并在 for 循环中使用这个数组。对不起,如果我弄错了,但它很混乱。
  • 你想要数组中的所有 133 个结果吗?
  • 所以...看看我的代码。我和他们一起研究“碎片”。首先我得到 50,和他们一起做点什么,然后再得到 50 个,做点什么,我想得到最后 33 个,也想和他们做点什么,但我不能得到它们,因为 $cc 不能达到 50,但只有 33 .
【解决方案2】:

试试这个:

$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,则执行其他代码。

你明白吗?

【讨论】:

  • 你不考虑案例$i == 50$i == 100
  • 我不必 > 意味着超过 x。而
  • 真的吗?你在哪里看到的?
【解决方案3】:

将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
    }
}

【讨论】:

  • 谢谢!这解决了问题。 :)
  • @M42,我想你的意思是在函数内部做 $i
【解决方案4】:

循环内的所有 continue 似乎都是不必要的。如果你只是想处理整个结果集并分块做某事,你可以这样做

$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 循环中做同样的事情

【讨论】:

  • 是的。我在这个 for 循环中使用批处理和数据库插入/更新。
  • @user1043744,好的。如果您正在执行批量插入,您仍然可以在 while 循环中处理您的行(无需额外的 for 循环)。当计数器达到 50(使用 $cc % 50 == 0 进行测试)时,进行批量插入。 while 循环完成后,再进行一次批量插入以获取其余部分。
  • 我明白了。但我需要限制批次。我已经使用 M42 的解决方案来让这个东西正常工作。也感谢您的时间。我很感激! :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-10
  • 1970-01-01
  • 2011-08-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多