converting php variables into javascript

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rpjd
    New Member
    • Mar 2007
    • 25

    converting php variables into javascript

    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 DisplayCustomer s 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_con nect("dbname=Da tabaseName host=localhost user=User password=passwo rd");
    if (!pg_connection _busy($connect) )
    {
    $result=pg_quer y$connect, "select * from customer;");
    }
    $numrows=pg_num rows($result);
    $numfields=pg_n um_fields($resu lt);
    echo "<script language=\"Java script\">\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<$nu mfields;$i++)
    {
    $fieldname[$i]=pg_field_name( $result,$i);
    echo "<script language=\"Java script\">\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<$nu mfields;$j++)
    {
    $customers[$i][$j]=pg_result($res ult,$i,$j);
    echo "<script language=\"Java script\">\n";
    echo "var customers = new Array();\n";
    foreach ($customers as $key => $customer)
    {
    echo "customers[\"$key\"] = \"$customer\";\ n";
    }
    echo "</script<\n";
    }
    }
    ?>
    [/php]

    Code:
    <script type="text/javascript" language="javascript">
    	function DisplayCustomers()
    	{
    		document.write('<tr>');
    		for (i=0; i < numfield; i++)
    			{
    			document.write('<td>' + fieldname[i] + '</td>');
    			}
    		document.write('</tr>');
    	}
    	for (i=0; i < numrow; i++)
    		{
    		document.write('<tr>');
    		for (j=0; j < numfields; j++)
    			{
    			document.write('<td>' + customers[i][j] + '</td>');
    			}
    		document.write('</tr>');
    			}
    	</script>
Working...