0

I am trying to run a stored procedure using PHP. I want to put the results of the query into a javascript object. I cannot find how to accomplish this. I have run this code but I get one of the results I am returning is undefined. Here is my code:

$result = mysql_query("call sp_getGenre()");
    if($result === FALSE){
            die(mysql_error());
        }
        while($row = mysql_fetch_array($result)){
    ?>
            <script type="text/javascript">
                console.log(<? $row['Type'] ?>);
                var genreObj = new Object();
                genreObj.name = "<? echo $row['Type'] ?>";
                genreObj.level = <? echo $row['Level'] ?>;
    <?
                $parentID = $row['PrevID'];
                if($parent == null){ $parent = "0"; }
    ?>
                genreObj.parent = <? $parent ?>;
                arrGenre.push(genreObj);
            </script>
    <?
        }
    ?>

I am fairly new to the PHP world but would greatly appreciate a point in the right direction. Thanks in advance.

seroth
  • 581
  • 1
  • 9
  • 26
  • Can you paste here the output, why you don't use echo or = to display variable content in genreObj.parent = $parent ?>; You can easily create the object using Ajax – Hamza Feb 09 '13 at 01:31
  • Thank you for teh fast replies. I'm not fully understanding. Can you give me an example of what genreObj.name = " echo $row['Type'] ?>"; should look like please. – seroth Feb 09 '13 at 01:34

3 Answers3

3

Build an associative array in PHP resembling the JavaScript object you want, then use json_encode() to convert it into a JSON object, you can then easily pass on to JavaScript.

You may even be able to do something like:

<?php
// stuff
$resultObj = json_encode($row);
?>

<script type="text/javascript">
var genreObj = <?= $resultObj ?>;
</script>

This is not supposed to be a perfect solution to your problem, but hopefully it will give you some ideas.

Ayman Safadi
  • 11,502
  • 1
  • 27
  • 41
  • Is it possible to build a PHP object and pass that object to a javascript object? – seroth Feb 09 '13 at 02:01
  • Not by default, your object class has to provide encoding methods. See http://stackoverflow.com/questions/4697656/using-json-encode-on-objects-in-php-regardless-of-scope – Barmar Feb 09 '13 at 02:12
  • Is using that method the only way? – seroth Feb 09 '13 at 02:16
1

I think

<? $row['Type'] ?>

Should be

<?= $row['Type'] ?> or
<? echo $row['Type'] ?>

Same goes for

<? $parent ?>;
Leeish
  • 5,203
  • 2
  • 17
  • 45
0

Code below is not fully tested but the basic Idea is to Write all values into an array, then convert that array to a json object. From there assign the json object to the jsonresults variable. Now you can pull up all the info you need via javascript.

<?php


$result = mysql_query("call sp_getGenre()");
if($result === FALSE){
    die(mysql_error());
}
while($row = mysql_fetch_array($result)){
    $resultsarray = array("type"=> $row['type'], "level" => $row['Level'}, "previd" => $row['PrevID']);
    $jsonifiedresults = json_encode($resultsarray);
    ?>
            <script type="text/javascript">
                jsonresult = <? echo $jsonifiedresults; ?>;

                console.log(jsonresult.type);
                var genreObj = new Object();
                genreObj.name = jsonresult.type;
                genreObj.level = jsonresult.level;
                genreObj.parent = jsonreult.prevID;
                arrGenre.push(genreObj);
            </script>
<?php
        }
?>
swbullis
  • 21
  • 1
  • 2