开发者

How do I preserve this variable through multiple function calls?

开发者 https://www.devze.com 2023-03-05 02:00 出处:网络
I have this code: $k = 0; //loop through every question present in query results and run function to present the different question structures

I have this code:

$k = 0;
    //loop through every question present in query results and run function to present the different question structures
    while ($qs = mysql_fetch_assoc($get_questions))
    {
        $type = $qs['item_type'];
        $item_id = $qs['item_id'];
        $question = $qs['question_text'];
        $question_2 = $qs['question_text_2'];
        present_question($item_id, $type, $question, $question_2, $i, $k);
        $i ++;
        $ids[] = $item_id;
    }

now skipping out the switch case in the middle it can end up at this function:

function multi_response($data, $ID, $k){
    $j = 1;     
    while ($answers = mysql_fetch_assoc($data))
        {       
            $as = $answers['text_value'];
            echo "<input name='multi_response[$k][id]' type='hidden' value='$ID'>";
            echo "<strong>".$j.  ".</strong><input type='checkbox' name='multi_response[$k][answer]' value='$as'> $as</br>";
            $k++;
            $j++;
        }       
    return;
}

What I want to do is essentially everytime multi_response() is called, $k is to continuing from where it was last instead of from 0. $k开发者_开发百科 is basically my index value and if it resets to 0 it is overwriting the previous data in the array multi_response[][]

I have been trying to return $k to the original loop and parse it back through with no luck.


Send $k by reference:

function multi_response($data, $ID, &$k){
   //rest of function here ...
}
0

精彩评论

暂无评论...
验证码 换一张
取 消