继续使用 mysql_fetch_assoc
我有几行代码,但找不到正确使用它的正确方法。
$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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
将 for 循环移到函数中并在 while 循环之后调用它:
Move the for loop in a function and call it after the while loop:
试试这个:
所以所有结果都会经历这个循环。如果 $i 小于 50(或者如果结果小于 50),则执行一些代码,或者如果 $i 大于 50 但小于 100,则执行一些不同的代码。最后,如果 $i 大于 100,则执行一些其他代码。
你明白吗?
Try this:
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?
你可以尝试:
You could try:
循环内的所有继续似乎都是不必要的。如果您只是尝试处理整个结果集并分块执行某些操作,那么您可以这样做,
但我确实同意@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
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