How to Dynamically assign request id to any array in PHP. e.g. $_SESSION[] or a simple array?
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.
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
}
}
}
精彩评论