PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
wooxie
Forum Newbie
Posts: 5 Joined: Wed Dec 11, 2013 8:23 am
Post
by wooxie » Wed Dec 18, 2013 3:36 am
Hello,
I got a error in my code witch I can't figure out what is wrong maybe some one could help me?
Code: Select all
<?php
session_start();
if (!isset($_SESSION['vragen'])) {
$_SESSION['vragen'] = array(
'n1' => array(),
'n2' => array(),
'ans' => array()
);
$_SESSION['count'] = 0;
$number1 = range(10,100);
$number2 = range(1,10);
$uitkomst= $number1 / $number2;
$nieuw= round ($uitkomst,0);
$number1= $nieuw*$number2;
$arr = array_rand($number1, 100);
foreach ($arr as $k) $_SESSION['vragen']['n1'][] = $number1[$k];
$number1 = array_diff($number1, $_SESSION['vragen']['n1']);
$arr = array_rand($number2, 10);
foreach ($arr as $k) $_SESSION['vragen']['n2'][] = $number2[$k];
}
if (isset($_POST['ans'])) {
$_SESSION['vragen']['ans'][$_SESSION['count']] = $_POST['ans'];
++$_SESSION['count'];
}
?>
<html>
<head>
<title>Delen groep 6 </title>
</head>
<body>
<form method='post' action=''>
<?php
$q = $_SESSION['count']+1;
if ($q < 11) {
echo <<<EOT
<h2>Groep 6 Delen (:)</h2>
<h5>vraag $q</h5>
{$_SESSION['vragen']['n1'][$_SESSION['count']]}
:
{$_SESSION['vragen']['n2'][$_SESSION['count']]}
<br><br>
Jou Antwoordt: <input type='text' size='3' name='ans'><br>
<br><br>
<input name='btnsubmit' type='submit' value='Volgende'>
<input name='btnreset' type='reset' value='Leegmaken'>
EOT;
}
else {
echo "<h5>Resultaat</h5>
<table border='1' cellpadding='3' style='border-collapse:collapse'>\n
<tr><td></td><td>Vraag</td><td>Antwoordt</td><td>Jou Antwoordt</td><td>Goed</td></tr>\n";
for ($i=0; $i<10; $i++) {
$qno = $i+1;
$qtext = "{$_SESSION['vragen']['n1'][$i]} : {$_SESSION['vragen']['n2'][$i]}";
$ans = $_SESSION['vragen']['n1'][$i] / $_SESSION['vragen']['n2'][$i];
$yours = $_SESSION['vragen']['ans'][$i];
$chk = $ans==$yours ? '✓' : '';
echo "<tr><td>$qno</td><td>$qtext</td><td>$ans</td><td>$yours</td><td>$chk</td></tr>\n";
}
echo "</table>\n<br><input name='btnsubmit' type='submit' value='Nieuwe Test'>";
unset($_SESSION['vragen']);
}
?>
Last edited by
requinix on Wed Dec 18, 2013 1:32 pm, edited 1 time in total.
Reason: please use [syntax] tags when posting code
requinix
Spammer :|
Posts: 6617 Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA
Post
by requinix » Wed Dec 18, 2013 1:33 pm
I, for one, am not too inclined to go looking through code to find something that could possibly be wrong. I could be doing other things with that time.
How about giving us a description of what's wrong? What it does and what it is supposed to do? Whether you get warnings or errors and what they say?
Christopher
Site Administrator
Posts: 13596 Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US
Post
by Christopher » Wed Dec 18, 2013 10:41 pm
wooxie wrote: I got a error in my code witch I can't figure out what is wrong maybe some one could help me?
Yes, what is the error message. It should give the error and line number.
(#10850)
wooxie
Forum Newbie
Posts: 5 Joined: Wed Dec 11, 2013 8:23 am
Post
by wooxie » Thu Dec 19, 2013 1:48 am
It's says Undefined offset: 0 error in line:
line 43 - 45
Celauran
Moderator
Posts: 6427 Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada
Post
by Celauran » Thu Dec 19, 2013 6:45 am
I'm going to guess you don't have error reporting turned on. You're trying to divide arrays, which just isn't going to work.
Christopher
Site Administrator
Posts: 13596 Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US
Post
by Christopher » Thu Dec 19, 2013 10:22 am
wooxie wrote: It's says Undefined offset: 0 error in line:
line 43 - 45
The problem is undefined array offsets in these lines:
Code: Select all
echo <<<EOT
<h2>Groep 6 Delen (:)</h2>
<h5>vraag $q</h5>
{$_SESSION['vragen']['n1'][$_SESSION['count']]}
:
{$_SESSION['vragen']['n2'][$_SESSION['count']]}
<br><br>
Jou Antwoordt: <input type='text' size='3' name='ans'><br>
<br><br>
<input name='btnsubmit' type='submit' value='Volgende'>
<input name='btnreset' type='reset' value='Leegmaken'>
EOT;
Do you know that $_SESSION['count'], $_SESSION['vragen'], $_SESSION['vragen']['n1'] and $_SESSION['vragen']['n1'][$_SESSION['count']] (and 'n2') are all defined?
(#10850)
Celauran
Moderator
Posts: 6427 Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada
Post
by Celauran » Thu Dec 19, 2013 11:33 am
They're not.
Code: Select all
<?php
session_start();
if (!isset($_SESSION['vragen'])) {
$_SESSION['vragen'] = array(
'n1' => array(),
'n2' => array(),
'ans' => array()
);
$_SESSION['count'] = 0;
$number1 = range(10,100);
$number2 = range(1,10);
$uitkomst= $number1 / $number2; // Fatal error right here
$nieuw= round ($uitkomst,0);
$number1= $nieuw*$number2;
$arr = array_rand($number1, 100);
foreach ($arr as $k) $_SESSION['vragen']['n1'][] = $number1[$k]; // Would have been defined here
$number1 = array_diff($number1, $_SESSION['vragen']['n1']);
$arr = array_rand($number2, 10);
foreach ($arr as $k) $_SESSION['vragen']['n2'][] = $number2[$k];
}
// snip
wooxie
Forum Newbie
Posts: 5 Joined: Wed Dec 11, 2013 8:23 am
Post
by wooxie » Tue Feb 25, 2014 6:41 am
Can someone tell me how to fix this maybe?
Christopher
Site Administrator
Posts: 13596 Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US
Post
by Christopher » Tue Feb 25, 2014 3:26 pm
You need to make sure that $_SESSION['count'] is set and likewise that $_SESSION['vragen']['n1'][$_SESSION['count']] has a value at that index.
(#10850)