dshgdhdfcas30210 2014-04-23 08:16
浏览 166
已采纳

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

This question will probably be marked as duplicate, but I didnt find other answers helpful.

I am creating a site where user could answer multiple type of questions. Thus i wrote code so the corresponding html template will appear, according to type of question. Right now my php code is following:

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

And what I want to do is to make following html template appear, when $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>

My question is, how could I make it appear with question. Right now $_SESSION['answering']['questions'][$x]; is undefined.How could I transfer it from first file to second?

  • 写回答

3条回答 默认 最新

  • doumei7420 2014-04-23 08:27
    关注

    $_SESSION['answering']['questions'];
    contains a list of items, you have to specify which one should be displayed.

    In case you need the last index, this is simple (and ugly) 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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持