0

So Im making an website which is sapoust to generate a quiz and for few last days Im facing an error : Notice: Undefined offset 1 Notice: Undefined offset 2 and so on. On line 31 :

$z = $id[$i];

And here is the rest of the code whic may be helpful

    for($i=0; $i<$liczba; $i++) {
        $random = rand($i, $liczba) ;
        do{
        $random = rand($i, $liczba) ;
        if(in_array($random, $wylosowanieNR) == false) {
            array_push($wylosowanieNR,$random) ;
            array_push($wylosowanieR,$random) ;
            break ;
        }
        }while(in_array($random, $wylosowanieNR) == false) ;

}
return $wylosowanieR ;
}
//code
$id[] = losowanie($_POST['ilePytan']) ;
//code
$z = $id[$i];
                    $zapytanie = "SELECT * FROM pytania WHERE id = '$z' ";

Another error is :

Notice: Array to string conversion  at line 37 :
$zapytanie = "SELECT * FROM pytania WHERE id = '$z' ";

Please help. Im trying to fix this error for 5 days already. Please help

devpro
  • 16,184
  • 3
  • 27
  • 38
gabi 13
  • 17
  • 2
  • That is a `notice`, not an error. The cause is obvious from the message: you try to access an element in the array `$id` that does not exist. – arkascha Jan 23 '16 at 11:18
  • Array to string notice means $id[$I] is not a string – devpro Jan 23 '16 at 11:40
  • @arkascha then the error message probably isn't obvious, as your description obviously isn't correct. It's saying `Array to string conversion on `"[...] '$z' "``. Meaning `$z` probably contains an array instead of an expected string. (and notice is an error level in PHP, still worth fixing) – Wouter J Jan 23 '16 at 11:40
  • @WouterJ That is another notice, again, _not_ an error. I referred to the notice addressed here by the OP, see the top of the question, and the line giving as a reference just below. – arkascha Jan 23 '16 at 11:44
  • Possible duplicate of [Reference - What does this error mean in PHP?](http://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) – Qirel Jan 23 '16 at 12:37

2 Answers2

0

Your code snippet is not complete, but i guess the top part is the function losowanie(). If this is the case, the error is in this line:

$id[] = losowanie($_POST['ilePytan']) ;

You add the result array to the array $id so that it contains a single element that is an array. I don't think you want this.

$id = losowanie($_POST['ilePytan']) ;

should fix this.

ul90
  • 762
  • 3
  • 8
0

As per your ist notice:

losowanie function will be returned an array and you are again assign this into an array as $id[] this should be this:

$id = losowanie($_POST['ilePytan']);

Becuase your second notice is very clear about the array to string conversion you are using an array as a string. It means above mentioned line will solve issues.

devpro
  • 16,184
  • 3
  • 27
  • 38