Hoger/lager opdracht voor school
Zelf dacht ik hierbij aan een array, maar ik weet dan weer niet hoe ik bijvoorbeeld een sessie in een array kan opslaan. Kunnen jullie me hierbij helpen?
index.php
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?php
session_start();
if($_SESSION["name"])
header("Location:play.php");
?>
<!DOCTYPE html>
<html>
<head>
<title>HogerLager</title>
<meta charset="UTF-8">
</head>
<body>
<h1>HogerLager</h1>
<h2>Voer uw naam in</h2>
<form action="play.php" method="POST">
<input type="text" name="name">
<input type="submit" name="savename" value="Spelen!">
</form>
</body>
</html>
session_start();
if($_SESSION["name"])
header("Location:play.php");
?>
<!DOCTYPE html>
<html>
<head>
<title>HogerLager</title>
<meta charset="UTF-8">
</head>
<body>
<h1>HogerLager</h1>
<h2>Voer uw naam in</h2>
<form action="play.php" method="POST">
<input type="text" name="name">
<input type="submit" name="savename" value="Spelen!">
</form>
</body>
</html>
play.php
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?php
session_start();
if($_POST["name"]&&!$_SESSION["name"])
$_SESSION["name"]=$_POST["name"];
elseif(!$_POST["name"]&&!$_SESSION["name"])
header("Location:index.php");
if(!$_SESSION["numb"])
$_SESSION["numb"]=rand(1,1000);
?>
<!DOCTYPE html>
<html>
<head>
<title>HogerLager</title>
<meta charset="UTF-8">
</head>
<body>
<h1>HogerLager</h1>
<h2>Kies een cijfer (van 1 tot 1000)</h2>
<h3>Now Playing: <?php echo $_SESSION["name"]; ?> | <a href="clear.php">Opnieuw</a></h3>
<h4>Status</h4>
<p><?php if($_POST["name"]){echo "Invoer speler: ".$_POST["name"]." | ";} ?>Sessie cijfer: <?php echo $_SESSION["numb"]; ?> | Sessie speler: <?php echo $_SESSION["name"]; ?></p>
<form action="try.php" method="POST">
<input type="text" name="value">
<input type="submit" name="savename" value="Spelen!">
</form>
</body>
</html>
session_start();
if($_POST["name"]&&!$_SESSION["name"])
$_SESSION["name"]=$_POST["name"];
elseif(!$_POST["name"]&&!$_SESSION["name"])
header("Location:index.php");
if(!$_SESSION["numb"])
$_SESSION["numb"]=rand(1,1000);
?>
<!DOCTYPE html>
<html>
<head>
<title>HogerLager</title>
<meta charset="UTF-8">
</head>
<body>
<h1>HogerLager</h1>
<h2>Kies een cijfer (van 1 tot 1000)</h2>
<h3>Now Playing: <?php echo $_SESSION["name"]; ?> | <a href="clear.php">Opnieuw</a></h3>
<h4>Status</h4>
<p><?php if($_POST["name"]){echo "Invoer speler: ".$_POST["name"]." | ";} ?>Sessie cijfer: <?php echo $_SESSION["numb"]; ?> | Sessie speler: <?php echo $_SESSION["name"]; ?></p>
<form action="try.php" method="POST">
<input type="text" name="value">
<input type="submit" name="savename" value="Spelen!">
</form>
</body>
</html>
try.php
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?php
session_start();
if(!$_SESSION["name"])
header("Location:index.php");
if(!$_SESSION["numb"])
header("Location:play.php");
if(!$_SESSION["try"])
$_SESSION["try"]=1;
else
$_SESSION["try"]=$_SESSION["try"]+1;
?>
<!DOCTYPE html>
<html>
<head>
<title>HogerLager</title>
<meta charset="UTF-8">
</head>
<body>
<h1>HogerLager</h1>
<h2>U raadde: <?php echo $_POST["value"]; ?> (poging <?php echo $_SESSION["try"]; ?>)</h2>
<?php
if(!$_POST["value"]){
$return="Voer een cijfer in. <a href=\"play.php\">Opnieuw</a>";
}elseif($_POST["value"]>$_SESSION["numb"]){
$return="Voer een lager cijfer in. <a href=\"play.php\">Opnieuw</a>";
}elseif($_POST["value"]<$_SESSION["numb"]){
$return="Voer een hoger cijfer in. <a href=\"play.php\">Opnieuw</a>";
}elseif($_POST["value"]==$_SESSION["numb"]){
if($_SESSION["try"]==1)
$try="poging";
else
$try="pogingen";
$return="GEFELICITEERD! U raadde ".$_SESSION["numb"]." in ".$_SESSION["try"]." ".$try.". <a href=\"highscores.php\">Highscores</a> | <a href=\"play.php\">Opnieuw</a>";
unset($_SESSION["numb"]);
unset($_SESSION["try"]);
}
?>
<h3><?php echo $return; ?></h3>
</body>
</html>
session_start();
if(!$_SESSION["name"])
header("Location:index.php");
if(!$_SESSION["numb"])
header("Location:play.php");
if(!$_SESSION["try"])
$_SESSION["try"]=1;
else
$_SESSION["try"]=$_SESSION["try"]+1;
?>
<!DOCTYPE html>
<html>
<head>
<title>HogerLager</title>
<meta charset="UTF-8">
</head>
<body>
<h1>HogerLager</h1>
<h2>U raadde: <?php echo $_POST["value"]; ?> (poging <?php echo $_SESSION["try"]; ?>)</h2>
<?php
if(!$_POST["value"]){
$return="Voer een cijfer in. <a href=\"play.php\">Opnieuw</a>";
}elseif($_POST["value"]>$_SESSION["numb"]){
$return="Voer een lager cijfer in. <a href=\"play.php\">Opnieuw</a>";
}elseif($_POST["value"]<$_SESSION["numb"]){
$return="Voer een hoger cijfer in. <a href=\"play.php\">Opnieuw</a>";
}elseif($_POST["value"]==$_SESSION["numb"]){
if($_SESSION["try"]==1)
$try="poging";
else
$try="pogingen";
$return="GEFELICITEERD! U raadde ".$_SESSION["numb"]." in ".$_SESSION["try"]." ".$try.". <a href=\"highscores.php\">Highscores</a> | <a href=\"play.php\">Opnieuw</a>";
unset($_SESSION["numb"]);
unset($_SESSION["try"]);
}
?>
<h3><?php echo $return; ?></h3>
</body>
</html>
clear.php
even voor ik over de array's ga beginnen: in alle drie je scripts ga je er van uit dat de $_POST array en de $_SESSION array de door jouw genoemde items bezitten terwijl dat niet zo hoeft te zijn. Je zult hierdoor absoluut foutmeldingen krijgen. even een voorbeeld uit play.php:
Ga je die pagina nu direct aanroepen (zonder dat er iets gePOST is) dan zul je foutmeldingen krijgen dat $_POST["name"] en $_SESSION["name"] niet bestaan.
(Ook al bestaat de $_POST array wel dan zal het element $_POST["name"] nog niet bestaan.)
Om dit uit te sluiten zul je eerst de functie isset() moeten gebruiken:
Toevoeging op 28/09/2014 19:39:35:
Vaak zie je ook een variant als deze:
Code (php)
Hier wordt eerst eerst gecontroleerd of $_POST["name"] wel bestaat EN als die bestaat dan wordt er gekeken of hij wel inhoud en dus geen lege waarde heeft.
Een andere variant die precies hetzelfde doet:
Code (php)
Gewijzigd op 28/09/2014 19:34:50 door Frank Nietbelangrijk
Heb je enig idee hoe ik de highscore-functie het beste kan maken?
Realiseer je dan wel dat deze highscore enkel de highscore bijhoudt van één speler?
en ook dat na bepaalde tijd (een kwartier of een half uur) niet spelen de session data uitgewist is?
Als je geen database mag gebruiken dan blijft er alleen een mogelijkheid over om de highscore in een bestand op te slaan. Het nadeel hiervan is dat je voldoende schrijfrechten moet hebben op de server.
Misschien moet ik me daar eens in verdiepen. Schrijfrechten zijn geen probleem aangezien we een eigen APACHE server hebben geïnstalleerd.
Toevoeging op 28/09/2014 19:50:45:
De schrijfrechten kun je met chmod instellen. Ik adviseer om een aparte directory aan te maken waarin geschreven mag worden. als ik me niet vergis moet je die directory instellen op 755. daarna maak je in de directory een .htaccess bestand met de volgende inhoud:
Je directory is nu vanuit de browser niet meer toegankelijk
Toevoeging op 28/09/2014 19:59:28:
Ik ga er even vanuit dat je de naam en de highscore van bijvoorbeeld de tien hoogste spelers wilt onthouden.
je zou dan een array in JSON kunnen opslaan bijvoorbeeld.
(pretty cool huh?)
Code (php)
Toevoeging op 28/09/2014 20:02:10:
uitlezen gaat net andersom natuurlijk:
Code (php)
1
2
3
4
5
6
7
2
3
4
5
6
7
<?php
$hs = json_decode(file_get_contents('/home/domein/directory/highscore.json'));
// show us the entire array
echo '<pre>';
print_r($hs);
echo '</pre>';
$hs = json_decode(file_get_contents('/home/domein/directory/highscore.json'));
// show us the entire array
echo '<pre>';
print_r($hs);
echo '</pre>';
Daarnaast wil ik de output anders weergeven, bij voorkeur in een tabel.
Maar we zitten op de goede weg, bedankt hiervoor :-)
Ik heb trouwens gewoon even het bestand highscores.json gebruikt, in dezelfde map. Kwam er niet helemaal uit met het pad, dus dit was even makkelijker.
index.php
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?php
error_reporting(-1);
ini_set("display_errors","On");
date_default_timezone_set("Europe/Amsterdam");
session_start();
if(isset($_SESSION["name"]))
header("Location:play.php");
?>
<!DOCTYPE html>
<html>
<head>
<title>HogerLager</title>
<meta charset="UTF-8">
</head>
<body>
<h1>HogerLager</h1>
<h2>Voer uw naam in</h2>
<form action="play.php" method="POST">
<input type="text" name="name">
<input type="submit" name="savename" value="Spelen!">
</form>
</body>
</html>
error_reporting(-1);
ini_set("display_errors","On");
date_default_timezone_set("Europe/Amsterdam");
session_start();
if(isset($_SESSION["name"]))
header("Location:play.php");
?>
<!DOCTYPE html>
<html>
<head>
<title>HogerLager</title>
<meta charset="UTF-8">
</head>
<body>
<h1>HogerLager</h1>
<h2>Voer uw naam in</h2>
<form action="play.php" method="POST">
<input type="text" name="name">
<input type="submit" name="savename" value="Spelen!">
</form>
</body>
</html>
play.php
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?php
error_reporting(-1);
ini_set("display_errors","On");
date_default_timezone_set("Europe/Amsterdam");
session_start();
if(isset($_POST["name"])&&!isset($_SESSION["name"]))
$_SESSION["name"]=$_POST["name"];
elseif(!isset($_POST["name"])&&!isset($_SESSION["name"]))
header("Location:index.php");
if(!isset($_SESSION["numb"]))
$_SESSION["numb"]=rand(1,1000);
?>
<!DOCTYPE html>
<html>
<head>
<title>HogerLager</title>
<meta charset="UTF-8">
</head>
<body>
<h1>HogerLager</h1>
<h2>Kies een cijfer (van 1 tot 1000)</h2>
<h3>Now Playing: <?php echo $_SESSION["name"]; ?> | <a href="clear.php">Opnieuw</a></h3>
<h4>Status</h4>
<p><?php if(isset($_POST["name"])){echo "Invoer speler: ".$_POST["name"]." | ";} ?>Sessie cijfer: <?php echo $_SESSION["numb"]; ?> | Sessie speler: <?php echo $_SESSION["name"]; ?></p>
<form action="try.php" method="POST">
<input type="text" name="value">
<input type="submit" name="savename" value="Spelen!">
</form>
</body>
</html>
error_reporting(-1);
ini_set("display_errors","On");
date_default_timezone_set("Europe/Amsterdam");
session_start();
if(isset($_POST["name"])&&!isset($_SESSION["name"]))
$_SESSION["name"]=$_POST["name"];
elseif(!isset($_POST["name"])&&!isset($_SESSION["name"]))
header("Location:index.php");
if(!isset($_SESSION["numb"]))
$_SESSION["numb"]=rand(1,1000);
?>
<!DOCTYPE html>
<html>
<head>
<title>HogerLager</title>
<meta charset="UTF-8">
</head>
<body>
<h1>HogerLager</h1>
<h2>Kies een cijfer (van 1 tot 1000)</h2>
<h3>Now Playing: <?php echo $_SESSION["name"]; ?> | <a href="clear.php">Opnieuw</a></h3>
<h4>Status</h4>
<p><?php if(isset($_POST["name"])){echo "Invoer speler: ".$_POST["name"]." | ";} ?>Sessie cijfer: <?php echo $_SESSION["numb"]; ?> | Sessie speler: <?php echo $_SESSION["name"]; ?></p>
<form action="try.php" method="POST">
<input type="text" name="value">
<input type="submit" name="savename" value="Spelen!">
</form>
</body>
</html>
try.php
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?php
error_reporting(-1);
ini_set("display_errors","On");
date_default_timezone_set("Europe/Amsterdam");
session_start();
if(!isset($_SESSION["name"]))
header("Location:index.php");
if(!isset($_SESSION["numb"]))
header("Location:play.php");
if(!isset($_SESSION["try"]))
$_SESSION["try"]=1;
else
$_SESSION["try"]=$_SESSION["try"]+1;
?>
<!DOCTYPE html>
<html>
<head>
<title>HogerLager</title>
<meta charset="UTF-8">
</head>
<body>
<h1>HogerLager</h1>
<h2>U raadde: <?php echo $_POST["value"]; ?> (poging <?php echo $_SESSION["try"]; ?>)</h2>
<?php
if(!isset($_POST["value"])){
$return="Voer een cijfer in. <a href=\"play.php\">Opnieuw</a>";
}elseif($_POST["value"]>$_SESSION["numb"]){
$return="Voer een lager cijfer in. <a href=\"play.php\">Opnieuw</a>";
}elseif($_POST["value"]<$_SESSION["numb"]){
$return="Voer een hoger cijfer in. <a href=\"play.php\">Opnieuw</a>";
}elseif($_POST["value"]==$_SESSION["numb"]){
if($_SESSION["try"]==1)
$try="poging";
else
$try="pogingen";
$return="GEFELICITEERD! U raadde ".$_SESSION["numb"]." in ".$_SESSION["try"]." ".$try.". <a href=\"highscores.php\">Highscores</a> | <a href=\"play.php\">Opnieuw</a>";
$highscore=array("name"=>$_SESSION["name"],"score"=>$_SESSION["try"],"date"=>date("d-m-Y"));
file_put_contents("highscores.json",json_encode($highscore));
unset($_SESSION["numb"]);
unset($_SESSION["try"]);
}
?>
<h3><?php echo $return; ?></h3>
</body>
</html>
error_reporting(-1);
ini_set("display_errors","On");
date_default_timezone_set("Europe/Amsterdam");
session_start();
if(!isset($_SESSION["name"]))
header("Location:index.php");
if(!isset($_SESSION["numb"]))
header("Location:play.php");
if(!isset($_SESSION["try"]))
$_SESSION["try"]=1;
else
$_SESSION["try"]=$_SESSION["try"]+1;
?>
<!DOCTYPE html>
<html>
<head>
<title>HogerLager</title>
<meta charset="UTF-8">
</head>
<body>
<h1>HogerLager</h1>
<h2>U raadde: <?php echo $_POST["value"]; ?> (poging <?php echo $_SESSION["try"]; ?>)</h2>
<?php
if(!isset($_POST["value"])){
$return="Voer een cijfer in. <a href=\"play.php\">Opnieuw</a>";
}elseif($_POST["value"]>$_SESSION["numb"]){
$return="Voer een lager cijfer in. <a href=\"play.php\">Opnieuw</a>";
}elseif($_POST["value"]<$_SESSION["numb"]){
$return="Voer een hoger cijfer in. <a href=\"play.php\">Opnieuw</a>";
}elseif($_POST["value"]==$_SESSION["numb"]){
if($_SESSION["try"]==1)
$try="poging";
else
$try="pogingen";
$return="GEFELICITEERD! U raadde ".$_SESSION["numb"]." in ".$_SESSION["try"]." ".$try.". <a href=\"highscores.php\">Highscores</a> | <a href=\"play.php\">Opnieuw</a>";
$highscore=array("name"=>$_SESSION["name"],"score"=>$_SESSION["try"],"date"=>date("d-m-Y"));
file_put_contents("highscores.json",json_encode($highscore));
unset($_SESSION["numb"]);
unset($_SESSION["try"]);
}
?>
<h3><?php echo $return; ?></h3>
</body>
</html>
clear.php
Code (php)
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
<?php
error_reporting(-1);
ini_set("display_errors","On");
date_default_timezone_set("Europe/Amsterdam");
session_start();
if(isset($_SESSION["name"]))
unset($_SESSION["name"]);
header("Location:index.php");
?>
error_reporting(-1);
ini_set("display_errors","On");
date_default_timezone_set("Europe/Amsterdam");
session_start();
if(isset($_SESSION["name"]))
unset($_SESSION["name"]);
header("Location:index.php");
?>
highscores.php
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?php
error_reporting(-1);
ini_set("display_errors","On");
date_default_timezone_set("Europe/Amsterdam");
session_start();
?>
<!DOCTYPE html>
<html>
<head>
<title>HogerLager</title>
<meta charset="UTF-8">
</head>
<body>
<h1>HogerLager</h1>
<h2>Highscores</h2>
<h3>Waar staat u?</h3>
<?php
$highscores=json_decode(file_get_contents("highscores.json"));
echo "<pre>";
foreach($highscores as $value){
echo $value." ";
}
echo "</pre>";
?>
<a href="play.php">Speel nog een keer!</a>
</body>
</html>
error_reporting(-1);
ini_set("display_errors","On");
date_default_timezone_set("Europe/Amsterdam");
session_start();
?>
<!DOCTYPE html>
<html>
<head>
<title>HogerLager</title>
<meta charset="UTF-8">
</head>
<body>
<h1>HogerLager</h1>
<h2>Highscores</h2>
<h3>Waar staat u?</h3>
<?php
$highscores=json_decode(file_get_contents("highscores.json"));
echo "<pre>";
foreach($highscores as $value){
echo $value." ";
}
echo "</pre>";
?>
<a href="play.php">Speel nog een keer!</a>
</body>
</html>
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?php
// hulp functie voor het sorteren van de array
function cmp($a, $b)
{
if($a['score'] > $b['score'])
return false;
return true;
}
// haal de highscores op die we tot nu toe hebben
$highscores = json_decode(file_get_contents("highscores.json"));
// voeg een element toe aan de array
$highscores[] = array(
"name" => $_SESSION["name"],
"score" => $_SESSION["try"],
"date" => date("d-m-Y")
);
// sorteer de array op volgorde van score van hoog naar laag
usort($highscores, 'cmp');
// hou maximaal de eerste tien elementen over en laat de rest vervallen
$highscores = array_slice($highscores, 0, 10);
// sla de scores weer op
file_put_contents("highscores.json", json_encode($highscore));
?>
// hulp functie voor het sorteren van de array
function cmp($a, $b)
{
if($a['score'] > $b['score'])
return false;
return true;
}
// haal de highscores op die we tot nu toe hebben
$highscores = json_decode(file_get_contents("highscores.json"));
// voeg een element toe aan de array
$highscores[] = array(
"name" => $_SESSION["name"],
"score" => $_SESSION["try"],
"date" => date("d-m-Y")
);
// sorteer de array op volgorde van score van hoog naar laag
usort($highscores, 'cmp');
// hou maximaal de eerste tien elementen over en laat de rest vervallen
$highscores = array_slice($highscores, 0, 10);
// sla de scores weer op
file_put_contents("highscores.json", json_encode($highscore));
?>
Toevoeging op 29/09/2014 01:25:07:
in highscores.php zul je ook rekening moeten houden met een twee dimensionale array.
Code (php)
Toevoeging op 29/09/2014 01:33:59:
Even een paar andere opmerkingen:
in al je bestanden hou je de php logica netjes bovenin en de html hou je onderin behalve in try.php en ook een heel klein beetje in highscores.php (in die laatste zou regel 18 omhoog moeten)
Zou je try.php willen verbeteren in dat opzicht?
Verder mag je je code best wat luchtiger opstellen. Laat hier en daar maar een lege regel over om aan te geven dat je aan een nieuw stukje code begint. ook mogen er best wat meer spaties in, vooral om de operators. Het maakt voor de snelheid niets uit.
Als laatste voorzie je code van commentaar. Leg uit wat de code doet voor je leraar maar ook voor jezelf later of voor anderen.
Gewijzigd op 29/09/2014 01:45:29 door Frank Nietbelangrijk
Tot nu toe dit:
index.php
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<?php
/* HogerLager by Mario Braam */
// Turn error reporting on for development
error_reporting(-1);
ini_set("display_errors","On");
// Set timezone
date_default_timezone_set("Europe/Amsterdam");
// Start sessions
session_start();
// Check if playersession is set
if(isset($_SESSION["name"]))
// If playersession is already set, redirect to the gamepage
header("Location:play.php");
?>
<!DOCTYPE html>
<html>
<head>
<title>HogerLager</title>
<meta charset="UTF-8">
</head>
<body>
<h1>HogerLager</h1>
<h2>Voer uw naam in</h2>
<form action="play.php" method="POST">
<input type="text" name="name">
<input type="submit" name="savename" value="Spelen!">
</form>
</body>
</html>
/* HogerLager by Mario Braam */
// Turn error reporting on for development
error_reporting(-1);
ini_set("display_errors","On");
// Set timezone
date_default_timezone_set("Europe/Amsterdam");
// Start sessions
session_start();
// Check if playersession is set
if(isset($_SESSION["name"]))
// If playersession is already set, redirect to the gamepage
header("Location:play.php");
?>
<!DOCTYPE html>
<html>
<head>
<title>HogerLager</title>
<meta charset="UTF-8">
</head>
<body>
<h1>HogerLager</h1>
<h2>Voer uw naam in</h2>
<form action="play.php" method="POST">
<input type="text" name="name">
<input type="submit" name="savename" value="Spelen!">
</form>
</body>
</html>
play.php
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?php
/* HogerLager by Mario Braam */
// Turn error reporting on for development
error_reporting(-1);
ini_set("display_errors","On");
// Set timezone
date_default_timezone_set("Europe/Amsterdam");
// Start sessions
session_start();
// Check if the player has entered a name and the session does not already exist
if(isset($_POST["name"])&&!isset($_SESSION["name"]))
// In this case, set the playersession
$_SESSION["name"]=$_POST["name"];
// It's possible that the player visits this page directly, then we check if the playersession is set. Otherwise, redirect
elseif(!isset($_POST["name"])&&!isset($_SESSION["name"]))
header("Location:index.php");
// If a valuesession hasn't been set already, create one with a random number
if(!isset($_SESSION["numb"]))
$_SESSION["numb"]=rand(1,1000);
?>
<!DOCTYPE html>
<html>
<head>
<title>HogerLager</title>
<meta charset="UTF-8">
</head>
<body>
<h1>HogerLager</h1>
<h2>Kies een cijfer (van 1 tot 1000)</h2>
<h3>Now Playing: <?php echo $_SESSION["name"]; ?> | <a href="clear.php">Opnieuw</a></h3>
<h4>Status</h4>
<p><?php if(isset($_POST["name"])){echo "Invoer speler: ".$_POST["name"]." | ";} ?>Sessie cijfer: <?php echo $_SESSION["numb"]; ?> | Sessie speler: <?php echo $_SESSION["name"]; ?></p>
<form action="try.php" method="POST">
<input type="text" name="value">
<input type="submit" name="savename" value="Spelen!">
</form>
</body>
</html>
/* HogerLager by Mario Braam */
// Turn error reporting on for development
error_reporting(-1);
ini_set("display_errors","On");
// Set timezone
date_default_timezone_set("Europe/Amsterdam");
// Start sessions
session_start();
// Check if the player has entered a name and the session does not already exist
if(isset($_POST["name"])&&!isset($_SESSION["name"]))
// In this case, set the playersession
$_SESSION["name"]=$_POST["name"];
// It's possible that the player visits this page directly, then we check if the playersession is set. Otherwise, redirect
elseif(!isset($_POST["name"])&&!isset($_SESSION["name"]))
header("Location:index.php");
// If a valuesession hasn't been set already, create one with a random number
if(!isset($_SESSION["numb"]))
$_SESSION["numb"]=rand(1,1000);
?>
<!DOCTYPE html>
<html>
<head>
<title>HogerLager</title>
<meta charset="UTF-8">
</head>
<body>
<h1>HogerLager</h1>
<h2>Kies een cijfer (van 1 tot 1000)</h2>
<h3>Now Playing: <?php echo $_SESSION["name"]; ?> | <a href="clear.php">Opnieuw</a></h3>
<h4>Status</h4>
<p><?php if(isset($_POST["name"])){echo "Invoer speler: ".$_POST["name"]." | ";} ?>Sessie cijfer: <?php echo $_SESSION["numb"]; ?> | Sessie speler: <?php echo $_SESSION["name"]; ?></p>
<form action="try.php" method="POST">
<input type="text" name="value">
<input type="submit" name="savename" value="Spelen!">
</form>
</body>
</html>
try.php
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
/* HogerLager by Mario Braam */
// Turn error reporting on for development
error_reporting(-1);
ini_set("display_errors","On");
// Set timezone
date_default_timezone_set("Europe/Amsterdam");
// Start sessions
session_start();
// Check if playersession is set
if(!isset($_SESSION["name"]))
// If it doesn't exist, redirect to playerpage
header("Location:index.php");
// Check if numbersession is set
if(!isset($_SESSION["numb"]))
// If it doesn't exist, redirect to gamepage
header("Location:play.php");
// Check if attemptsession is set
if(!isset($_SESSION["try"]))
// If it isn't, set to 1 (1 attempt)
$_SESSION["try"]=1;
// Otherwise, count +1
else
$_SESSION["try"]=$_SESSION["try"]+1;
?>
<!DOCTYPE html>
<html>
<head>
<title>HogerLager</title>
<meta charset="UTF-8">
</head>
<body>
<h1>HogerLager</h1>
<h2>U raadde: <?php echo $_POST["value"]; ?> (poging <?php echo $_SESSION["try"]; ?>)</h2>
<?php
// Just to be sure, check if the player has entered a value
if(!isset($_POST["value"])){
$return="Voer een cijfer in. <a href=\"play.php\">Opnieuw</a>";
// If he has, check if the entered value is higher than the value that's stored
}elseif($_POST["value"]>$_SESSION["numb"]){
$return="Voer een lager cijfer in. <a href=\"play.php\">Opnieuw</a>";
// The entered value isn't higher, then it should be lower
}elseif($_POST["value"]<$_SESSION["numb"]){
$return="Voer een hoger cijfer in. <a href=\"play.php\">Opnieuw</a>";
// What's this? It's a match! It must be a right attempt
}elseif($_POST["value"]==$_SESSION["numb"]){
// 1 attempt means we use a other word
if($_SESSION["try"]==1)
$try="poging";
else
$try="pogingen";
// Congratulate the player and give him some information
$return="GEFELICITEERD! U raadde ".$_SESSION["numb"]." in ".$_SESSION["try"]." ".$try.". <a href=\"highscores.php\">Highscores</a> | <a href=\"play.php\">Opnieuw</a>";
// Grab the file
$highscores=json_decode(file_get_contents("highscores.json"));
// Create an array from the required information
$highscores[]=array("name"=>$_SESSION["name"],"score"=>$_SESSION["try"],"datetime"=>date("d-m-Y H:i:s"));
// Limit the arrays
$highscores=array_slice($highscores,0,10);
// And store it again!
file_put_contents("highscores.json",json_encode($highscores));
// Now all the informationprocessing is done, unset the sessions with the number of attempts and the value
unset($_SESSION["numb"]);
unset($_SESSION["try"]);
}
?>
<h3><?php echo $return; ?></h3>
</body>
</html>
/* HogerLager by Mario Braam */
// Turn error reporting on for development
error_reporting(-1);
ini_set("display_errors","On");
// Set timezone
date_default_timezone_set("Europe/Amsterdam");
// Start sessions
session_start();
// Check if playersession is set
if(!isset($_SESSION["name"]))
// If it doesn't exist, redirect to playerpage
header("Location:index.php");
// Check if numbersession is set
if(!isset($_SESSION["numb"]))
// If it doesn't exist, redirect to gamepage
header("Location:play.php");
// Check if attemptsession is set
if(!isset($_SESSION["try"]))
// If it isn't, set to 1 (1 attempt)
$_SESSION["try"]=1;
// Otherwise, count +1
else
$_SESSION["try"]=$_SESSION["try"]+1;
?>
<!DOCTYPE html>
<html>
<head>
<title>HogerLager</title>
<meta charset="UTF-8">
</head>
<body>
<h1>HogerLager</h1>
<h2>U raadde: <?php echo $_POST["value"]; ?> (poging <?php echo $_SESSION["try"]; ?>)</h2>
<?php
// Just to be sure, check if the player has entered a value
if(!isset($_POST["value"])){
$return="Voer een cijfer in. <a href=\"play.php\">Opnieuw</a>";
// If he has, check if the entered value is higher than the value that's stored
}elseif($_POST["value"]>$_SESSION["numb"]){
$return="Voer een lager cijfer in. <a href=\"play.php\">Opnieuw</a>";
// The entered value isn't higher, then it should be lower
}elseif($_POST["value"]<$_SESSION["numb"]){
$return="Voer een hoger cijfer in. <a href=\"play.php\">Opnieuw</a>";
// What's this? It's a match! It must be a right attempt
}elseif($_POST["value"]==$_SESSION["numb"]){
// 1 attempt means we use a other word
if($_SESSION["try"]==1)
$try="poging";
else
$try="pogingen";
// Congratulate the player and give him some information
$return="GEFELICITEERD! U raadde ".$_SESSION["numb"]." in ".$_SESSION["try"]." ".$try.". <a href=\"highscores.php\">Highscores</a> | <a href=\"play.php\">Opnieuw</a>";
// Grab the file
$highscores=json_decode(file_get_contents("highscores.json"));
// Create an array from the required information
$highscores[]=array("name"=>$_SESSION["name"],"score"=>$_SESSION["try"],"datetime"=>date("d-m-Y H:i:s"));
// Limit the arrays
$highscores=array_slice($highscores,0,10);
// And store it again!
file_put_contents("highscores.json",json_encode($highscores));
// Now all the informationprocessing is done, unset the sessions with the number of attempts and the value
unset($_SESSION["numb"]);
unset($_SESSION["try"]);
}
?>
<h3><?php echo $return; ?></h3>
</body>
</html>
clear.php
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?php
/* HogerLager by Mario Braam */
// Turn error reporting on for development
error_reporting(-1);
ini_set("display_errors","On");
// Set timezone
date_default_timezone_set("Europe/Amsterdam");
// Start sessions
session_start();
// Check if playersession is set
if(isset($_SESSION["name"]))
// If it does, unset
unset($_SESSION["name"]);
// Now redirect
header("Location:index.php");
?>
/* HogerLager by Mario Braam */
// Turn error reporting on for development
error_reporting(-1);
ini_set("display_errors","On");
// Set timezone
date_default_timezone_set("Europe/Amsterdam");
// Start sessions
session_start();
// Check if playersession is set
if(isset($_SESSION["name"]))
// If it does, unset
unset($_SESSION["name"]);
// Now redirect
header("Location:index.php");
?>
highscores.php
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?php
/* HogerLager by Mario Braam */
// Turn error reporting on for development
error_reporting(-1);
ini_set("display_errors","On");
// Set timezone
date_default_timezone_set("Europe/Amsterdam");
// Start sessions
session_start();
?>
<!DOCTYPE html>
<html>
<head>
<title>HogerLager</title>
<meta charset="UTF-8">
<style>
table{
width:40%;
}
table th{
text-align:left;
}
</style>
</head>
<body>
<h1>HogerLager</h1>
<h2>Highscores</h2>
<h3>Waar staat u?</h3>
<table>
<tr>
<th>Speler</th>
<th>Pogingen</th>
<th>Datum</th>
</tr>
<?php
// Grab highscores from file
$highscores=json_decode(file_get_contents("highscores.json"));
// Turn the array into a clear table
foreach($highscores as $value){
echo "<tr>";
echo "<td>".$value->name."</td>
<td>".$value->score."</td>
<td>".$value->datetime."</td>";
echo "</tr>";
}
?>
</table>
<p><a href="play.php">Speel opnieuw!</a></p>
</body>
</html>
/* HogerLager by Mario Braam */
// Turn error reporting on for development
error_reporting(-1);
ini_set("display_errors","On");
// Set timezone
date_default_timezone_set("Europe/Amsterdam");
// Start sessions
session_start();
?>
<!DOCTYPE html>
<html>
<head>
<title>HogerLager</title>
<meta charset="UTF-8">
<style>
table{
width:40%;
}
table th{
text-align:left;
}
</style>
</head>
<body>
<h1>HogerLager</h1>
<h2>Highscores</h2>
<h3>Waar staat u?</h3>
<table>
<tr>
<th>Speler</th>
<th>Pogingen</th>
<th>Datum</th>
</tr>
<?php
// Grab highscores from file
$highscores=json_decode(file_get_contents("highscores.json"));
// Turn the array into a clear table
foreach($highscores as $value){
echo "<tr>";
echo "<td>".$value->name."</td>
<td>".$value->score."</td>
<td>".$value->datetime."</td>";
echo "</tr>";
}
?>
</table>
<p><a href="play.php">Speel opnieuw!</a></p>
</body>
</html>
Ik heb er nu commentaar tussen geplaatst. Wat betreft de spaties en witruimte, dit is wat ik gewend ben haha.
Toevoeging op 29/09/2014 21:20:51:
Kleine aanpassing:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
de scores staan in een 2d array.
de primaire array bevat wederom array's
de primaire array heeft elementen van 0 tm ?
de secundaire array heeft drie elementen: score, name en date.
om nu de hele secundaire elementen te sorteren in de primaire array moeten we een 'eigen' sorteerfunctie gebruiken. Die functie heb ik 'cmp' genoemd, de afkorting van compare.
de functie cmp($a, $b) hoort dus bij usort('cmp');. Als je goed kijkt naar de functie 'cmp' dan zie je dat de scores vergeleken worden en dus wordt de hele array op de scores gesorteerd.
Stel dat je nou op naam zou willen sorteren dan moet er in die functie dus op naam en niet op score gesorteerd worden..
Ik hoop dat ik het zo wat verduidelijkt heb.
index.php
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<?php
/* HogerLager by Mario Braam */
// Turn error reporting on for development
error_reporting(-1);
ini_set("display_errors","On");
// Set timezone
date_default_timezone_set("Europe/Amsterdam");
// Start sessions
session_start();
// Check if playersession is set
if(isset($_SESSION["name"]))
// If playersession is already set, redirect to the gamepage
header("Location:play.php");
?>
<!DOCTYPE html>
<html>
<head>
<title>HogerLager</title>
<meta charset="UTF-8">
</head>
<body>
<h1>HogerLager</h1>
<h2>Voer uw naam in</h2>
<form action="play.php" method="POST">
<input type="text" name="name">
<input type="submit" name="savename" value="Spelen!">
</form>
</body>
</html>
/* HogerLager by Mario Braam */
// Turn error reporting on for development
error_reporting(-1);
ini_set("display_errors","On");
// Set timezone
date_default_timezone_set("Europe/Amsterdam");
// Start sessions
session_start();
// Check if playersession is set
if(isset($_SESSION["name"]))
// If playersession is already set, redirect to the gamepage
header("Location:play.php");
?>
<!DOCTYPE html>
<html>
<head>
<title>HogerLager</title>
<meta charset="UTF-8">
</head>
<body>
<h1>HogerLager</h1>
<h2>Voer uw naam in</h2>
<form action="play.php" method="POST">
<input type="text" name="name">
<input type="submit" name="savename" value="Spelen!">
</form>
</body>
</html>
play.php
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?php
/* HogerLager by Mario Braam */
// Turn error reporting on for development
error_reporting(-1);
ini_set("display_errors","On");
// Set timezone
date_default_timezone_set("Europe/Amsterdam");
// Start sessions
session_start();
// Check if the player has entered a name and the session does not already exist
if(isset($_POST["name"])&&!isset($_SESSION["name"]))
// In this case, set the playersession
$_SESSION["name"]=$_POST["name"];
// It's possible that the player visits this page directly, then we check if the playersession is set. Otherwise, redirect
elseif(!isset($_POST["name"])&&!isset($_SESSION["name"]))
header("Location:index.php");
// If a valuesession hasn't been set already, create one with a random number
if(!isset($_SESSION["numb"]))
$_SESSION["numb"]=rand(1,1000);
?>
<!DOCTYPE html>
<html>
<head>
<title>HogerLager</title>
<meta charset="UTF-8">
</head>
<body>
<h1>HogerLager</h1>
<h2>Kies een cijfer (van 1 tot 1000)</h2>
<h3>Now Playing: <?php echo $_SESSION["name"]; ?> | <a href="clear.php">Opnieuw</a></h3>
<h4>Status</h4>
<p><?php if(isset($_POST["name"])){echo "Invoer speler: ".$_POST["name"]." | ";} ?>Sessie cijfer: <?php echo $_SESSION["numb"]; ?> | Sessie speler: <?php echo $_SESSION["name"]; ?></p>
<form action="try.php" method="POST">
<input type="number" name="value">
<input type="submit" name="savename" value="Spelen!">
</form>
</body>
</html>
/* HogerLager by Mario Braam */
// Turn error reporting on for development
error_reporting(-1);
ini_set("display_errors","On");
// Set timezone
date_default_timezone_set("Europe/Amsterdam");
// Start sessions
session_start();
// Check if the player has entered a name and the session does not already exist
if(isset($_POST["name"])&&!isset($_SESSION["name"]))
// In this case, set the playersession
$_SESSION["name"]=$_POST["name"];
// It's possible that the player visits this page directly, then we check if the playersession is set. Otherwise, redirect
elseif(!isset($_POST["name"])&&!isset($_SESSION["name"]))
header("Location:index.php");
// If a valuesession hasn't been set already, create one with a random number
if(!isset($_SESSION["numb"]))
$_SESSION["numb"]=rand(1,1000);
?>
<!DOCTYPE html>
<html>
<head>
<title>HogerLager</title>
<meta charset="UTF-8">
</head>
<body>
<h1>HogerLager</h1>
<h2>Kies een cijfer (van 1 tot 1000)</h2>
<h3>Now Playing: <?php echo $_SESSION["name"]; ?> | <a href="clear.php">Opnieuw</a></h3>
<h4>Status</h4>
<p><?php if(isset($_POST["name"])){echo "Invoer speler: ".$_POST["name"]." | ";} ?>Sessie cijfer: <?php echo $_SESSION["numb"]; ?> | Sessie speler: <?php echo $_SESSION["name"]; ?></p>
<form action="try.php" method="POST">
<input type="number" name="value">
<input type="submit" name="savename" value="Spelen!">
</form>
</body>
</html>
try.php
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php
/* HogerLager by Mario Braam */
// Turn error reporting on for development
error_reporting(-1);
ini_set("display_errors","On");
// Set timezone
date_default_timezone_set("Europe/Amsterdam");
// Start sessions
session_start();
// Check if playersession is set
if(!isset($_SESSION["name"]))
// If it doesn't exist, redirect to playerpage
header("Location:index.php");
// Check if numbersession is set
if(!isset($_SESSION["numb"]))
// If it doesn't exist, redirect to gamepage
header("Location:play.php");
// Check if attemptsession is set
if(!isset($_SESSION["try"]))
// If it isn't, set to 1 (1 attempt)
$_SESSION["try"]=1;
// Otherwise, count +1
else
$_SESSION["try"]=$_SESSION["try"]+1;
?>
<!DOCTYPE html>
<html>
<head>
<title>HogerLager</title>
<meta charset="UTF-8">
</head>
<body>
<h1>HogerLager</h1>
<?php
if(isset($_POST["value"])and is_numeric($_POST["value"])){
?>
<h2>U raadde: <?php echo $_POST["value"]; ?> (poging <?php echo $_SESSION["try"]; ?>)</h2>
<?php
}
// Just to be sure, check if the player has entered a value
if(!isset($_POST["value"])or !is_numeric($_POST["value"])){
$return="Voer een cijfer in. <a href=\"play.php\">Opnieuw</a>";
// If he has, check if the entered value is higher than the value that's stored
}elseif($_POST["value"]>$_SESSION["numb"]){
$return="Voer een lager cijfer in. <a href=\"play.php\">Opnieuw</a>";
// The entered value isn't higher, then it should be lower
}elseif($_POST["value"]<$_SESSION["numb"]){
$return="Voer een hoger cijfer in. <a href=\"play.php\">Opnieuw</a>";
// What's this? It's a match! It must be a right attempt
}elseif($_POST["value"]==$_SESSION["numb"]){
// 1 attempt means we use a other word
if($_SESSION["try"]==1)
$try="poging";
else
$try="pogingen";
// Congratulate the player and give him some information
$return="GEFELICITEERD! U raadde ".$_SESSION["numb"]." in ".$_SESSION["try"]." ".$try.". <a href=\"highscores.php\">Highscores</a> | <a href=\"play.php\">Opnieuw</a>";
// Grab the file
$highscores=json_decode(file_get_contents("highscores.json"));
// Create an array from the required information
$highscores[]=array("name"=>$_SESSION["name"],"score"=>$_SESSION["try"],"datetime"=>date("d-m-Y H:i:s"));
// Limit the arrays
$highscores=array_slice($highscores,0,10);
// And store it again!
file_put_contents("highscores.json",json_encode($highscores));
// Now all the informationprocessing is done, unset the sessions with the number of attempts and the value
unset($_SESSION["numb"]);
unset($_SESSION["try"]);
}
?>
<h3><?php echo $return; ?></h3>
</body>
</html>
/* HogerLager by Mario Braam */
// Turn error reporting on for development
error_reporting(-1);
ini_set("display_errors","On");
// Set timezone
date_default_timezone_set("Europe/Amsterdam");
// Start sessions
session_start();
// Check if playersession is set
if(!isset($_SESSION["name"]))
// If it doesn't exist, redirect to playerpage
header("Location:index.php");
// Check if numbersession is set
if(!isset($_SESSION["numb"]))
// If it doesn't exist, redirect to gamepage
header("Location:play.php");
// Check if attemptsession is set
if(!isset($_SESSION["try"]))
// If it isn't, set to 1 (1 attempt)
$_SESSION["try"]=1;
// Otherwise, count +1
else
$_SESSION["try"]=$_SESSION["try"]+1;
?>
<!DOCTYPE html>
<html>
<head>
<title>HogerLager</title>
<meta charset="UTF-8">
</head>
<body>
<h1>HogerLager</h1>
<?php
if(isset($_POST["value"])and is_numeric($_POST["value"])){
?>
<h2>U raadde: <?php echo $_POST["value"]; ?> (poging <?php echo $_SESSION["try"]; ?>)</h2>
<?php
}
// Just to be sure, check if the player has entered a value
if(!isset($_POST["value"])or !is_numeric($_POST["value"])){
$return="Voer een cijfer in. <a href=\"play.php\">Opnieuw</a>";
// If he has, check if the entered value is higher than the value that's stored
}elseif($_POST["value"]>$_SESSION["numb"]){
$return="Voer een lager cijfer in. <a href=\"play.php\">Opnieuw</a>";
// The entered value isn't higher, then it should be lower
}elseif($_POST["value"]<$_SESSION["numb"]){
$return="Voer een hoger cijfer in. <a href=\"play.php\">Opnieuw</a>";
// What's this? It's a match! It must be a right attempt
}elseif($_POST["value"]==$_SESSION["numb"]){
// 1 attempt means we use a other word
if($_SESSION["try"]==1)
$try="poging";
else
$try="pogingen";
// Congratulate the player and give him some information
$return="GEFELICITEERD! U raadde ".$_SESSION["numb"]." in ".$_SESSION["try"]." ".$try.". <a href=\"highscores.php\">Highscores</a> | <a href=\"play.php\">Opnieuw</a>";
// Grab the file
$highscores=json_decode(file_get_contents("highscores.json"));
// Create an array from the required information
$highscores[]=array("name"=>$_SESSION["name"],"score"=>$_SESSION["try"],"datetime"=>date("d-m-Y H:i:s"));
// Limit the arrays
$highscores=array_slice($highscores,0,10);
// And store it again!
file_put_contents("highscores.json",json_encode($highscores));
// Now all the informationprocessing is done, unset the sessions with the number of attempts and the value
unset($_SESSION["numb"]);
unset($_SESSION["try"]);
}
?>
<h3><?php echo $return; ?></h3>
</body>
</html>
clear.php
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?php
/* HogerLager by Mario Braam */
// Turn error reporting on for development
error_reporting(-1);
ini_set("display_errors","On");
// Set timezone
date_default_timezone_set("Europe/Amsterdam");
// Start sessions
session_start();
// Check if playersession is set
if(isset($_SESSION["name"]))
// If it does, unset
unset($_SESSION["name"]);
// Now redirect
header("Location:index.php");
?>
/* HogerLager by Mario Braam */
// Turn error reporting on for development
error_reporting(-1);
ini_set("display_errors","On");
// Set timezone
date_default_timezone_set("Europe/Amsterdam");
// Start sessions
session_start();
// Check if playersession is set
if(isset($_SESSION["name"]))
// If it does, unset
unset($_SESSION["name"]);
// Now redirect
header("Location:index.php");
?>
highscores.php
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
/* HogerLager by Mario Braam */
// Turn error reporting on for development
error_reporting(-1);
ini_set("display_errors","On");
// Set timezone
date_default_timezone_set("Europe/Amsterdam");
// Start sessions
session_start();
function cmp($a,$b){
if($a["score"]>$b["score"])
return false;
return true;
}
?>
<!DOCTYPE html>
<html>
<head>
<title>HogerLager</title>
<meta charset="UTF-8">
<style>
table{
width:40%;
}
table th{
text-align:left;
}
</style>
</head>
<body>
<h1>HogerLager</h1>
<h2>Highscores</h2>
<h3>Waar staat u?</h3>
<table>
<tr>
<th>Speler</th>
<th>Pogingen</th>
<th>Datum</th>
</tr>
<?php
// Grab highscores from file
$highscores=json_decode(file_get_contents("highscores.json"));
usort($highscores,"cmp");
// Turn the array into a clear table
if(isset($highscores)){
foreach($highscores as $value){
echo "<tr>";
echo "<td>".$value->name."</td>
<td>".$value->score."</td>
<td>".$value->datetime."</td>";
echo "</tr>";
}
}else{
echo "<tr><td colspan=\"3\">Er zijn nog geen highscores!</td></tr>";
}
?>
</table>
<p><a href="play.php">Speel opnieuw!</a></p>
</body>
</html>
/* HogerLager by Mario Braam */
// Turn error reporting on for development
error_reporting(-1);
ini_set("display_errors","On");
// Set timezone
date_default_timezone_set("Europe/Amsterdam");
// Start sessions
session_start();
function cmp($a,$b){
if($a["score"]>$b["score"])
return false;
return true;
}
?>
<!DOCTYPE html>
<html>
<head>
<title>HogerLager</title>
<meta charset="UTF-8">
<style>
table{
width:40%;
}
table th{
text-align:left;
}
</style>
</head>
<body>
<h1>HogerLager</h1>
<h2>Highscores</h2>
<h3>Waar staat u?</h3>
<table>
<tr>
<th>Speler</th>
<th>Pogingen</th>
<th>Datum</th>
</tr>
<?php
// Grab highscores from file
$highscores=json_decode(file_get_contents("highscores.json"));
usort($highscores,"cmp");
// Turn the array into a clear table
if(isset($highscores)){
foreach($highscores as $value){
echo "<tr>";
echo "<td>".$value->name."</td>
<td>".$value->score."</td>
<td>".$value->datetime."</td>";
echo "</tr>";
}
}else{
echo "<tr><td colspan=\"3\">Er zijn nog geen highscores!</td></tr>";
}
?>
</table>
<p><a href="play.php">Speel opnieuw!</a></p>
</body>
</html>
Als eerste een enkelvoudige array:
Code (php)
output:
Zoals je ziet is de array gesorteerd op alfabetische volgorde.
Nu een enkelvoudige associatieve array:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?php
$arr2 = array('id' => 1, 'naam' => 'Frank', 'email' => '[email protected]');
// sorteer op de inhoud van de elementen
asort($arr2);
foreach($arr2 as $index => $value)
{
echo $index . ': ' . $value . '<br>';
}
echo '<br>';
// sorteer op de indexen id, naam en email
ksort($arr2);
foreach($arr2 as $index => $value)
{
echo $index . ': ' . $value . '<br>';
}
?>
$arr2 = array('id' => 1, 'naam' => 'Frank', 'email' => '[email protected]');
// sorteer op de inhoud van de elementen
asort($arr2);
foreach($arr2 as $index => $value)
{
echo $index . ': ' . $value . '<br>';
}
echo '<br>';
// sorteer op de indexen id, naam en email
ksort($arr2);
foreach($arr2 as $index => $value)
{
echo $index . ': ' . $value . '<br>';
}
?>
output:
De eerste keer is de array gesorteerd op alfabetische volgorde.
De tweede keer is zijn de array indexen gesorteerd op alfabetische volgorde.
Toevoeging op 06/10/2014 17:31:05:
Nu gaan we naar een twee dimensionale array:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php
// een array waarin ieder element wederom een array is ofwel een twee dimensionale array
$arr3 = array(
array('id' => 3, 'naam' => 'Frank', 'email' => '[email protected]'),
array('id' => 1, 'naam' => 'Mario', 'email' => '[email protected]'),
array('id' => 2, 'naam' => 'Gert', 'email' => '[email protected]')
);
// probeer de array weer te geven:
foreach($arr3 as $index => $value)
{
echo $index . ': ' . $value . '<br>';
}
?>
// een array waarin ieder element wederom een array is ofwel een twee dimensionale array
$arr3 = array(
array('id' => 3, 'naam' => 'Frank', 'email' => '[email protected]'),
array('id' => 1, 'naam' => 'Mario', 'email' => '[email protected]'),
array('id' => 2, 'naam' => 'Gert', 'email' => '[email protected]')
);
// probeer de array weer te geven:
foreach($arr3 as $index => $value)
{
echo $index . ': ' . $value . '<br>';
}
?>
output:
Code (php)
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
Notice: Array to string conversion in C:\xampp\www\arr.php on line 13
0: Array
Notice: Array to string conversion in C:\xampp\www\arr.php on line 13
1: Array
Notice: Array to string conversion in C:\xampp\www\arr.php on line 13
2: Array
0: Array
Notice: Array to string conversion in C:\xampp\www\arr.php on line 13
1: Array
Notice: Array to string conversion in C:\xampp\www\arr.php on line 13
2: Array
Omdat ieder element in de buitenste array ook een array is kunnen we die niet zo weergeven.
Toevoeging op 06/10/2014 17:51:35:
We zullen een stapje verder moeten gaan en moeten zeggen 'Wat' we willen echoën uit de binnenste array:
Code (php)
output:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
id: 1
naam: Frank
email: [email protected]
id: 3
naam: Mario
email: [email protected]
id: 2
naam: Gert
email: [email protected]
naam: Frank
email: [email protected]
id: 3
naam: Mario
email: [email protected]
id: 2
naam: Gert
email: [email protected]
Ook kunnen we natuurlijk alleen de naam weergeven van ieder element:
output:
Toevoeging op 06/10/2014 18:14:48:
Nu weer terug naar het sorteren:
Stel dat we nu weer de functie sort() of asort() willen gebruiken dan ontstaat het probleem dat deze functies alleen werken op de buitenste array en niet op de binnenste:
Code (php)
output:
Dit geeft niet het gewenste resultaat. de asort() functie blijkt nog wel zo slim te zijn dat je elementen die een array zijn niet kunt sorteren. (array == array == array). Dus pakt de array het eerste element in de binnenste array, het id.
Echter willen wij niet op het id sorteren maar op naam. Er zit nu niets anders op om onze eigen sorteer algo op te geven aan de hand van een functie die door usort() meerdere malen wordt aangeroepen om zo alle elementen te kunnen sorteren.
Hier een voorbeeld van twee verschillende functies die beiden dezelfde array's anders sorteren:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?php
function sortNaam($a, $b)
{
if($a['naam'] < $b['naam'])
return false;
return true;
}
function sortEmailReverse($a, $b)
{
if($a['email'] > $b['email'])
return false;
return true;
}
// een array waarin ieder element wederom een array is ofwel een twee dimensionale array
$arr3 = array(
array('id' => 3, 'naam' => 'Frank', 'email' => '[email protected]'),
array('id' => 1, 'naam' => 'Mario', 'email' => '[email protected]'),
array('id' => 2, 'naam' => 'Gert', 'email' => '[email protected]')
);
// sorteer de array op volgorde van naam van hoog naar laag
usort($arr3, 'sortNaam');
// dump de complete array op het scherm
echo '<pre>';
print_r($arr3);
echo '</pre>';
// sorteer de array op volgorde van email van LAAG naar HOOG
usort($arr3, 'sortEmailReverse');
// dump de complete array op het scherm
echo '<pre>';
print_r($arr3);
echo '</pre>';
?>
function sortNaam($a, $b)
{
if($a['naam'] < $b['naam'])
return false;
return true;
}
function sortEmailReverse($a, $b)
{
if($a['email'] > $b['email'])
return false;
return true;
}
// een array waarin ieder element wederom een array is ofwel een twee dimensionale array
$arr3 = array(
array('id' => 3, 'naam' => 'Frank', 'email' => '[email protected]'),
array('id' => 1, 'naam' => 'Mario', 'email' => '[email protected]'),
array('id' => 2, 'naam' => 'Gert', 'email' => '[email protected]')
);
// sorteer de array op volgorde van naam van hoog naar laag
usort($arr3, 'sortNaam');
// dump de complete array op het scherm
echo '<pre>';
print_r($arr3);
echo '</pre>';
// sorteer de array op volgorde van email van LAAG naar HOOG
usort($arr3, 'sortEmailReverse');
// dump de complete array op het scherm
echo '<pre>';
print_r($arr3);
echo '</pre>';
?>
output:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
Array
(
[0] => Array
(
[id] => 3
[naam] => Frank
[email] => [email protected]
)
[1] => Array
(
[id] => 2
[naam] => Gert
[email] => [email protected]
)
[2] => Array
(
[id] => 1
[naam] => Mario
[email] => [email protected]
)
)
Array
(
[0] => Array
(
[id] => 1
[naam] => Mario
[email] => [email protected]
)
[1] => Array
(
[id] => 2
[naam] => Gert
[email] => [email protected]
)
[2] => Array
(
[id] => 3
[naam] => Frank
[email] => [email protected]
)
)
(
[0] => Array
(
[id] => 3
[naam] => Frank
[email] => [email protected]
)
[1] => Array
(
[id] => 2
[naam] => Gert
[email] => [email protected]
)
[2] => Array
(
[id] => 1
[naam] => Mario
[email] => [email protected]
)
)
Array
(
[0] => Array
(
[id] => 1
[naam] => Mario
[email] => [email protected]
)
[1] => Array
(
[id] => 2
[naam] => Gert
[email] => [email protected]
)
[2] => Array
(
[id] => 3
[naam] => Frank
[email] => [email protected]
)
)
Gewijzigd op 06/10/2014 18:41:59 door Frank Nietbelangrijk