繁体   English   中英

如何将变量从一个PHP文件传输到另一个?

[英]How to transfer variable from one php file to another?

这个问题可能会被标记为重复,但是我没有找到其他答案。

我正在创建一个用户可以回答多种类型问题的网站。 因此,我根据问题的类型编写了代码,以便相应的html模板出现。 现在我的PHP代码如下:

<?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']++;
?>

我想做的是在$ type =='1'时显示以下html模板。

<?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>

我的问题是,如何使它出现在问题中。 现在$ _SESSION ['answering'] ['questions'] [$ x]; 未定义。如何将其从第一个文件传输到第二个文件?

删除$x ,然后尝试:

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

$_SESSION['answering']['questions'];
包含项目列表 ,您必须指定应显示的项目。

如果您需要最后一个索引,这是简单(难看)的解决方案:

<?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>

我解决了问题。 现在,我的第二个文件如下:

<?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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM