mikesta707 Posted August 3, 2009 Share Posted August 3, 2009 I was wondering if it was possible to use variables defined in different code blocks. IE would the following work: <script> my_var = "Test Data"; </script> //some html or something <script> document.write(my_var); </script> obviously my implementation is far more complicated. I use PHP and echo javascript so I can build a javascript array with the same data as a php array. The code that does this looks like this: [code=php:0] echo "<script type=\"text/javascript\">"; echo "full_array = new Array();\n"; echo "full_array[0] = new Array(7);\n"; for ($i = 0; $i < count($full_array); $i++){ echo "full_array[$i][0] = " . $full_array[$i][0] . ";\n"; echo "full_array[$i][1] = " . $full_array[$i][1] . ";\n"; echo "full_array[$i][2] = " . $full_array[$i][2] . ";\n"; echo "full_array[$i][3] = " . $full_array[$i][3] . ";\n"; echo "full_array[$i][4] = " . $full_array[$i][4] . ";\n"; echo "full_array[$i][5] = " . $full_array[$i][5] . ";\n"; echo "full_array[$i][6] = " . $full_array[$i][6] . ";\n"; } echo "</script>"; when I try to use the full_array var in one of my functions, it shows as undefined. Am I doing this right? Is my Javascript code wrong? I am far more adept at PHP than Javascript, so I think I am doing something stupidly wrong with javascript. Anyone have any ideas?[/code] Quote Link to comment https://forums.phpfreaks.com/topic/168693-using-variables-from-different-code-blocks/ Share on other sites More sharing options...
gevans Posted August 3, 2009 Share Posted August 3, 2009 You're mixing your php and javascript. they need to be kept seperate for ($i = 0; $i < count($full_array); $i++){ There you're trying to count what will be a javascript client-side array with php Quote Link to comment https://forums.phpfreaks.com/topic/168693-using-variables-from-different-code-blocks/#findComment-889952 Share on other sites More sharing options...
mikesta707 Posted August 3, 2009 Author Share Posted August 3, 2009 You're mixing your php and javascript. they need to be kept seperate for ($i = 0; $i < count($full_array); $i++){ There you're trying to count what will be a javascript client-side array with php well, my JS array and PHP array are the same size. They are the same exact array actually, and thats how I iterate through my PHP array so I can assign the correct values to my JS array. I'm assuming this is wrong, How should it be done. I've done a few google searches on this, and most of the links I have found say to do it this way. Quote Link to comment https://forums.phpfreaks.com/topic/168693-using-variables-from-different-code-blocks/#findComment-889960 Share on other sites More sharing options...
gevans Posted August 3, 2009 Share Posted August 3, 2009 Can you post all your code, without seeing the php that relates to what you showed me it's a little confusing! Quote Link to comment https://forums.phpfreaks.com/topic/168693-using-variables-from-different-code-blocks/#findComment-889975 Share on other sites More sharing options...
mikesta707 Posted August 3, 2009 Author Share Posted August 3, 2009 yeah sure, but there isn't much else, just building my array from a data base. I am afraid I have a different problem than I first thought. Let me explain what I am trying to do first, bear with me. I take data from a database, and populate a 2d PHP array with it. via the following: $tot_perc = 0; $count = 0; $full_array = null; while (OCIFetchInto($do, $results, OCI_ASSOC)){ $r = $results; $threshold = 70; $style = ""; if ($r[COUNT_ALL] == 0){ $percent = 0; $percent = number_format($percent, 0); } else { $percent = $r[COUNT_POPULATED] / $r[COUNT_ALL]; $percent *= 100; $percent = number_format($percent, 0); } if ($percent < $threshold){ $style = "style='background-color:#FF0000'"; } else if ($percent > $treshold && $percent <= 89){ $style = "style='background-color:#FFFF00'"; } else if ($percent > 89){ $style = "style='background-color:#00FF00'"; } $temp_array = array($r[ORG_ID], $r[TABLE_NAME], $r[FIELD_NAME], $r[COUNT_ALL], $r[COUNT_POPULATED], $percent, $style); if ($full_array == null){ $full_array = array($temp_array); } else { array_push($full_array, $temp_array); } $tot_perc += $percent; $count++; } the query and execution of the query has been left out, but its just a simple oracle database query that does indeed work and get the correct information. What I want to do is create a button that populates a table I have with the information from the array. Since I can only build the array with PHP, and can only dynamically add stuff to a table with javascript, I am faced with the issue of having both of them talk to each other. I am able to build a 2-d javascript array correctly, with the correct data with the following code: echo "<script type=\"text/javascript\">"; echo "full_array = new Array();\n"; for ($i = 0; $i < count($full_array); $i++){ echo "full_array[$i] = new Array(7);\n"; echo "full_array[$i][0] = " . "\"" . $full_array[$i][0] . "\"" . ";\n"; echo "full_array[$i][1] = " . "\"" . $full_array[$i][1] . "\"" . ";\n"; echo "full_array[$i][2] = " . "\"" . $full_array[$i][2] . "\"" . ";\n"; echo "full_array[$i][3] = " . "\"" . $full_array[$i][3] . "\"" . ";\n"; echo "full_array[$i][4] = " . "\"" . $full_array[$i][4] . "\"" . ";\n"; echo "full_array[$i][5] = " . "\"" . $full_array[$i][5] . "\"" . ";\n"; echo "full_array[$i][6] = " . "\"" . $full_array[$i][6] . "\"" . ";\n"; } echo "</script>"; What I am having trouble with is a function that takes that array, and creates the table data. Currently my function looks like the following: function fill_table(){ var k = 1; string = "" for(i = 0; i < full_array.length; i++){ string += "<tr>"; string += "<td>" + (i + 1) + "</td>"; string += "<td>"; string += full_array[i][0]; string += "</td>"; string += "<td>"; string += full_array[i][1]; string += "</td>"; string += "<td>"; string += full_array[i][2]; string += "</td>"; string += "<td>"; string += full_array[i][3]; string += "</td>"; string += "<td>"; string += full_array[i][4]; string += "</td>"; string += "<td full_array[i][6]>"; string += full_array[i][5] + "%"; string += "</td>"; string += "</tr>"; k++; } div = document.getElementById("data");//the id of the div tag with the table div.innerHTML = div.innerHTML + string;//SHOULD fill out the table. does not } when the page begins, the following is already in the "data" div tag (i create it with php, I'm not sure why i didn't just write straight html, but its already working so...) echo "<div id=\"data\">"; echo "<table border='1'>"; echo "<tr>"; echo "<td>Row #</td>"; echo "<td>ORG_ID</td>"; echo "<td>"; echo "TABLE_NAME"; echo "</td>"; echo "<td>"; echo "FIELD_NAME"; echo "</td>"; echo "<td>"; echo "COUNT_ALL"; echo "</td>"; echo "<td>"; echo "COUNT_POPULATED"; echo "</td>"; echo "<td>"; echo "Percentage"; echo "</td>"; echo "</tr>"; What currently happens, is that the function does write the information from the array into the div tag, but it doesn't continue the table. the output looks like: [column name][column][column][etc][etc][etc] a string of the data from the array, not seperated into columns. just static text that has a rather strange format I am not sure what the problem is with my javascript or PHP. Edit: Note, When I do a call to the JS alert function, and alert the string variable from the fill_table() function, it does output the correct information, with the HTML tags and all. Is there a problem with the innerHTML method that I am missing? That seems to be the only part that doesn't work Quote Link to comment https://forums.phpfreaks.com/topic/168693-using-variables-from-different-code-blocks/#findComment-889996 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.