By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,889 Members | 1,044 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,889 IT Pros & Developers. It's quick & easy.

converting php variables into javascript

P: 25
Apache2 over XP Home, PHP5, PostgreSQL8.2
If this is not the correct forum for this, it can be reposted accordingly.
I have php variables/arrays that I want to display in a php webpage. What I am doing is using my server-side php script to generate javascript and convert my php data. The 1st bit of code is my php script, the 2nd is my javascript from my php webpage. I have 2 variables $numrows and $numfields, and 2 arrays $fieldname[] and $parts[][] (hopefully self-explanatory). In my php webpage I am using javascript to display a table with the converted values. You will notice in my javascript that numrow is used after numfield, yet its numfield that is showing as undefined. I am calling DisplayCustomers from my xmlhttprequest. Are there any Javascript/PHP people who can help my with this? Am I going into a dead end with this? Any advice/comments welcome!

rpjd.

php script
[php]
<?php
$connect=pg_connect("dbname=DatabaseName host=localhost user=User password=password");
if (!pg_connection_busy($connect))
{
$result=pg_query$connect, "select * from customer;");
}
$numrows=pg_numrows($result);
$numfields=pg_num_fields($result);
echo "<script language=\"Javascript\">\n";
echo "var numrows = new var;\n";
for ( $numrows as $key => $numrow)
{
echo "numrows[\"$key\"] = \"$numrow\";\n";
}
echo "</script>\n";
echo "var numfields = new var;\n";
for ( $numfields as $key => $numfield)
{
echo "numfields[\"$key\"] = \"$numfield\";\n";
}
echo "</script>\n";
$customers[][] = array("name" => "", "address" => "", "contact_no" => "");
for($i=0;$i<$numfields;$i++)
{
$fieldname[$i]=pg_field_name($result,$i);
echo "<script language=\"Javascript\">\n";
echo " var fieldname = new Array();\n";
foreach ($fieldname as $key => $fieldnames)
{
echo "fieldname[\"$key\"] = \"$fieldnames\";\n";
}
echo "</script>\n";
}

for($i=0;$i< $numrows;$i++)
{
for($j=0;$j<$numfields;$j++)
{
$customers[$i][$j]=pg_result($result,$i,$j);
echo "<script language=\"Javascript\">\n";
echo "var customers = new Array();\n";
foreach ($customers as $key => $customer)
{
echo "customers[\"$key\"] = \"$customer\";\n";
}
echo "</script<\n";
}
}
?>
[/php]

Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript" language="javascript">
  2.     function DisplayCustomers()
  3.     {
  4.         document.write('<tr>');
  5.         for (i=0; i < numfield; i++)
  6.             {
  7.             document.write('<td>' + fieldname[i] + '</td>');
  8.             }
  9.         document.write('</tr>');
  10.     }
  11.     for (i=0; i < numrow; i++)
  12.         {
  13.         document.write('<tr>');
  14.         for (j=0; j < numfields; j++)
  15.             {
  16.             document.write('<td>' + customers[i][j] + '</td>');
  17.             }
  18.         document.write('</tr>');
  19.             }
  20.     </script>
  21.  
Apr 29 '07 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.