douyuan4357 2016-11-20 19:34
浏览 11

如何重复从php文件中获取数据直到达到某个条件?

I have a php file that gets a word from a database randomly and json encodes it. I want to get the word using jquery but also make sure that word isn't in a list already. I'm confused on how I can repeatedly hit the server till my condition is met. Here is what I have:

php:

 <?php
    $sql = "SELECT * FROM words ORDER BY RAND() LIMIT 1";
    $result = $db->query($sql);

    $row = $result->fetch_assoc();
    $word = $row['word'];
    echo json_encode($word);
    ?>

Jquery function:

$(document).ready(function() {
    $("#newRound").on("click",function(){
        $.getJSON("getWord.php",function(data){
            //check here if data is already in wordsSoFar arary and if it is, get another word from getword.php
            document.getElementById("input1").style.visibility = 'visible';
            currentWord = data; //set the current work
            lives = 6; //reset lives
            tracker = 0; 
            incorrectLettersGuessed = "";
            allGuessedLetters = "";
            updateLetters();
            document.getElementById('hangman').innerHTML = '<center><img src="stage1.png"></center>';
            createTable(currentWord);
            output.innerHTML = '<center>'+messages.validLetter + '</center>';
            alert(currentWord);
        });
    });
});
  • 写回答

2条回答 默认 最新

  • donglankui1263 2016-11-20 19:50
    关注

    if you want a sql solution:

    $sql = "SELECT * FROM words WHERE column_name NOT IN ('yourword1','yourword2','youword3'...) ORDER BY RAND() LIMIT 1";

    EDIT: i saw your comment too late (Check it on client side).

    here a jquery solution (because youre working with jQuery):

    $.each(yourwordsarray, function( index, value ) {
      if(value == word_is_in){
    .. do what you want with the word
    }
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)