navlag navlag - 2 months ago 12
PHP Question

How Can I Take Oracle 10G Data Into a PHP Array and Then Convert It to a JavaScript Array?

I'm trying to define a PHP array, then connect to Oracle 10g and take the data requested from the connection and put it into the PHP array.

Finally, I want to take the PHP array and convert it into a JavaScript array.

Here's what I'm trying.

Can somebody point out why this is not working and perhaps offer a solution?

Thank you.

$dbArray = array();
$conn = oci_connect("username", "password", "connecturl");
$query = 'select endpoint_name from endpoint_ref order by endpoint_name asc';
$stid = oci_parse($conn, $query);
$result = oci_execute($stid);
while ($row = oci_fetch_array($stid, OCI_RETURN_NULLS+OCI_ASSOC))
{
foreach ($row as $item) {
array_push($dbArray, $item);
var_dump($dbArray);
echo $dbArray;
}
}
$jsArray = json_encode($dbArray);


Also, how can I set this JavaScript variable named "availableTags" to equal my newly encoded JavaScript array?

$(function() {
var availableTags = jsArray;
$( "#tags" ).autocomplete({
source: availableTags
});
});
</script>

Answer

The conversion in my question was correct, the problem was the JavaScript function. This is how to properly call the JSON encoded PHP array containing the Oracle 10g data.

    <script>
    $(function() {
        var availableTags = <?php echo json_encode($dbArray); ?>;
        $( "#endPointName" ).autocomplete({
        source: availableTags
        });
    });
    </script>
Comments