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条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?