我可能有一些愚蠢的问题。
这是我的代码:
$customers = Mage::getModel('customer/customer')
->getCollection()
->addAttributeToSelect('*')
->addFieldToFilter('group_id', $groupId);
$LimitLoop = 50;
foreach($customers as $customer)
{
$email=$customer->getEmail();
$CustomerPhone = $customer->getPrimaryBillingAddress()->getTelephone();
$CustomerName = $customer->getName();
$CustomerEmail = $customer->getEmail();
if($EnableSMSNotification==1 && $smstext!="") {
$api = new TextMagicAPI(array(
"username" => $TextMagicUsername,
"password" => $TextMagicPassword
));
// Use this number for testing purposes. This is absolutely free.
$phones = array($CustomerPhone);
$results = $api->send($smstext, $phones, true);
$write = Mage::getSingleton('core/resource')->getConnection('core_write');
$sql_insert = "insert into VivasIndustries_SmsHistory values ('','$CustomerName','$CustomerPhone','$CustomerEmail','$smstext',NOW())";
$write->query($sql_insert);
}
}
这是一个简单的foreach loop
。
如何将其限制为$LimitLoop
中指出的值?
如何在此循环中仅获取有限数量的结果?
提前致谢!
答案 0 :(得分:2)
如果使用$ awk -F'\t' '$3!="oil"{$0 = $1 FS $2 " " $3 FS $4} 1' file
12345 jones, michael madison 0101-10101-2
12345 water oil 0101-10101-2
12346 jones, mike mason 0101-10101-3
12347 jones, nick norris 0101-10101-4
循环是不可能的,那么您需要手动添加递增计数器以执行相同的逻辑。像这样:
for
答案 1 :(得分:1)
如果您使用类似
的内容,可以从其他示例中保存变量$ i$LimitLoop = 50;
foreach ($customers as $customer) {
/* your code to run */
/* ..... */
/* at the bottom */
$LimitLoop--;
if ($LimitLoop===0) {
break;
}
}
答案 2 :(得分:0)
这很简单:
$customers = Mage::getModel('customer/customer')
->getCollection()
->addAttributeToSelect('*')
->addFieldToFilter('group_id', $groupId);
$LimitLoop = 50;
$counter = 0; //Add a means of counting how many times it's been through the loop
foreach($customers as $customer)
{
$email=$customer->getEmail();
$CustomerPhone = $customer->getPrimaryBillingAddress()->getTelephone();
$CustomerName = $customer->getName();
$CustomerEmail = $customer->getEmail();
if($EnableSMSNotification==1 && $smstext!="") {
$api = new TextMagicAPI(array(
"username" => $TextMagicUsername,
"password" => $TextMagicPassword
));
// Use this number for testing purposes. This is absolutely free.
$phones = array($CustomerPhone);
$results = $api->send($smstext, $phones, true);
$write = Mage::getSingleton('core/resource')->getConnection('core_write');
$sql_insert = "insert into VivasIndustries_SmsHistory values ('','$CustomerName','$CustomerPhone','$CustomerEmail','$smstext',NOW())";
$write->query($sql_insert);
}
$counter++; //Increment the counter
if($counter >= $LimitLoop) break; //Break the loop if we have enough results
}
答案 3 :(得分:0)
您要么使用break
语句,要么使用最多for
的{{1}}循环。
for循环(我使用array_keys函数,$LimitLoop
可能是一个关联数组或缺少数字键等。索引键数组(将有数字排序)然后保证我们总是得到下一个关键):
$customers
注意:您还必须插入一些逻辑来检查您是否有50个客户使用上述循环,否则它将开始抛出有关访问阵列中无效偏移的警告。
符:
$keys = array_keys($customers);
for ($i = 0; $i < $LimitLoop; $i++) {
$customer = $customers[$keys[$i]];
// the rest of your code
}