views:

27

answers:

1

Hi friends I am stuck with a program logic in array. Basically, what am trying to do is that I have link that says "Unlock answer" use clicks on this link and then I get the request id and unlock the specific answer. But I have other answers there which needs to be unlocked. And user again and again clicks on any answer to unlock. But am able to unlock only one answer due to the reason that I only get one request ID. I am stuck that how can I unlock any no of answers with each time request id and the unlocked answer stayed there and the new one also unlocked.

Please help me solve this problem.

A: 

There are a couple ways you can do it. The simplest would be to have session arrays $_SESSION['locked'] and $_SESSION['unlocked']. Whenever you receive a new request ID, add that entry to $_SESSION['unlocked'] and remove it from $_SESSION['locked']. Then you can simply read either session array to determine whether the answer should be displayed or remain hidden.

Another way of doing it would involve essentially the same logic, but using a database table to keep track of what is locked or unlocked for each user.

If the complete list of possible request IDs is static, you can even avoid tracking which answers are locked, and simply keep a Session array (or database table) for which ones have been unlocked for which users.

The following is the general idea:

if(isset($_POST['requestid'])){
    $_SESSION['unlocked'][] = $_POST['requestid']
}

foreach($answers AS $answer){
    foreach($_SESSION['unlocked'] AS $requestid){
        if($answer['requestid'] == $requestid){
            //output revealed answer
        } else{
            //output locked answer
        }
    } 
}
DeathMagus
I have a while loop where all question are being displayed and I get the question id from loop and then get the Id of requested unlocked id for an answer. by matiching if (requestid==dbtableID) then unlock but it always unlock one item because I have only one request ID. I want to store all request ID into session array or any other array.
junjua
Right - the user can only request one unlock at a time, but you want the page to remember previous unlocks and store them as well, correct? In that case, as I said above - when an unlock request is processed, save it in a persistent session array. Whenever a new unlock request is made, display both that request and any previous requests stored in the array as unlocked.
DeathMagus
okay great you understand my issue well. Can you please tell me how can I do this am stuck how can I do this storing values in session prev ids and new ones. Please help thanks a lot.
junjua
Pseudo-code now posted in original answer.
DeathMagus