php会话和不同的页面浏览量

时间:2014-06-06 18:34:39

标签: php session switch-statement case

我试图解决一个PHP练习,我不知道如何显示网站的不同部分。我知道你可以使用开关包含其他页面(.php)。

基本上,如果您第一次访问网站,您会看到消息:孩子们感到无聊,带他们去停车(链接),按下链接后你会进入公园景观并且#39 ; s消息显示有球图片和三个孩子的图片。如果你点击球,一个孩子(随机选择)将获得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>

2 个答案:

答案 0 :(得分:1)

<?php
session_start();
$_SESSION['counter']++;

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

开始会话 - &gt;将会话变量增加一个(用于页面视图) - &gt;取决于变量的值,它做了不同的事情

编辑:

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

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

答案 1 :(得分:-1)

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