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 2 of 2
  1. #1
    New Coder
    Join Date
    Jun 2012
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Pushing to javascript array in php loop

    I am trying to push to the javascript array after looping 200 times through the member array to push every member that matches the department selected description into the javascript array., However it fails

    PHP Code:
    <? $i ;
        while( 
    $i 200)
        {
        if (
    $members[$i]['department'] == $depttts){
        
        echo 
    "<script type='text/javascript'>contacts.push({"NAME":"SirSaula","TITLE":"John","CONTACT":"bab","RESEARCH":"Ph.D."});</script>";
        }
        else {
        
    $i++;
        }
        }
        
    ?>

  • #2
    Senior Coder
    Join Date
    Sep 2010
    Posts
    2,025
    Thanks
    15
    Thanked 239 Times in 239 Posts
    Code:
    <script>
    myarray = [];
    <?php
    for ( $i = 0 ; $i < count($mystyle) ; $i++ )
    {
    echo "myarray.push('".$mystyle[$i]."');\n";
    }
    ?>
    </script>
    An example from one of my pages.


  •  

    Posting Permissions

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