movieprodw
asked on
PHP foreach select range to show
Hello,
I have this:
foreach ($customers as $customer){
and I only need the 500th - 1000th records in that array, how do I only foreach those?
Thanks
I have this:
foreach ($customers as $customer){
and I only need the 500th - 1000th records in that array, how do I only foreach those?
Thanks
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Maybe there is a way to delete the first 500 out of the $customers array? then I could just tell it to stop when $i = 1000
Is that possible?
Is that possible?
Yes, array_slice() can remove parts of an array.
ASKER
Thanks Ray... did that at apparently it is an object, can you do the same with an object?
$customers = array_slice($customers, 0, 5);
$customers = array_slice($customers, 0, 5);
You might also consider a for() statement.
http://php.net/manual/en/control-structures.for.php
Something like this:
http://php.net/manual/en/control-structures.for.php
Something like this:
for($i = 500; $i <= 1000; $i++) {
/* Do Stuff */
}
No, you cannot use PHP array functions on objects directly, but you can often cast objects as arrays and then use array functions. You can find the data types with var_dump()
ASKER
Okay, trying to work with Magentos objects to update 20k passwords but it keeps timing out, it is super slow.
I figured if I could do 500 at a time it would be great, but it had to load the first 500 to get to the second 500, takes forever.
I was hoping that I could just start at the 500th and run to the 1000th, then 1000th to 1500th
I figured if I could do 500 at a time it would be great, but it had to load the first 500 to get to the second 500, takes forever.
I was hoping that I could just start at the 500th and run to the 1000th, then 1000th to 1500th
Forgive me yes you had to put $i++
You can increase the PHP time limit. Try putting set_time_limit(2) inside the loop. But I'm concerned that 20K of anything should not be too slow -- 20K is not a big number in computer science. Are there any things that slow this process down unnecessarily?
ASKER
I agree, 20k should be lightening.
Do you see any red flags in my code below? I was just running 50 for the test, it seems to take about 30 seconds for 50.
Do you see any red flags in my code below? I was just running 50 for the test, it seems to take about 30 seconds for 50.
// LOAD CUSTOMER COLLECTION
$customers = Mage::getModel('customer/customer')->getCollection();
// LOAD VARIABLES
$count = $_GET['count'];
$countplus = $count + 50;
$basenumber = '0';
// LOAD FILE
$file = 'account_passwords.csv';
$current = file_get_contents($file);
// LOAD CUSTOMER INFO
foreach ($customers as $customer){
if($basenumber > $count) {
if($basenumber > $countplus) {
file_put_contents($file, $current);
echo '<script type="text/javascript">
window.location = "/resetallpasswords.php?count='.$countplus.'"
</script>';
echo 'orange';
exit();
} else {
$password = strtoupper(substr( $customer->getEmail(), 0, 3)).rand(111,999);
$customer->setPassword($password)->save();
$current .= $customer->getGroupId(). ",". $customer->getEmail(). ",". $customer->getPassword()."\n";
}
}
$basenumber++;
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for the help guys, I could not get it to work, ended up just inserting it all into the system using 2 scripts, one to make the passwords then another to add them in the db.
Thank you for the help, I learned some stuff
Thank you for the help, I learned some stuff
ASKER
I am trying a similar way but the issue is that calling to load each '$customer' is very slow, so I have 20k results and it has to load the first 500 each time I do that. It would be great if I could just start at 500