powelly Posted March 16, 2007 Share Posted March 16, 2007 I have the following file that contains stocknumbers and stock levels "109",0 "10956",1 "10988",0 "1099",0 "11",0 "110",0 "11064",2 "111",0 "11128",18 "11138",0 "11157",0 "11197",6 "112",0 "11256",0 "11275",0 "11298",0 "113",0 "11334",0 "11375",12 the file is an output from an EPOS system and therefore cannot really be changed, what Im looking to do is pass a variable to a php file in the url to retrieve the stock level of an item. ie lookupstock.php?plu=11128 script looks up the plu number then returns the stock level in a variable echo "PLU=".$stocklevel; Hope this makes sense, if anyone can point me to a tutorial that can get me started it would be appreciated. Thanks Link to comment https://forums.phpfreaks.com/topic/42974-find-a-result-within-a-text-file/ Share on other sites More sharing options...
Orio Posted March 16, 2007 Share Posted March 16, 2007 Let's say we have a file called data.txt that contains: "109",0 "10956",1 "10988",0 "1099",0 "11",0 "110",0 "11064",2 <?php function get_data($num) { $num = '"'.$num.'"'; $lines = file("data.txt"); foreach($lines as $line) { list($stock, $val) = explode(",", $line); if($num == $stock) return trim($val); } return FALSE; } $values = array(10956, 110, 99999); foreach ($values as $value) { $data = get_data($value); if($data !== FALSE) echo $data; else echo "Not found"; echo "<br>"; } /* Output- 1 0 Not found */ ?> Orio. Link to comment https://forums.phpfreaks.com/topic/42974-find-a-result-within-a-text-file/#findComment-208740 Share on other sites More sharing options...
boo_lolly Posted March 16, 2007 Share Posted March 16, 2007 would it not be the same as this? <?php $_GET['plu'] = $num; function getLevel($num){ $fp = fopen("data.txt", "r"); while(!feof($fp)){ $line = fgets($fp, 20); list($stock_level, $val) = split(",", $line); $stock_level = str_replace('"', '', $stock_level); (($stock_level == $num) ? (return $stock_level) : (return FALSE)); } } ?> i'm sure orio's got a better understanding of what's going on here than i do... but don't see why this wouldn't work either. what do you think, orio? EDIT: there's a problem with my 'if' statement... it will only return the value if it is the last line read... how would i make it return the value (and true) if it passed the 'if' statement at any point throughout reading the flatfile? you know, just a little coding advice (slightly unrelated to the topic). any suggestions? Link to comment https://forums.phpfreaks.com/topic/42974-find-a-result-within-a-text-file/#findComment-208759 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.