meltingpoint Posted January 6, 2011 Share Posted January 6, 2011 example structure of text database- mytextfile.txt 10-11201|2010/09/01|Sam|Thurston 10-11307|2010/09/04|Tony|Piper 10-11405|2010/09/11|Sarah|Smith <?php $file2 = 'mytextfile.txt'; $openedfile =fopen($file2, "r") or die("ERROR- could not open file for editing."); // flock($openedfile, LOCK_EX) or die("Error!- Could not obtain exclusive lock on the file to edit."); $hold[$record_count] = explode("|", trim(fgets($openedfile))); while(!feof($openedfile)) { $record_count++; $hold[$record_count] = explode("|", trim(fgets($openedfile))); } ?> What I need to do is loop through this and one by one- take any arrays that contain a date that is between $dateX and $dateY (which comes via a form input) and place it in a new array $matched. I am stumped. 1-Is there a way to do it inside the above while loop that I am not seeing? 2- Do I need to open the file in another manner? 2- Would it be best to now loop through $hold[$record_count] I am trying to keep the process short so as not to use up too much memory. Point me in the right direction for this one please. Quote Link to comment https://forums.phpfreaks.com/topic/223611-loop-through-mulit-dimension-array-and-match-dates/ Share on other sites More sharing options...
PFMaBiSmAd Posted January 6, 2011 Share Posted January 6, 2011 1-Is there a way to do it inside the above while ^^^ Yes. If the only processing you will do on the data is to retrieve matching records, so that you don't actually need to store all the records in memory, then yes, you should add a conditional test inside of the while(){} loop so that you only keep the matching records. If all your date values in the file are in the same exact format yyyy/mm/dd, with leading zeros in the mm and dd values and the $dateX and $dateY are both also in that same exact format, then you can compare the date values directly using greater-than/less-than comparisons and form an if(){} statement that only saves records that have a date field between the $dateX and $dateY values. Quote Link to comment https://forums.phpfreaks.com/topic/223611-loop-through-mulit-dimension-array-and-match-dates/#findComment-1155859 Share on other sites More sharing options...
meltingpoint Posted January 6, 2011 Author Share Posted January 6, 2011 Wonderful. Yes- the database dates and form dates are formated the same. Yes- I need to do additional things with the remaining data- that is why I want to save those records in an array $matched so that I can do other things with them and then output the results. Problem is that the first iteration: $hold[$record_count] = explode("|", trim(fgets($openedfile))); takes the $openedfile and puts the first line into $hold[1] and then continues on until the end of the file with the loop. So the first line is not counted in the conditional statement. Also- if there is no record that INITIALLY meets the conditional statement inside the while loop- it does not reach the $record_count++. Here is what I have tried. $file = 'mytextfile.txt'; $openedfile =fopen($file2, "r") or die("ERROR- could not open file for editing."); // flock($openedfile, LOCK_EX) or die("Error!- Could not obtain exclusive lock on the file to edit. Please try again"); $hold[$record_count] = explode("|", trim(fgets($openedfile))); while(!feof($openedfile)) { if($hold[$record_count][1] >= $dateX && $hold[$record_count][1] <= $dateY) { $record_count++; $hold[$record_count] = explode("|", trim(fgets($openedfile))); } There is much wrong with this- but I am stumped how to: A- discard data not needed and B- Create new $matched array of data that meets the requirement. Quote Link to comment https://forums.phpfreaks.com/topic/223611-loop-through-mulit-dimension-array-and-match-dates/#findComment-1155871 Share on other sites More sharing options...
meltingpoint Posted January 7, 2011 Author Share Posted January 7, 2011 **Could really use the help on this one. Anyone? Quote Link to comment https://forums.phpfreaks.com/topic/223611-loop-through-mulit-dimension-array-and-match-dates/#findComment-1156067 Share on other sites More sharing options...
PFMaBiSmAd Posted January 7, 2011 Share Posted January 7, 2011 <?php $file2 = 'mytextfile.txt'; $openedfile = fopen($file2, "r") or die("ERROR- could not open file for editing."); flock($openedfile, LOCK_EX) or die("Error!- Could not obtain exclusive lock on the file to edit."); $dateX = '2010/09/04'; // simulate test data $dateY = '2010/09/04'; while(!feof($openedfile)){ $record = explode("|", trim(fgets($openedfile))); if($record[1] >= $dateX && $record[1] <= $dateY){ $matched[] = $record; } } echo '<pre>',print_r($matched,true),'</pre>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/223611-loop-through-mulit-dimension-array-and-match-dates/#findComment-1156177 Share on other sites More sharing options...
meltingpoint Posted January 7, 2011 Author Share Posted January 7, 2011 Thanks- when I get off work I will give it a go. Much thanks! Quote Link to comment https://forums.phpfreaks.com/topic/223611-loop-through-mulit-dimension-array-and-match-dates/#findComment-1156458 Share on other sites More sharing options...
meltingpoint Posted January 8, 2011 Author Share Posted January 8, 2011 That worked perfectly. Thanks a million. Quote Link to comment https://forums.phpfreaks.com/topic/223611-loop-through-mulit-dimension-array-and-match-dates/#findComment-1156686 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.