首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何判断它是否是时间循环中的最后一个?

问如何判断它是否是时间循环中的最后一个?
EN

Stack Overflow用户
提问于 2014-04-24 14:14:56
回答 2查看 96关注 0票数 0

例如iOS推送程序

代码语言:javascript
复制
$push = new ApnsPHP();//ApnsPHP push class
/*
  ...
 */

$limit = 1000;

//want not to use fetchAll() method so as to reduce php memory.
$stmt = $pdo->query('SELECT * FROM table');

$i = 0;
while ($row = $stmt->fetch()) {
  $send = true;
  ++$i;
  //adding a que by max $limit count
  $push->add($row['token'],$row['msg']);

  if($i % $limit === 0){
   $push->send();
   $send = false;

  }
}

//send rest que if remaining :#1
if($send){
 $push->send();
}

我认为在循环之后做同样的事情并不酷。

所以我想减少1号街区。

如果使用fetchAll()和foreach,那么在循环中判断最后一个是很容易的。但是,将大量的结果数据转换为数组和保存正在增加php内存。

有谁知道酷的路吗?

抱歉,英语坏了。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-04-24 14:24:16

试试这个,也许它能帮你:-

代码语言:javascript
复制
$stmt = $pdo->query('SELECT * FROM table');
$total_count = $stmt->rowCount();
$i = 0;
while ($row = $stmt->fetch())
{
    $send = true;
    ++$i;
    if($total_count == $i)
    {
        //Do Process For Last Record
    }
    //adding a que by max $limit count
    $push->add($row['token'],$row['msg']);

    if($i % $limit === 0){
        $push->send();
        $send = false;
    }
}
票数 0
EN

Stack Overflow用户

发布于 2014-04-24 14:27:19

使用$stmt->rowCount()计数选定的行。

代码语言:javascript
复制
$stmt = $pdo->query('SELECT * FROM table');

$i = 0;
$numRows = $stmt->rowCount();
while ($row = $stmt->fetch()) {
  $send = true;
  ++$i;

  if($numRows == $i){ 
    echo 'Last iteration';
  }

  //adding a que by max $limit count
  $push->add($row['token'],$row['msg']);

  if($i % $limit === 0){
   $push->send();
   $send = false;

  }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23271514

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档