Jump to content

Proper way to save your place looping an array/db


digibucc

Recommended Posts

Ok, I can throw actual code here but it's all rather long, so i am going to give an example:

<?php
$max = 10; // max runs per exec
$arr = array_fill(0, 50, 0);
$last = file_get_contents('last.txt');
$n = 0;

echo 'left off on '. $last;

while ($n < $max) {
 foreach ($arr as $k => $v) {
  if ($k > $last) {
   // do something with the value ($v)
   $n ++;
  }
 }
}

file_put_contents(last.txt, $k));
?>
<meta http-equiv="refresh" content="3">

this isn't anything i use, but i do something similar when i want to save where i left off in a program to come back to later. that way if the program crashes or i have to close it, i haven't lost my place. this is normally when working with database records. it makes it run incrementally and as i said save it's place to return to.

 

it just feels wrong though, and i'm sure it is. especially the refresh. what is the best way to do this?

 

I would use array_slice instead of if ($k > $last), but that is normally comparing row values from a db, so i can't just arbitrarily slice that.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.