Random waardes in array behouden na refresh
Weet iemand hoe ik dit op kan lossen zonder cookies/sessies? Hieronder staat mijn code.
<html>
<head>
<title></title>
</head>
<body>
<p>
Code (php)
<form name="form1" method="post" action="">
<p>
<label for="txtGetal"></label>
Vergelijkingsgetal
<input type="text" name="txtGetal" id="txtGetal">
<input type="submit" name="btnVergelijk" id="btnVergelijk" value="Vergelijk">
</p>
<p>
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?php
if(isset($_POST['btnVergelijk']))
{
$vgetal = $_POST['txtGetal'];
$teller1 = 0;
for ($i=0; $i < 20; $i++)
{
$teller1++;
if($newarray[$i] >= $vgetal)
{
$n = $newarray[$i] - 10 ;
print "Element " . $teller1 . " = " . $n . "</br>";
}
elseif($newarray[$i] < $vgetal)
{
$n = $newarray[$i] + 5;
print "Element " . $teller1 . " = " . $n . "</br>";
}
}
}
?>
if(isset($_POST['btnVergelijk']))
{
$vgetal = $_POST['txtGetal'];
$teller1 = 0;
for ($i=0; $i < 20; $i++)
{
$teller1++;
if($newarray[$i] >= $vgetal)
{
$n = $newarray[$i] - 10 ;
print "Element " . $teller1 . " = " . $n . "</br>";
}
elseif($newarray[$i] < $vgetal)
{
$n = $newarray[$i] + 5;
print "Element " . $teller1 . " = " . $n . "</br>";
}
}
}
?>
</p>
</form>
</body>
</html>
Gewijzigd op 07/09/2011 13:17:51 door Daan Dutilh
Toevoeging op 07/09/2011 13:41:28:
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
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
<?php
session_start();
?>
<html>
<head>
<title></title>
</head>
<body>
<p>
<?php
if(!isset($_SESSION['array']))
{
$newarray = array();
for ($i=0; $i < 20; $i++)
{
$randomgetal = (rand()%500);
$newarray[] = $randomgetal;
}
$_SESSION['array'] = $newarray;
}
$newarray = $_SESSION['array'];
$teller = 0;
foreach ($newarray as $number)
{
$teller++;
print "Element " . $teller . " = " . $number . "</br>";
}
?>
<form name="form1" method="post" action="">
<p>
<label for="txtGetal"></label>
Vergelijkingsgetal
<input type="text" name="txtGetal" id="txtGetal">
<input type="submit" name="btnVergelijk" id="btnVergelijk" value="Vergelijk">
</p>
<p>
<?php
if(isset($_POST['btnVergelijk']))
{
$vgetal = $_POST['txtGetal'];
$teller1 = 0;
for ($i=0; $i < 20; $i++)
{
$teller1++;
if($newarray[$i] >= $vgetal)
{
$n = $newarray[$i] - 10 ;
print "Element " . $teller1 . " = " . $n . "</br>";
}
elseif($newarray[$i] < $vgetal)
{
$n = $newarray[$i] + 5;
print "Element " . $teller1 . " = " . $n . "</br>";
}
}
}
$_SESSION['array'] = $newarray;
?>
</p>
</form>
</body>
</html>
session_start();
?>
<html>
<head>
<title></title>
</head>
<body>
<p>
<?php
if(!isset($_SESSION['array']))
{
$newarray = array();
for ($i=0; $i < 20; $i++)
{
$randomgetal = (rand()%500);
$newarray[] = $randomgetal;
}
$_SESSION['array'] = $newarray;
}
$newarray = $_SESSION['array'];
$teller = 0;
foreach ($newarray as $number)
{
$teller++;
print "Element " . $teller . " = " . $number . "</br>";
}
?>
<form name="form1" method="post" action="">
<p>
<label for="txtGetal"></label>
Vergelijkingsgetal
<input type="text" name="txtGetal" id="txtGetal">
<input type="submit" name="btnVergelijk" id="btnVergelijk" value="Vergelijk">
</p>
<p>
<?php
if(isset($_POST['btnVergelijk']))
{
$vgetal = $_POST['txtGetal'];
$teller1 = 0;
for ($i=0; $i < 20; $i++)
{
$teller1++;
if($newarray[$i] >= $vgetal)
{
$n = $newarray[$i] - 10 ;
print "Element " . $teller1 . " = " . $n . "</br>";
}
elseif($newarray[$i] < $vgetal)
{
$n = $newarray[$i] + 5;
print "Element " . $teller1 . " = " . $n . "</br>";
}
}
}
$_SESSION['array'] = $newarray;
?>
</p>
</form>
</body>
</html>
Bedankt voor je super snelle antwoord. Zo werkt het inderdaad goed.