Apenas o último e-mail da matriz de arquivos sendo carregado [fechado]
-
12-12-2019 - |
Pergunta
Estou com o seguinte problema:Eu tenho uma lista de e-mails em um arquivo.Quero percorrer isso e criar clientes, depois quero fazer algo com o cliente.Porém no meu script está carregando apenas o último cliente.
Eu tenho o seguinte código:
require_once("./app/Mage.php");
Mage::app();
$file = fopen("Test.txt", "r");
if ($file) {
$emails = file("Test.txt");
foreach($emails as $email) {
echo $email . "<br/>"; // < shows all emails
// load up the customer
$customer = Mage::getModel('customer/customer')
->setWebsiteId(1)
->loadByEmail($email);
echo $customer->getId() . "<br/>"; // < returns blank except for the last email line
// if we found the customer
if ($customer->getId()){
echo $customer->getId() . "adsf<br/>"; // < only shown on last line
//.. do something
}
}
fclose($file);
} else {
// error opening the file.
}
Eu também tentei
while(!feof($file)){
$email = fgets($file);
e
while (($email = fgets($file)) !== false)
mas o mesmo resultado, então parece que não há algo errado com o código php.Alguém pode me ajudar?
Solução
Abaixo deverá ler seu arquivo linha por linha e carregar os clientes.Já tive problemas antes em que arquivos de texto salvos no Windows tinham finais de linha ilegíveis.Certifique-se de que todos os endereços de e-mail não sejam exibidos em uma linha
if ($file = fopen("Test.txt", "rfile)) {
while (($email = fgets($handle)) !== false) {
echo $email . "<br/>"; // < shows all emails
// load up the customer
$customer = Mage::getModel('customer/customer')
->setWebsiteId(1)
->loadByEmail(trim($email));
echo $customer->getId() . "<br/>"; // < returns blank except for the last email line
// if we found the customer
if ($customer->getId()){
echo $customer->getId() . "adsf<br/>"; // < only shown on last line
//.. do something
}
}
fclose($file);
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a magento.stackexchange