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
    Senior Coder
    Join Date
    May 2006
    Posts
    1,680
    Thanks
    28
    Thanked 4 Times in 4 Posts

    How do I get data from php array into clientside array for javascript ) ?

    Hi,

    I have a mysql table contain a glossary
    which I would like to accesses from the web-browser
    so I guess I need to get the data into javascript or some
    other clientside code.

    The table is simple, just two columns so could be handled
    by an associatve array.

    It contains about 2000 rows and I want to use it so that
    when the mouse hovers over a technical word, the glossary
    explanation is show in a window.

    What route would you suggest ?


    Thanks.


    .
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,307
    Thanks
    23
    Thanked 612 Times in 611 Posts
    If you want this in JS why not just make a JS array? Hard code it and include it when you load the page that will require it.
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #3
    The fat guy next door VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    8,700
    Thanks
    6
    Thanked 1,011 Times in 984 Posts
    Quote Originally Posted by sunfighter View Post
    If you want this in JS why not just make a JS array? Hard code it and include it when you load the page that will require it.
    Right, echo the JS array in a PHP file that you include in the actual page (or echo it in the actual page right away).
    I’m not a JS pro per se but this is how I’d do it (logically):
    PHP Code:
    <script type="text/javascript">
    var glossary = [
    <?php
      
    foreach(whatever_your_array_is as $item) {
        echo(
    $item.','); // remember to include a function that removes the last comma or doesn’t put one in the first place
      
    }
    ?>
    ];
    // and there you have your JS array for further use on the page
    </script>
    But then again: Why use a JS array in the first place? You could as well just print the glossary info right next to each technical word, hide it with CSS, and simply use a CSS :hover to show it.
    Last edited by VIPStephan; 11-23-2013 at 06:25 PM.

  • #4
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    it would probably be easier and less hacky to just use PHP's json_encode to dump the variable as a Javascript object:

    Code:
    var glossary = <?php echo json_encode( $yourArray ); ?>;
    That's just reading the data. You'll still have to make the occurences of the keywords display popups.

  • #5
    Senior Coder
    Join Date
    May 2006
    Posts
    1,680
    Thanks
    28
    Thanked 4 Times in 4 Posts
    Thanks for your input.

    Some good points ...


    Will work on it

    .
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.


  •  

    Posting Permissions

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