1
<?php

require "connection.php";

$cek = mysql_query("SELECT * FROM `kelimeler` ORDER BY `numara` DESC LIMIT 10");

$i = 0;
$tr_dizi = array();
$alm_dizi = array();

while ($satir = mysql_fetch_array($cek)) {
    $tr_dizi[$i] = $satir["turkce"];
    $alm_dizi[$i] = $satir["almanca"];
    $i++;
}

$deger = rand(1,10);

setcookie("soru", $tr_dizi[$deger]);
setcookie("cevap", $alm_dizi[$deger]);

?>

<?php

if (isset($_POST["kelime"])) {
    if ($_POST["kelime"] == $_COOKIE['cevap']) {
        echo '<div class="bildirim">Richtig &#10004</div>';
    } else {
        echo '<div class="bildirim">'.$_COOKIE['soru'].' = '.$_COOKIE['cevap'].'</div>';
    }
}

?>

This is a quiz game like a = b what is a ? if user send b it says 'yes!' else correct answer was 'b' php say

Notice: Undefined offset: 10 on line 19 - 20 - 55

Line 19 -> setcookie("soru", $tr_dizi[$deger]);  
Line 20 -> setcookie("cevap", $alm_dizi[$deger]); 
Line 55 -> <divclass="kelime"><?php echo $tr_dizi[$deger] ?></div>

when user send the answer program check it and ask new question and yes or no is showing on new page i'm using cookies for that

how can i fix this error i can make error handling but i'm a new developer i want to learn thank you for everybody :)

Darwin von Corax
  • 5,201
  • 3
  • 17
  • 28
Brown
  • 27
  • 7

2 Answers2

2

It is not setting 10 as $i starts from 0. And you are fetching 10 rows from database.

So the keys would be 0-9. The rand should generate numbers between 0-9. Try with -

$deger = rand(0, 9);

And try to avoid using mysql. It is deprecated now.

Sougata Bose
  • 31,517
  • 8
  • 49
  • 87
0

Array indexes in php are zero-based, that is, an array with 10 elements indexes them as [0] through [9]. Change your initialization of $degar to

$degar = rand(0, 9);

and you should get rid of the error.

Darwin von Corax
  • 5,201
  • 3
  • 17
  • 28