RE: [PHP] Creating a javascript array from database data
I'm not sure if this is the problem but it is an important point. print \$text\; does not output anything to screen, as with all prints and echos it outputs it to html (in this case within a js function definition). Try viewing the source produced. Tim -- From: Neil Freeman [SMTP:[EMAIL PROTECTED]] Sent: 15 August 2001 18:10 To: Sheridan Saint-Michel Cc: php-general Subject: Re: [PHP] Creating a javascript array from database data Thanks a lot for your help Sheridan, This now appears to work ok :) but if I remove the: print \$text\; line (as it is not required), I receive errors complaining about a syntax error (related to what?) and that 'text' is undefined. As this line simply outputs the value to screen why should this cause a problem if it is removed? Or could it be some other problem? Neil PS: Here is my updated code: -- html head titleMenus test/title /head body bgcolor=white ?php $dbhost = 'localhost'; $dbuser = 'guest'; $dbpass = 'guest'; $dbname = 'IFE'; $dbtable = 'menus'; //-- DATABASE CONNECTION // mysql_connect($dbhost,$dbuser,$dbpass) or die (Unable to connect to database); mysql_select_db($dbname) or die (Unable to select database); $sql= SELECT * FROM $dbtable; $result = mysql_query($sql); $number = mysql_numrows($result); $i = 0; if ($number == 0) print Error - No records found; elseif ($number 0) { echo Script Language=\JavaScript\\n; echo text = new Array(; while ($i $number) { $text = mysql_result($result, $i, name); $i++; if ($i $number) print ,; else print )\n; } echo /Script\n; } mysql_free_result($result); mysql_close(); ? /body /html -- Sheridan Saint-Michel wrote: *** This message was virus checked with: SAVI 3.48 last updated 14th August 2001 *** The thing to ALWAYS remember when working with both PHP and JavaScript is that PHP is Server-Side and JavaScript is Client-Side. What this mean is practical terms is that when going from JavaScript to PHP you have to submit or redirect back to the server... and when going from PHP to JavaScript (Like you are trying to do here) you have to make sure your PHP outputs JavaScript. So instead of printing $text you need to actually print the JavaScript... So in your case change the middle of your script to something like: $number = mysql_numrows($result); $i = 0; if ($number == 0) print Error - No records found; else { echo Script Language=\JavaScript\\n; echo text = new Array(; while ($i $number) { $text = mysql_result($result, $i, name); print \$text\; $i++; if ($i $number) print ,; else print )\n; } echo /Script\n; } } Note: This is my quick *untested* fix done by modifying your code as little as possible. I would suggest you use mysql_fetch_row and use your loop to parse it rather than making several calls to mysql_result (especially if you are ever going to have more than a few entries in the DB). Sheridan Saint-Michel Website Administrator FoxJet, an ITW Company www.foxjet.com - Original Message - From: Neil Freeman [EMAIL PROTECTED] To: PHP General [EMAIL PROTECTED] Sent: Wednesday, August 15, 2001 11:16 AM Subject: [PHP] Creating a javascript array from database data Hi there, Well after a few hours roaming around various websites I am at a loss. Here is what I am trying to do: 1) Access a MySQL database which contains 1 table 2) Read the records from this table 3) Store the values returned from this table into javascript array elements, ie, if I get the values dog, cat and cow back I want these stored in an array as such: myArray[0] = valueReturned1 myArray[1] = valueReturned2
Re: [PHP] Creating a javascript array from database data (Off-List)
Thanks for the clarification Sheridan - I think it must have been end of day madness on my part :) I see what's going on now with the 'print' line. Cheers, Neil Sheridan Saint-Michel wrote: *** This message was virus checked with: SAVI 3.48 last updated 14th August 2001 *** Think in terms of output... The JavaScript is literally going to try and run the output of the PHP (It doesn't even know PHP exists). PHP is outputting the source code for the HTML and JavaScript. So what you are trying to do is get PHP to take dog,cat,cow from MySQL and output: Script Language=JavaScript text = new Array(dog,cat,cow) /Script This block of code does exactly that, with the print\$text\; filling in dog cat and cow. I've added comments so you can see how each line produces the above (I am using C-Style comments as the // gets mucked up in e-mail) The parts in [] are comments within comments to show flow control and are not output. else { /* Script Language=JavaScript */ echo Script Language=\JavaScript\\n; /* text=new Array( */ echo text = new Array(; while ($i $number) $text = mysql_result($result, $i, name); /* dog [first time] */ /* cat [second time] */ /* cow [third time] */ print \$text\; $i++; if ($i $number) /* , [first time] */ /* , [second time] */ print ,; else /* ) [third time] */ print )\n; } /* /Script */ echo /Script\n; } So if you can follow that (just walk all the way through the loop for each of the three passes) you can see that no part of the code (including the print \$text\;) is unnecessary Hope this helps Sheridan Saint-Michel Website Administrator FoxJet, an ITW Company www.foxjet.com - Original Message - From: Neil Freeman [EMAIL PROTECTED] To: Sheridan Saint-Michel [EMAIL PROTECTED] Cc: php-general [EMAIL PROTECTED] Sent: Wednesday, August 15, 2001 12:09 PM Subject: Re: [PHP] Creating a javascript array from database data Thanks a lot for your help Sheridan, This now appears to work ok :) but if I remove the: print \$text\; line (as it is not required), I receive errors complaining about a syntax error (related to what?) and that 'text' is undefined. As this line simply outputs the value to screen why should this cause a problem if it is removed? Or could it be some other problem? Neil PS: Here is my updated code: Snip -- Email: [EMAIL PROTECTED] [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] Creating a javascript array from database data
Yeah thanks Tim. 5:30 in the afternoon does bad things to my logical thinking. Not the best time to try and learn PHP :) Neil Tim Ward wrote: I'm not sure if this is the problem but it is an important point. print \$text\; does not output anything to screen, as with all prints and echos it outputs it to html (in this case within a js function definition). Try viewing the source produced. Tim -- From: Neil Freeman [SMTP:[EMAIL PROTECTED]] Sent: 15 August 2001 18:10 To: Sheridan Saint-Michel Cc: php-general Subject: Re: [PHP] Creating a javascript array from database data Thanks a lot for your help Sheridan, This now appears to work ok :) but if I remove the: print \$text\; line (as it is not required), I receive errors complaining about a syntax error (related to what?) and that 'text' is undefined. As this line simply outputs the value to screen why should this cause a problem if it is removed? Or could it be some other problem? Neil PS: Here is my updated code: -- html head titleMenus test/title /head body bgcolor=white ?php $dbhost = 'localhost'; $dbuser = 'guest'; $dbpass = 'guest'; $dbname = 'IFE'; $dbtable = 'menus'; //-- DATABASE CONNECTION // mysql_connect($dbhost,$dbuser,$dbpass) or die (Unable to connect to database); mysql_select_db($dbname) or die (Unable to select database); $sql= SELECT * FROM $dbtable; $result = mysql_query($sql); $number = mysql_numrows($result); $i = 0; if ($number == 0) print Error - No records found; elseif ($number 0) { echo Script Language=\JavaScript\\n; echo text = new Array(; while ($i $number) { $text = mysql_result($result, $i, name); $i++; if ($i $number) print ,; else print )\n; } echo /Script\n; } mysql_free_result($result); mysql_close(); ? /body /html -- Sheridan Saint-Michel wrote: *** This message was virus checked with: SAVI 3.48 last updated 14th August 2001 *** The thing to ALWAYS remember when working with both PHP and JavaScript is that PHP is Server-Side and JavaScript is Client-Side. What this mean is practical terms is that when going from JavaScript to PHP you have to submit or redirect back to the server... and when going from PHP to JavaScript (Like you are trying to do here) you have to make sure your PHP outputs JavaScript. So instead of printing $text you need to actually print the JavaScript... So in your case change the middle of your script to something like: $number = mysql_numrows($result); $i = 0; if ($number == 0) print Error - No records found; else { echo Script Language=\JavaScript\\n; echo text = new Array(; while ($i $number) { $text = mysql_result($result, $i, name); print \$text\; $i++; if ($i $number) print ,; else print )\n; } echo /Script\n; } } Note: This is my quick *untested* fix done by modifying your code as little as possible. I would suggest you use mysql_fetch_row and use your loop to parse it rather than making several calls to mysql_result (especially if you are ever going to have more than a few entries in the DB). Sheridan Saint-Michel Website Administrator FoxJet, an ITW Company www.foxjet.com - Original Message - From: Neil Freeman [EMAIL PROTECTED] To: PHP General [EMAIL PROTECTED] Sent: Wednesday, August 15, 2001 11:16 AM Subject: [PHP] Creating a javascript array from database data Hi there, Well after a few hours roaming around various websites I am at a loss. Here is what I am trying to do: 1) Access a MySQL database which contains 1 table 2) Read the records from
[PHP] Creating a javascript array from database data
Hi there, Well after a few hours roaming around various websites I am at a loss. Here is what I am trying to do: 1) Access a MySQL database which contains 1 table 2) Read the records from this table 3) Store the values returned from this table into javascript array elements, ie, if I get the values dog, cat and cow back I want these stored in an array as such: myArray[0] = valueReturned1 myArray[1] = valueReturned2 myArray[2] = valueReturned3 You get the idea. Problem being that I cannot work out how to implement the javascript section of this. At the moment my php script writes the values returned from the database to screen but I require these to be stored in a javascript array. Please can someone help me before I go mad :) Here is my current .php script: -- html head titleMenus test/title /head body bgcolor=white ?php $dbhost = 'localhost'; $dbuser = 'guest'; $dbpass = 'guest'; $dbname = 'IFE'; $dbtable = 'menus'; //-- DATABASE CONNECTION // mysql_connect($dbhost,$dbuser,$dbpass) or die (Unable to connect to database); mysql_select_db($dbname) or die (Unable to select database); $sql= SELECT * FROM $dbtable; $result = mysql_query($sql); $number = mysql_numrows($result); $i = 0; if ($number == 0) print Error - No records found; elseif ($number 0) { while ($i $number) { $text = mysql_result($result, $i, name); print $text; $i++; } } mysql_free_result($result); mysql_close(); ? /body /html -- Thanks, Neil Email: [EMAIL PROTECTED] [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
RE: [PHP] Creating a javascript array from database data
-- while you are looping through the date jump in and out of php like this to populate the javascript array script language=Javascript function DataArray() { a = new Array() ? while ($i $number) { $text = mysql_result($result, $i, name); ? a[?=$i?] = ?=$text? ? $i++; } ? } HTH Jon Jon Farmer Systems Programmer, Entanet www.enta.net Tel 01952 428969 Mob 07968 524175 PGP Key available, send blank email to [EMAIL PROTECTED] -Original Message- From: Neil Freeman [mailto:[EMAIL PROTECTED]] Sent: 15 August 2001 17:16 To: PHP General Subject: [PHP] Creating a javascript array from database data Hi there, Well after a few hours roaming around various websites I am at a loss. Here is what I am trying to do: 1) Access a MySQL database which contains 1 table 2) Read the records from this table 3) Store the values returned from this table into javascript array elements, ie, if I get the values dog, cat and cow back I want these stored in an array as such: myArray[0] = valueReturned1 myArray[1] = valueReturned2 myArray[2] = valueReturned3 You get the idea. Problem being that I cannot work out how to implement the javascript section of this. At the moment my php script writes the values returned from the database to screen but I require these to be stored in a javascript array. Please can someone help me before I go mad :) Here is my current .php script: -- html head titleMenus test/title /head body bgcolor=white ?php $dbhost = 'localhost'; $dbuser = 'guest'; $dbpass = 'guest'; $dbname = 'IFE'; $dbtable = 'menus'; //-- DATABASE CONNECTION // mysql_connect($dbhost,$dbuser,$dbpass) or die (Unable to connect to database); mysql_select_db($dbname) or die (Unable to select database); $sql= SELECT * FROM $dbtable; $result = mysql_query($sql); $number = mysql_numrows($result); $i = 0; if ($number == 0) print Error - No records found; elseif ($number 0) { while ($i $number) { $text = mysql_result($result, $i, name); print $text; $i++; } } mysql_free_result($result); mysql_close(); ? /body /html -- Thanks, Neil Email: [EMAIL PROTECTED] [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] Creating a javascript array from database data
The thing to ALWAYS remember when working with both PHP and JavaScript is that PHP is Server-Side and JavaScript is Client-Side. What this mean is practical terms is that when going from JavaScript to PHP you have to submit or redirect back to the server... and when going from PHP to JavaScript (Like you are trying to do here) you have to make sure your PHP outputs JavaScript. So instead of printing $text you need to actually print the JavaScript... So in your case change the middle of your script to something like: $number = mysql_numrows($result); $i = 0; if ($number == 0) print Error - No records found; else { echo Script Language=\JavaScript\\n; echo text = new Array(; while ($i $number) { $text = mysql_result($result, $i, name); print \$text\; $i++; if ($i $number) print ,; else print )\n; } echo /Script\n; } } Note: This is my quick *untested* fix done by modifying your code as little as possible. I would suggest you use mysql_fetch_row and use your loop to parse it rather than making several calls to mysql_result (especially if you are ever going to have more than a few entries in the DB). Sheridan Saint-Michel Website Administrator FoxJet, an ITW Company www.foxjet.com - Original Message - From: Neil Freeman [EMAIL PROTECTED] To: PHP General [EMAIL PROTECTED] Sent: Wednesday, August 15, 2001 11:16 AM Subject: [PHP] Creating a javascript array from database data Hi there, Well after a few hours roaming around various websites I am at a loss. Here is what I am trying to do: 1) Access a MySQL database which contains 1 table 2) Read the records from this table 3) Store the values returned from this table into javascript array elements, ie, if I get the values dog, cat and cow back I want these stored in an array as such: myArray[0] = valueReturned1 myArray[1] = valueReturned2 myArray[2] = valueReturned3 You get the idea. Problem being that I cannot work out how to implement the javascript section of this. At the moment my php script writes the values returned from the database to screen but I require these to be stored in a javascript array. Please can someone help me before I go mad :) Here is my current .php script: -- html head titleMenus test/title /head body bgcolor=white ?php $dbhost = 'localhost'; $dbuser = 'guest'; $dbpass = 'guest'; $dbname = 'IFE'; $dbtable = 'menus'; file://-- DATABASE CONNECTION // mysql_connect($dbhost,$dbuser,$dbpass) or die (Unable to connect to database); mysql_select_db($dbname) or die (Unable to select database); $sql= SELECT * FROM $dbtable; $result = mysql_query($sql); $number = mysql_numrows($result); $i = 0; if ($number == 0) print Error - No records found; elseif ($number 0) { while ($i $number) { $text = mysql_result($result, $i, name); print $text; $i++; } } mysql_free_result($result); mysql_close(); ? /body /html -- Thanks, Neil -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] Creating a javascript array from database data
Thanks a lot for your help Sheridan, This now appears to work ok :) but if I remove the: print \$text\; line (as it is not required), I receive errors complaining about a syntax error (related to what?) and that 'text' is undefined. As this line simply outputs the value to screen why should this cause a problem if it is removed? Or could it be some other problem? Neil PS: Here is my updated code: -- html head titleMenus test/title /head body bgcolor=white ?php $dbhost = 'localhost'; $dbuser = 'guest'; $dbpass = 'guest'; $dbname = 'IFE'; $dbtable = 'menus'; //-- DATABASE CONNECTION // mysql_connect($dbhost,$dbuser,$dbpass) or die (Unable to connect to database); mysql_select_db($dbname) or die (Unable to select database); $sql= SELECT * FROM $dbtable; $result = mysql_query($sql); $number = mysql_numrows($result); $i = 0; if ($number == 0) print Error - No records found; elseif ($number 0) { echo Script Language=\JavaScript\\n; echo text = new Array(; while ($i $number) { $text = mysql_result($result, $i, name); $i++; if ($i $number) print ,; else print )\n; } echo /Script\n; } mysql_free_result($result); mysql_close(); ? /body /html -- Sheridan Saint-Michel wrote: *** This message was virus checked with: SAVI 3.48 last updated 14th August 2001 *** The thing to ALWAYS remember when working with both PHP and JavaScript is that PHP is Server-Side and JavaScript is Client-Side. What this mean is practical terms is that when going from JavaScript to PHP you have to submit or redirect back to the server... and when going from PHP to JavaScript (Like you are trying to do here) you have to make sure your PHP outputs JavaScript. So instead of printing $text you need to actually print the JavaScript... So in your case change the middle of your script to something like: $number = mysql_numrows($result); $i = 0; if ($number == 0) print Error - No records found; else { echo Script Language=\JavaScript\\n; echo text = new Array(; while ($i $number) { $text = mysql_result($result, $i, name); print \$text\; $i++; if ($i $number) print ,; else print )\n; } echo /Script\n; } } Note: This is my quick *untested* fix done by modifying your code as little as possible. I would suggest you use mysql_fetch_row and use your loop to parse it rather than making several calls to mysql_result (especially if you are ever going to have more than a few entries in the DB). Sheridan Saint-Michel Website Administrator FoxJet, an ITW Company www.foxjet.com - Original Message - From: Neil Freeman [EMAIL PROTECTED] To: PHP General [EMAIL PROTECTED] Sent: Wednesday, August 15, 2001 11:16 AM Subject: [PHP] Creating a javascript array from database data Hi there, Well after a few hours roaming around various websites I am at a loss. Here is what I am trying to do: 1) Access a MySQL database which contains 1 table 2) Read the records from this table 3) Store the values returned from this table into javascript array elements, ie, if I get the values dog, cat and cow back I want these stored in an array as such: myArray[0] = valueReturned1 myArray[1] = valueReturned2 myArray[2] = valueReturned3 You get the idea. Problem being that I cannot work out how to implement the javascript section of this. At the moment my php script writes the values returned from the database to screen but I require these to be stored in a javascript array. Please can someone help me before I go mad :) Here is my current .php script: -- html head titleMenus test/title /head body bgcolor=white ?php $dbhost = 'localhost'; $dbuser = 'guest'; $dbpass = 'guest'; $dbname = 'IFE'; $dbtable = 'menus'; file://-- DATABASE CONNECTION // mysql_connect($dbhost,$dbuser,$dbpass) or die (Unable to connect to database); mysql_select_db($dbname) or die (Unable to select database); $sql= SELECT * FROM $dbtable; $result = mysql_query($sql); $number = mysql_numrows($result); $i = 0; if ($number == 0) print Error - No records found; elseif ($number 0) { while ($i $number) { $text = mysql_result($result, $i, name); print $text; $i++; } } mysql_free_result($result); mysql_close(); ? /body /html -- Thanks, Neil -- Email: [EMAIL PROTECTED] [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED
Re: [PHP] Creating a javascript array from database data (Off-List)
Think in terms of output... The JavaScript is literally going to try and run the output of the PHP (It doesn't even know PHP exists). PHP is outputting the source code for the HTML and JavaScript. So what you are trying to do is get PHP to take dog,cat,cow from MySQL and output: Script Language=JavaScript text = new Array(dog,cat,cow) /Script This block of code does exactly that, with the print\$text\; filling in dog cat and cow. I've added comments so you can see how each line produces the above (I am using C-Style comments as the // gets mucked up in e-mail) The parts in [] are comments within comments to show flow control and are not output. else { /* Script Language=JavaScript */ echo Script Language=\JavaScript\\n; /* text=new Array( */ echo text = new Array(; while ($i $number) $text = mysql_result($result, $i, name); /* dog [first time] */ /* cat [second time] */ /* cow [third time] */ print \$text\; $i++; if ($i $number) /* , [first time] */ /* , [second time] */ print ,; else /* ) [third time] */ print )\n; } /* /Script */ echo /Script\n; } So if you can follow that (just walk all the way through the loop for each of the three passes) you can see that no part of the code (including the print \$text\;) is unnecessary Hope this helps Sheridan Saint-Michel Website Administrator FoxJet, an ITW Company www.foxjet.com - Original Message - From: Neil Freeman [EMAIL PROTECTED] To: Sheridan Saint-Michel [EMAIL PROTECTED] Cc: php-general [EMAIL PROTECTED] Sent: Wednesday, August 15, 2001 12:09 PM Subject: Re: [PHP] Creating a javascript array from database data Thanks a lot for your help Sheridan, This now appears to work ok :) but if I remove the: print \$text\; line (as it is not required), I receive errors complaining about a syntax error (related to what?) and that 'text' is undefined. As this line simply outputs the value to screen why should this cause a problem if it is removed? Or could it be some other problem? Neil PS: Here is my updated code: Snip -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]