Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Jul 2009
    Posts
    39
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Help transfering PHP array to JS

    Well i just got booted off the server after writing an essay here so this is the short version

    -Ive never worked html php or javascript, so any help, tips, etc is appreciated.
    -Im making a database for some1 to store client information on and they want it to be client-side
    -Each row represents 1 client
    -Made a working php version, now trying to make a working JS version

    heres the PHP version that prints the table correctly:
    ($data = 2D array of all information, $columns = array of column headers, $numRows = number of rows/clients)

    Code:
    function printTable()
          {
            echo '<table class = "halloween" border = "1">';
            echo '<tr>';
            foreach($this->columns as $header => $order)
            {
                    echo '<th>';
                    echo $header;
                    echo '</th>';
            }
            for($i = 0; $i < $this->numRows; $i++)
            {
                    echo '<tr>';
                    foreach($this->data[$i] as $value)
                    {
                            echo '<td>';
                            if(!$value=="")
                                    echo $value;
                            else
                                    echo '<input type = "text" size = 10>';
                            echo '</td>';
                    }
                    echo '</tr>';
            }
            echo '</table>';
          }
    that works!!

    now i am trying to pass this information to JS file so it will be client side

    I started by trying to pass the array of column headers, heres what i got

    Code:
     function printJSTable()
          {
            echo '<script src = "java_script.js">';
            echo 'var js_Array = new Array();\n';
            foreach($this->columns as $header => $order)
            {
                    echo 'js_Array.push($header);\n';
            }
            echo '</script>';
            echo '<input type = "button" value ="click" onclick = drawTable("js_Array")';
          }
    and here is what drawTable() currently is

    Code:
    function drawTable(columns)
    {
        alert("Hello World");
        for(i=0; i<columns.length; i++)
            {
                alert(columns[i]);
            }
    }
    When the button "click" is clicked it alerts "Hello World" and then alerts "j" then "s" then "_"

    well it alerts letter by letter the name of the array

    any tips? even tips addressing different issues would be greatly appreciated, any help at all really, even big picture stuff

    thanks!

  • #2
    New Coder
    Join Date
    Jul 2009
    Posts
    39
    Thanks
    12
    Thanked 0 Times in 0 Posts
    As was pointed out in another post, the actual database is going to be server side, I meant that I want the representation of the database (aka a table) to be client side. I alrdy have a table working in PHP but finding it very difficult to transfer that to JavaScript

  • #3
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,461
    Thanks
    11
    Thanked 600 Times in 580 Posts
    Quote Originally Posted by JustinC474 View Post
    I alrdy have a table working in PHP but finding it very difficult to transfer that to JavaScript
    difficult?
    you must not have heard about php's json_encode()...

    it's a one-call conversion from a php array to json, which is actually javascript.
    couldn't be any simpler.

    it's a whole lot faster and easier than trying to build js from strings in php loops.
    my site (updated 2014/10/20)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.3, IE11:9.2, IE9:2.7, IE10:2.6, FF:16.8, CH:47.5, SF:7.8, NON-MOUSE:37%

  • Users who have thanked rnd me for this post:

    JustinC474 (07-28-2009)

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,613
    Thanks
    80
    Thanked 4,635 Times in 4,597 Posts
    LOL! Oh, he says. So you don't have to write code like I did???

  • #5
    New Coder
    Join Date
    Jul 2009
    Posts
    39
    Thanks
    12
    Thanked 0 Times in 0 Posts
    haha guess so. thanks for the help!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •