dongyipa0028 2017-02-12 18:22
浏览 29
已采纳

有没有办法在HTML文档(或PHP脚本)中携带变量?

This is probably more of a 'programming technique' question, but I'm not sure which section of SO that should go to.

I am trying to make a website of "guided questions and (available) answers" - probably easily described structurally like a 'Choose Your Own Adventure' type approach.

Rather than embarking on creating an intricate tree that outlines each and every "storyline branching" and then hard-coding the (a href) to match, I was thinking of a more dynamic "journey" which begins with an array variable that outlines the sequence of the questions that will be asked.

for example, if a user chooses Sequence A; $questionarray = [Q1,Q3,Q2,Q5,Q6] if a user chooses Sequence B; $questionarray = [Q1,Q2,Q4,Q3,Q7] and so on.

My first attempt was to try something simple, just have it in sequence like [Q1,Q2,Q3,Q4,Q5] in which case I wouldn't need an array but a simple incrementing variable, say $qnum, and each subsequent file would call the next, like so - Q1.htm calls Q2.htm, Q2.htm calls Q3.htm, but not because it has been hard-coded in each file but determined by the incrementing 'unifying variable' of $qnum. (the thought was that, if that succeeded, it would then be a simple case of replacing $qnum with the afore-mentioned array variable '$questionarray'.)

The problem then arose that over several documents, HTML or even PHP, $qnum couldn't be "carried across" to the next file - without writing to, say, a textfile on the server-side to keep track.

I even tried to change the above 'sibling call sibling' structure to 'parent call child' (via "include Q".$qnum.".php") but the $_POST/document.reload also ends up resetting the $qnum back to declared value.

Is there another method I could use that I'm not aware of - aside from writing to/reading from a server-side text file, whichh would be quite inefficient, right?

  • 写回答

2条回答 默认 最新

  • douti9286 2017-02-12 19:32
    关注

    In Php you can use Sessions to keep track of data from one request to the next.

    you will need to start a session first and then you can get/set data in the $_SESSION array.

    For example:

    On the first page

    <?php
    
    session_start();
    
    $_SESSION['questions'] = array(1,2,4,3,7);
    
    ?>
    

    On a secondary page

    <?php
    
    session_start();
    
    //Replace with whatever logic is needed
    foreach($_SESSION['questions'] as $q){ 
      echo $q   
    }
    

    Note you should only start a session once per request. To check if a session is already started it is recommended you do the following:

    if (session_status() == PHP_SESSION_NONE) {
      session_start();
    }
    

    If you need to delete data from the session you can go about it in two ways:

    To delete an index in the $_SESSION array

    unset($_SESSION['questions']);
    

    To destroy the entire session

    session_destroy();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效