Jump to content

Storing a unique list of strings in an array


etrader

Recommended Posts

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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];

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.