etrader Posted June 9, 2011 Share Posted June 9, 2011 I want to store a list of strings in a file (to be called as an array) instead of database. Imagine that I have a string come out a search process $q = $_GET['q']; I can simply write the search history in a text file and call it by file() But I want to assign a unique ID to each search term as 1:word1 2:wordx 3:word... Now I can call each word by its given ID through an explode() process. But how I can assign the unique ID to each search-term? Quote Link to comment https://forums.phpfreaks.com/topic/238856-storing-a-unique-list-of-strings-in-an-array/ Share on other sites More sharing options...
WebStyles Posted June 9, 2011 Share Posted June 9, 2011 there's really no advantage in doing this with txt files, I would use a simple database with a uniqueID and a text field, but, if you insist: you can use a seperate file just to store the last id used, so it's easier to grab and increment: $lastID = file_get_contents('lastSearchID.txt'); $lastID++; file_put_contents('lastSearchID.txt',$lastID); or you can read the last line of your main .txt file, grab the id, increment it and then use it for next search. If you have many simultaneous connections, anticipate file_lock problems when two people are trying to write to it at the exact same time, etc. A Database is much better and faster for this. If you just write a small function to store the search string, the ID will be automatic function storeSearchString($string){ $conn = conn(); mysql_query("insert into `searchTracking` (`searchString`) values ('$string')",$conn); @mysql_close($conn); } hope this helps Quote Link to comment https://forums.phpfreaks.com/topic/238856-storing-a-unique-list-of-strings-in-an-array/#findComment-1227340 Share on other sites More sharing options...
AbraCadaver Posted June 9, 2011 Share Posted June 9, 2011 If you're tied to a text file then I would store an actual array using var_export() and then just include the file. Or you could serialize the array and then save to / read from the file. If you do this with multiple files or in multiple places maybe build two functions: //save include('file.txt'); $history[] = $new_item; file_put_contents('file.txt', var_export($history, true)); //read include('file.txt'); echo $history[1]; //or //save $history = unserialize(file_get_contents('file.txt')); $history[] = $new_item; file_put_contents('file.txt', serialize($history)); //read $history = unserialize(file_get_contents('file.txt')); echo $history[1]; Quote Link to comment https://forums.phpfreaks.com/topic/238856-storing-a-unique-list-of-strings-in-an-array/#findComment-1227503 Share on other sites More sharing options...
AbraCadaver Posted June 9, 2011 Share Posted June 9, 2011 Bah, I was over thinking. Just write them out to the file on separate lines without the number, then when you need them use file() as you suggested. They will have a unique number in the array. Quote Link to comment https://forums.phpfreaks.com/topic/238856-storing-a-unique-list-of-strings-in-an-array/#findComment-1227509 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.