2011-05-10 32 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

相关问题