【问题标题】:php sessions and different page views [closed]php会话和不同的页面视图[关闭]
【发布时间】:2014-06-06 18:34:38
【问题描述】:

我正在尝试解决一个 php 练习,但我不知道如何显示网站的不同部分。我知道您可以使用 switch 包含其他页面(.php)。

基本上,如果您第一次访问网站,您会看到消息:孩子们很无聊,带他们去公园(链接),点击链接后,您将进入公园视图,消息是显示有球图片和三个孩子的图片。如果您点击球,一个孩子(随机选择)将获得 1 次接球,您将被带回主页/默认视图。接球将在会话中保存,所以如果你回到公园,你会看到谁接住了球,你可以重复这个过程。你能给我一些建议吗?谢谢

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8" />
        <title></title>

    <style>
    #children {
        float:left;
        margin: 0px 100px;
    }
    #ball {
        margin:100px;
    }
    </style>
    </head>
    <body>

    <!-- Use this to make different page views -->

    <!-- home -->
    <p>Children are bored, take them to <a href="?mode=park">park</a></p>

<!-- park -->
<p>For children to be more fun, throw them a ball, you can also go <a href="?mode=home">back home</a></p>
<div id="children">
    <!-- Repeat this part to make 3 children -->
    <div>
        <img src="child.png" alt="child" width="100"/><br/>
        here child's name -  here amount of balls child has caught.
    </div>
    <!-- end -->
</div>
<div id="ball">
    <a href="?mode=throw">    <!--after throwing,  add 1 catch to random child and return home page-->
        <img src="ball.png" width="100" alt="ball" />
    </a>
    </div>



    </body>
    </html>

【问题讨论】:

  • 我会说使用cookie而不是会话

标签: php session switch-statement case


【解决方案1】:
<?php
session_start();
$_SESSION['counter']++;

if($_SESSION['counter'] > 0 && $_SESSION['counter'] < 5) {
    // Blah
}else if($_SESSION['counter'] > 5 && $_SESSION['counter'] < 10) {
    // Blah
}

启动会话 -> 将会话变量增加一(对于页面视图)-> 根据变量的值它做不同的事情

编辑:

if($_SESSION['mode'] == "park") {
    echo "<park image>";
}else if($_SESSION['mode'] == "city") {
    echo "<city image>";
}else {
    echo "<default image>";
}

更改这两个概念以适应您想要实现的目标。

【讨论】:

  • 是的,我了解会话是如何工作的,但我的主要问题是我不知道如何显示网站的某些部分,也就是如果 SESSION 模式?=park 然后只显示带有儿童图片的公园段落和一个球并隐藏其他一切。
  • 和上面代码的概念是一样的。我会为你添加。
  • 你不必使用 echo 语句(特别是如果你有多行 html)你可以做类似
【解决方案2】:
<?php
session_start();

if($_GET['child'] == null){
// set a counter for each child for one time only.
$_SESSION['counter'] = array(1 => 0, 2 => 0, 3 => 0); 
}

if($_GET['child'] != null){
  $childIndex = $_GET['child'];
  $_SESSION['counter'][$childIndex]++;
}

$_prob = rand(1, 3);    // set probability for choosing the child image every time the page is loading. 


?>

// in the ball tag you can insert the increment within that using javascript . something like: 

<div id="ball">
    <a href="?mode=throw">    
    <!--after throwing,  add 1 catch to random child and return home page-->
        <img src="ball.png" width="100" alt="ball" onclick="increment(<?php echo $_prob; ?>);"/>
    </a>
 </div>
<script>
  function increment(prob){
     window.location="yourlink?child="+prob;
  }
</script>

【讨论】:

  • 据我了解,这部分是用于投球然后保存接球。不过,我的主要问题是,我不知道如何包含/显示孩子们很无聊,带他们去公园(链接) 第一次你在现场,别无其他。然后点击链接后(地址栏模式改变),会带你到有图片的页面。
  • 你需要说明你做了什么改变......
  • javascript 代码的优点:window.location="yourlink?child="+prob;您可以将任何变量发送到任何页面。从那里您可以使用 php 处理它并将其作为 GET 变量使用。使用 php 关键字:$_GET['变量名'];从这里您可以将其设置为变量、会话、索引或您需要的任何内容。
  • 但我只有一页。基本上我必须根据我所处的模式隐藏一些网站正文。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-09-18
  • 1970-01-01
  • 1970-01-01
  • 2011-08-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多