2011-05-10 34 views
0

我有這樣的代碼:如何通過多個函數調用來保存這個變量?

$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; 
    } 

現在跳過了中間的開關情況下,它可以在這個函數結束:

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

我想要做的基本上是每次multi_response()被調用時, $k將從最後一次而不是從0繼續。$k基本上是我的索引值,如果重置爲0,它將覆蓋陣列中的先前數據multi_response[][]

我一直試圖將$k返回到原來的循環,並解析它通過沒有運氣。

回答

2

發送引用$ K:

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

u能解釋這樣對我? – buymypies 2011-05-10 14:37:25

+0

http://php.net/manual/en/language.references.pass.php @Emil只是輸入:) – Chris 2011-05-10 14:38:49

+2

你可以閱讀手冊中的參考資料:http://php.net/manual /en/language.references.php – 2011-05-10 14:38:59

相關問題