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.

Edited by digibucc
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.