orionlogic Posted May 18, 2007 Share Posted May 18, 2007 hi all, I have a database which stores years. 1 column with many rows for simplicity reasons. Something like 1970,1980,2003 etc... i have a javascript function that gets these variables and write the results into page. the place that i stuck is, i cannot able to put each variable and show each result. I know there must be a for or while loop for javascipt or for php but couldn't manage to do. that means : How should i put php variables that comes form db, in an array, than use the javascript function for each of them and write the results. below is the code. <?php $conn = mysql_connect("localhost", "root", "****"); mysql_select_db("test") or die ("mysql_error()"); $list = mysql_query("select * FROM years"); while ($row = mysql_fetch_array($list)) { $years = $row['years']; echo "<br>"; echo "<div id=\"yeardiv\"></div>"; echo " <SCRIPT TYPE=\"text/javascript\" LANGUAGE=\"JavaScript\"> dbYear = new Date($years);// try to put php results.. function calcYear(){ yearNow = new Date(); result = dbYear.getYear() - yearNow.getYear(); delete yearNow; if(result < 1975){ document.getElementById('yeardiv').innerHTML=\"this year is lower!\"; } else { document.getElementById('yeardiv').innerHTML=\"this year is higher!\"; } } window.onload=function(){calcYear();} </script> "; } ?> Thanks a lot! Quote Link to comment https://forums.phpfreaks.com/topic/52001-php-variables-into-javascript-function/ Share on other sites More sharing options...
Psycho Posted May 18, 2007 Share Posted May 18, 2007 Is there a reason you want to use javascript to display the records to the page? Why not do it with PHP? PHP and Javascript can't interact with each other in the manner you have above. PHP first creates the page. Once that is complete, the page is delivered to the browser. If any javascript exists in the page, it will then be run by the browser. In your code above $year would have no value within the javascript. Here is how you could do it within PHP: <?php $conn = mysql_connect("localhost", "root", "****"); mysql_select_db("test") or die ("mysql_error()"); $list = mysql_query("select * FROM years") or die ("mysql_error()"); if (!mysql_num_rows()) { echo "There were no results."; } else { echo "<br><div id=\"yeardiv\">"; $currentYear = date("Y"); while ($row = mysql_fetch_array($list)) { $year = $row['years']; if ( ($year-$currentYear) < 1975 ) { echo "$year: This year is lower!"; } else { echo "$year: This year is higher!"; } } echo "</div>"; } ?> If you really need to do this in Javascript, that can be done as well. Quote Link to comment https://forums.phpfreaks.com/topic/52001-php-variables-into-javascript-function/#findComment-256355 Share on other sites More sharing options...
orionlogic Posted May 18, 2007 Author Share Posted May 18, 2007 thanks for your reply, actually yes, there is a reason. I want to use the ability of javascript to write a timer,for each of the results.Like a countdown clock. For example in database there will be appoinment dates in date format and javascript writes each timer for each appointment in the same page. I don't think php handle this but can only populate the javascript function array. That's the way i think of course am i right? Is there a reason you want to use javascript to display the records to the page? Why not do it with PHP? PHP and Javascript can't interact with each other in the manner you have above. PHP first creates the page. Once that is complete, the page is delivered to the browser. If any javascript exists in the page, it will then be run by the browser. In your code above $year would have no value within the javascript. Here is how you could do it within PHP: <?php $conn = mysql_connect("localhost", "root", "****"); mysql_select_db("test") or die ("mysql_error()"); $list = mysql_query("select * FROM years") or die ("mysql_error()"); if (!mysql_num_rows()) { echo "There were no results."; } else { echo "<br><div id=\"yeardiv\">"; $currentYear = date("Y"); while ($row = mysql_fetch_array($list)) { $year = $row['years']; if ( ($year-$currentYear) < 1975 ) { echo "$year: This year is lower!"; } else { echo "$year: This year is higher!"; } } echo "</div>"; } ?> If you really need to do this in Javascript, that can be done as well. Quote Link to comment https://forums.phpfreaks.com/topic/52001-php-variables-into-javascript-function/#findComment-256364 Share on other sites More sharing options...
Psycho Posted May 18, 2007 Share Posted May 18, 2007 Here you go: <?php $conn = mysql_connect("localhost", "root", "****"); mysql_select_db("test") or die ("mysql_error()"); $list = mysql_query("select * FROM years") or die ("mysql_error()"); if (!mysql_num_rows()) { echo "There were no results."; } else { echo "<html><head>\n"; echo "<script type=\"text/javascript\">\n"; //Create the array in javascript echo " yearAry = new Array();\n"; $counter = 0; while ($row = mysql_fetch_array($list)) { echo " yearAry[$counter] = $row[years];\n"; $counter++; } echo " today = new Date();\n"; echo " yearNow = today.getFullYear()\n"; //Create the js function echo " function calcYear(){\n"; echo " divObj = document.getElementById('yeardiv');\n"; echo " for (i=0; i<yearAry.length; i++) {\n"; echo " if ( (yearAry[i]-yearNow) < 1975 ) {\n"; echo " txt = 'This year is lower!';\n"; echo " } else {"; echo " txt = 'This year is higer!';\n"; echo " }\n"; echo " divObj.innerHTML = divObj.innerHTML + '<br>' + yearAry[i] + ': ' + txt;\n"; echo " }\n"; echo " }\n"; echo " window.onload=function(){calcYear();}\n"; echo "</script>\n"; echo "</head><body>\n"; echo "<br><div id=\"yeardiv\"></div>\n"; echo "</body></html>\n"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/52001-php-variables-into-javascript-function/#findComment-256583 Share on other sites More sharing options...
orionlogic Posted May 19, 2007 Author Share Posted May 19, 2007 Thanks a lot. thats what i wanted. i will inform if i stuck somewhere Quote Link to comment https://forums.phpfreaks.com/topic/52001-php-variables-into-javascript-function/#findComment-256914 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.