I have array like shown in code, and I want it to increment to first if user A views it, then increment again if user B views it, and so on(Remove the first index value: icufxzmpra when user A views so when other users views then get next index value from array)ā¦ so every user gets the unique code from the list of array. what modification can be done to achieve that?
function provideCode() {
static $counter=0;
$codes = array('icufxzmpra', 'iccegxzmea', 'icufrzspsa', 'icerghtjkl', 'icqwderthg', 'icrgtlokhz','icqwdfrtgh','ickihjbngh','icjfhzudcd','icjhnbhzgs','ictgsvcbhg');
$code = $codes[$counter];
echo "\n Please copy the following code and paste it in the task on ***** to successfully complete the task : ";
echo $code;
++$counter;
}
I think you will need to store the counter somewhere, perhaps in a session variable. Declaring it as static will be fine if the same process calls the function multiple times, but each time a new instance of your PHP code is created by a user running it, the function is run for the first time and therefore presents the first array element.
Could you use user IDs as array keys? Maybe that would work.
Though Iām curious about what your objective is here. Do those strings mean anything or are you just wanting to assign a random unique code string to each user? There may be better ways to do that.
sessions may be inappropriate for data shared across multiple users. i would prefer a database that gives you ACID capability. at least you can just read the keys from a file via file(), then remove that key and write the contents back.
I wanted to assign each one unique key from the list of array. so there is no any chance that anyone can do any fraud. but finally I found solution. I am storing all information in database and then fetching the IDs from table and then deleting that entry. Hope this will work fine .