0
votes

First off hello, I am new here.
My problem is that I have a php file pulling info from a database. I will post the code below. What I need is for my JavaScript to take the output and load it into a list that generates some flash cards.

code sample `$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");

// select database
mysql_select_db($db) or die ("Unable to select database!");

$query1 = "SELECT * FROM category_tb WHERE cat_name = '$category'"; $result1 = mysql_query($query1) or die ("Error in query: $query1. " . mysql_error()); while ($row = mysql_fetch_array($result1)) {

                $cat_num = $row[1];
        }

// This establishes a link to MySQL $query = "SELECT * FROM english_lang, finnish_lang ". "WHERE english_lang.lang_id = finnish_lang.lang_id AND english_lang.cat_id = $cat_num"; $rt = mysql_query($query) or die ("Error in query: $query. " . mysql_error());

while($nt=mysql_fetch_array($rt)){ echo "{\"english\": \"$nt[1]\", \"finnish\": \"$nt[6]\" , \"asked\": states.notAsked},";
} `

So this basicly gets some data and formats it to be used by the javascript. if you want to look at the output of this to get a better idea the go here
http://languagelearner.byethost2.com/vocabulary2.php
select 1 of the first 2 categories as they are the only ones with data right
now.
the javascript is this:

code sample `

var string1;
var string2;
var number;
var states = {"oneVisible": 0, "bothVisible": 1, "notAsked": 2, "asked": 3}
var state = states.bothVisible;
var numberOfWordsAsked = 0;
var words = {"list": [


    ]
}

function displayWords(){

    if (state == states.bothVisible) {
        if (numberOfWordsAsked < words.list.length) {
            state = states.oneVisible;
            number = Math.floor(Math.random() * words.list.length);
            while (words.list[number].asked == states.asked) {
                number = Math.floor(Math.random() * words.list.length);
            }
            string1 = words.list[number].english;
            string2 = words.list[number].finnish;
            document.getElementById("fin").style.display = 'none';
            document.getElementById("eng").innerHTML = words.list[number].english;
            document.getElementById("fin").innerHTML = words.list[number].finnish;
            document.getElementById("b").value = "Show word";
            document.getElementById("correct").style.display = 'none';
        }
        else {
            document.getElementById("eng").innerHTML = "You know all the words in this category, congratulations!";
            document.getElementById("fin").style.display = 'none';
            document.getElementById("b").style.display = 'none';
            document.getElementById("correct").style.display = 'none';
        }
    }

    else {
        document.getElementById("fin").style.display = 'inline';
        state = states.bothVisible;
        document.getElementById("b").value = "Wrong";
        document.getElementById("correct").style.display = 'inline';
    }
}

function setCorrect(){
    words.list[number].asked = states.asked;
    numberOfWordsAsked += 1;
    displayWords();
}

//-->
</script>

`

so the output needs to go in here.
var words = {"list": [
]

Any help would be appreciated. I did not write the javascript, a friend did.
He used static info in the list.

2

2 Answers

4
votes

Try AJAX. Check out http://www.w3schools.com/PHP/php_ajax_database.asp

var ajax = new XMLHttpRequest();
ajax.onreadystatechange = function() {
if (ajax.readyState == 4) {
 alert(ajax.responseText);
}
};
ajax.open("GET", "ajax.php", true);
ajax.send(null);

outputs "hello world" when used in the same directory as a php file ajax.php:

<php

echo 'hello Word!';

?>
0
votes

To put php data structures into something javascript can parse, use json_encode. That should be enough to help you on your way.