2014-04-23 6 views
1

Cette question sera probablement marquée comme doublon, mais je n'ai pas trouvé d'autres réponses utiles.Comment transférer une variable d'un fichier php à un autre?

Je crée un site où l'utilisateur pourrait répondre à plusieurs types de questions. J'ai donc écrit du code pour que le modèle html correspondant apparaisse, selon le type de question. En ce moment, mon code php suit:

<?php 
include_once 'init/init.funcs.php'; 
$_SESSION['pollid']=(int) $_GET['pollid']; 
$questions = array(); 
$answer = $_POST['answer']; 
if (!isset($_SESSION['answering'])) { 
    $result = mysql_query('SELECT * from katse_kysimused where kysimustik_id="' . $_SESSION['pollid'] . '"'); 
    while($row = mysql_fetch_assoc($result)) { 
     $questions[] = $row['kysimus']; 
     } 
    $_SESSION['answering']['questions'] = $questions; 
    $_SESSION['answering']['index'] = 0; 
} 
    $x = $_SESSION['answering']['index']; 
    $result3 = mysql_query('SELECT tyyp_id FROM katse_kysimused where kysimus= "' . $_SESSION['answering']['questions'][$x] . '"'); 
    $type = mysql_result($result3, 0); 
    if ($type=='3'){ 
     echo $_SESSION['answering']['questions'][$x]; 
     $result4 = mysql_query('SELECT kysimus_id FROM katse_kysimused where kysimus= "' . $_SESSION['answering']['questions'][$x] . '"'); 
     $question_id = mysql_result($result4, 0); 
     $result5 = mysql_query('SELECT * from katse_valik_vastused where kysimus_id="' . $question_id . '"'); 
     if($result5 === FALSE) { 
      die(mysql_error()); 
     } 
     while($row = mysql_fetch_assoc($result5)) { 
      $options[] = $row['vasuts']; 
     } 
     foreach($options as $option=>$option_value) { 
      echo $option_value; 
} 
     } 

    if ($type=='1'){ 
      echo "<meta http-equiv='refresh' content='0;url=http://localhost/Praks/tekstkysimusele_vastamine2.php'>"; 
     } 
if(isset($_POST['submit'])){ 
    $result2 = mysql_query('SELECT kysimus_id FROM katse_kysimused where kysimus= "' . $_SESSION['answering']['questions'][$x -1] . '"'); 
    $q_id = mysql_result($result2, 0); 
    mysql_query('INSERT INTO katse_vastused2 (id, vastus,kysimus_id, vastustik_id) VALUES (NULL,"' . $answer . '","' . $q_id . '","1")'); 
    } 
$_SESSION['answering']['index']++; 
?> 

Et ce que je veux faire est de modèle html suivant apparaît, quand $ Type == « 1 ».

<?php 
echo $_SESSION['answering']['questions'][$x]; 
?> 
<html> 
<br> 
<form method="post" action="answering.php"> 
<input type="text" name="answer" style="width:300px; height:150px;"><br> 
<input name= "submit" type="submit" value="Vasta"> 
</form> 
</html> 

Ma question est, comment pourrais-je le faire apparaître avec question. En ce moment $ _SESSION ['répondre'] ['questions'] [$ x]; est indéfini.Comment pourrais-je le transférer du premier fichier à la seconde?

+0

Attention à l'injection SQL. –

+0

Comment appelez-vous le second fichier? –

+0

Je l'appelle 'tekstkysimusele_vastamine2.php'. – user244902

Répondre

0

$_SESSION['answering']['questions'];
contient une liste d'articles, vous devez spécifier lequel doit être affiché.

Si vous avez besoin le dernier indice, est simple (et laid) solution:

<?php 
    $last_index = count($_SESSION['answering']['questions']) -1; 
    echo $_SESSION['answering']['questions'][$last_index]; 
?> 
<html> 
<br> 
<form method="post" action="answering.php"> 
<input type="text" name="answer" style="width:300px; height:150px;"><br> 
<input name= "submit" type="submit" value="Vasta"> 
</form> 
</html> 
0

Retirez le $x et puis essayez:

echo $_SESSION['answering']['questions']; 
0

Je résolu le problème. Maintenant, mon deuxième fichier est le suivant:

<?php 
include_once 'init/init.funcs.php'; 
$x = $_SESSION['answering']['index']; 
echo $_SESSION['answering']['questions'][$x]; 
?> 
<html> 
<br> 
<form method="post" action="answering.php"> 
<input type="text" name="answer" style="width:300px; height:150px;"><br> 
<input name= "submit" type="submit" value="Vasta"> 
</form> 
</html> 
Questions connexes