Page 1 of 1

PHP Undefined offset: 0 error

Posted: Wed Dec 18, 2013 3:36 am
by wooxie
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 ? '&check;' : '';
        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']);
}
?>

Re: PHP Undefined offset: 0 error

Posted: Wed Dec 18, 2013 1:33 pm
by requinix
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?

Re: PHP Undefined offset: 0 error

Posted: Wed Dec 18, 2013 10:41 pm
by Christopher
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.

Re: PHP Undefined offset: 0 error

Posted: Thu Dec 19, 2013 1:48 am
by wooxie
It's says Undefined offset: 0 error in line:

line 43 - 45

Re: PHP Undefined offset: 0 error

Posted: Thu Dec 19, 2013 6:45 am
by Celauran
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.

Re: PHP Undefined offset: 0 error

Posted: Thu Dec 19, 2013 10:22 am
by Christopher
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?

Re: PHP Undefined offset: 0 error

Posted: Thu Dec 19, 2013 11:33 am
by Celauran
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

Re: PHP Undefined offset: 0 error

Posted: Tue Feb 25, 2014 6:41 am
by wooxie
Can someone tell me how to fix this maybe?

Re: PHP Undefined offset: 0 error

Posted: Tue Feb 25, 2014 3:26 pm
by Christopher
You need to make sure that $_SESSION['count'] is set and likewise that $_SESSION['vragen']['n1'][$_SESSION['count']] has a value at that index.