I'm attempting to read in a user's weightlifting history from a database using a php file(viewworkout.php) to first run the query and then a js file to get the results from the query. the script below is all i have so far, and it

<script type="text/javascript">
    <?php include("viewworkout.php");

    echo "var numrecords =  $numrows;\n";
    echo "var muscle = new Array();\n" ;
    echo "var exercise = new Array();\n" ;
    echo "var set = new Array();\n" ;
    echo "var weight = new Array();\n" ;
    echo "var reps = new Array();\n" ;
    echo "var difficulty = new Array();\n" ;

    for($i = 0; $i < $numrows; $i++){
    echo "var muscle[$i] = \"".$muscle[$i]."\";\n";
    }
    ?>
    </script>

The javascript debugger gives the error -
"missing ; before statement"
but the generated code(shown below) looks fine.
what am I doing wrong?
The line numbers are from the debugger btw.


20var muscle[0] = "Calves";
21var muscle[1] = "Calves";
22var muscle[2] = "Lats";
23var muscle[3] = "LowerBack";
24var muscle[4] = "LowerBack";
25var muscle[5] = "Quads";
26var muscle[6] = "Quads";
27var muscle[7] = "Quads";
28var muscle[8] = "Quads";
29var muscle[9] = "Traps";
30var muscle[10] = "Traps";
31var muscle[11] = "Traps";
32var muscle[12] = "Biceps";
33var muscle[13] = "Biceps";
34var muscle[14] = "Biceps";
35var muscle[15] = "Biceps";
36var muscle[16] = "Lats";
.......ect
Thanks in advanced

Your script is ok! Try to apply the following format and see how it works:

<?php
echo '<script type="text/javascript">';

include 'workout.php';
echo 'var numrecords = '.$numrows.';';

print "var muscle = [];\n
var exercise = [];\n

</script>"; // so on...
?>

Your script is ok! Try to apply the following format and see how it works:

<?php
echo '<script type="text/javascript">';

include 'workout.php';
echo 'var numrecords = '.$numrows.';';

print "var muscle = [];\n
var exercise = [];\n

</script>"; // so on...
?>

A) Why are you mixing echo and print?
B) His code was cleaner before, don't put HTML in PHP strings, its ugly and unmaintainable

Hi there mrs. Shawn, can you explain why!? Hoping to see a better solution for this issue... Tnx

Firstly, I hope the "mrs." was unintentional. Secondly, probably the cleanest way to write it is

<?php include("viewworkout.php") ?>
<script type="text/javascript">
  var numrecords =  <?php echo $numrows ?>;
  var muscle = new Array();
  var exercise = new Array();
  var set = new Array();
  var weight = new Array();
  var reps = new Array();
  var difficulty = new Array();
<?php for($i = 0; $i < $numrows; $i++): ?>
  var muscle[<?php echo $i ?>] = "<?php echo $muscle[$i] ?>";
<?php endfor ?>
</script>

Sorry, it was not intentional--ive slipped my mind on that part...
I just wanna' say thanks, regarding on how to write proper php line's. Good day to you...

This article has been dead for over six months. Start a new discussion instead.