2014-06-06 29 views
0

我想解決一個PHP的練習,我不知道如何顯示網站的不同部分。我知道你可以使用開關包含其他頁面(.php)。php會話和不同的頁面瀏覽量

基本上,如果你訪問網站的第一次,你會看到消息:孩子無聊,帶他們去公園(鏈接),按下鏈接後,您將獲得進入公園的景色和它的消息顯示球圖片和三個孩子的照片。如果你點擊球,一個孩子(隨機選擇)會得到一個接球,你將被帶回家/默認視圖。捕獲量將保存在會話中,所以如果你回到公園,你會看到誰抓到了球,你可以重複這個過程。你能給我一些提示怎麼做嗎?由於

<!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> 
+0

我會說使用一個cookie,而不是一個會話 – meda

回答

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>"; 
} 

更改這兩個概念,以適應你想要實現的。

+0

是的,我明白了會議是如何工作的,但我的主要問題是我不知道如何顯示網頁的部分又名如果會話模式?=公園則只顯示帶兒童圖片和球的公園段落,並隱藏其他一切。 – user2488832

+0

它與上面的代碼具有相同的概念。我爲你補充一點。 – Bioto

+0

好吧,生病試試吧 – user2488832

-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> 
+0

據我所知,這部分是爲了扔球,然後節省捕獲。我的主要問題是,雖然我不知道如何包含/顯示_孩子們感到無聊,請他們停下來(鏈接)_第一次你在現場時,沒有別的。然後,點擊鏈接(地址欄中的模式更改)後,您需要將頁面停放在圖片的位置。 – user2488832

+0

您需要說明您所做的更改...... – Runcorn

+0

JavaScript代碼的優點:window.location =「yourlink?child =」+ prob;你可以發送任何變量到任何頁面。從那裏你可以通過使用php來處理它,並將它作爲一個GET變量來處理。使用php關鍵字:$ _GET ['the variable name'];從這裏您可以將其設置爲變量,會話,索引或任何您需要的內容。 –

相關問題