PHP Undefined offset: 0 error

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

Post Reply
wooxie
Forum Newbie
Posts: 5
Joined: Wed Dec 11, 2013 8:23 am

PHP Undefined offset: 0 error

Post 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']);
}
?>
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
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: PHP Undefined offset: 0 error

Post 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?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: PHP Undefined offset: 0 error

Post 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.
(#10850)
wooxie
Forum Newbie
Posts: 5
Joined: Wed Dec 11, 2013 8:23 am

Re: PHP Undefined offset: 0 error

Post by wooxie »

It's says Undefined offset: 0 error in line:

line 43 - 45
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: PHP Undefined offset: 0 error

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: PHP Undefined offset: 0 error

Post 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?
(#10850)
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: PHP Undefined offset: 0 error

Post 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
wooxie
Forum Newbie
Posts: 5
Joined: Wed Dec 11, 2013 8:23 am

Re: PHP Undefined offset: 0 error

Post by wooxie »

Can someone tell me how to fix this maybe?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: PHP Undefined offset: 0 error

Post 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.
(#10850)
Post Reply