www.webdeveloper.com
Results 1 to 9 of 9

Thread: Pushing to javascript array in php loop

  1. #1
    Join Date
    Jun 2012
    Posts
    18

    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. #2
    Join Date
    Jan 2004
    Posts
    484
    The way you have it written it would loop forever.

  3. #3
    Join Date
    Jan 2004
    Posts
    484
    try this:

    PHP Code:
    <!DOCTYPE html>
    <head>
        <script>
            contacts = [];
        </script>
    </head>


    <? $i 
        while( 
    $i 200
        { 
             
                echo 
    "<script type='text/javascript'>contacts.push({\"NAME\":\"SirSaula\",\"TITLE\":\"John\",\"CONTACT\":\"bab\",\"RESEARCH\":\"Ph.D.\"});</script>"
                
    $i++; 
             
        } 
        
    ?>

    <script>
        
        alert(contacts);
    </script>

  4. #4
    Join Date
    Jun 2012
    Posts
    18
    Tried it, did not function properly.... hmm

  5. #5
    Join Date
    Jun 2012
    Posts
    18
    For some reason it won't echo the javascript call well smh

  6. #6
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,616
    If I understand correctly, I'd probably use a foreach() loop:
    PHP Code:
    <script type='text/javascript'>
    <?php
    foreach($members as $memb)
    {
        if (
    $memb['department'] == $depttts){
            echo 
    'contacts.push({"NAME":"SirSaula","TITLE":"John","CONTACT":"bab","RESEARCH":"Ph.D."});';
            
    // possible a "break;" here if you want to terminate loop once a match is found?
        
    }
    }
    ?>
    </script>
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  7. #7
    Join Date
    Jun 2012
    Posts
    18
    Your method worked now the final thing instead of strings i wish to push the entire php array contents into the javascript array

    My attempt is below but it did not work.

    PHP Code:
    <?php
    foreach($members as $memb)
    {
        if (
    $memb['department'] == $depttts){
            echo 
    'contacts.push({"NAME":<?php echo json_encode($memb[i]['firstname']); ?>,"TITLE":<?php echo json_encode($memb[i]['title']); ?>,"CONTACT":<?php echo json_encode($memb[i]['email']); ?>,"RESEARCH":"Ph.D."});';
            
    // possible a "break;" here if you want to terminate loop once a match is found?
        
    }
    }
    ?>

    Quote Originally Posted by NogDog View Post
    If I understand correctly, I'd probably use a foreach() loop:
    PHP Code:
    <script type='text/javascript'>
    <?php
    foreach($members as $memb)
    {
        if (
    $memb['department'] == $depttts){
            echo 
    'contacts.push({"NAME":"SirSaula","TITLE":"John","CONTACT":"bab","RESEARCH":"Ph.D."});';
            
    // possible a "break;" here if you want to terminate loop once a match is found?
        
    }
    }
    ?>
    </script>
    Last edited by sirsaula; 07-20-2012 at 05:04 PM.

  8. #8
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,616
    I'd let PHP do some of the work JSON-wise:
    PHP Code:
    <?php
    // test data:
    $members = array(
        array(
            
    'department' => 'good',
            
    'firstname'  => 'John',
            
    'title'      => 'Mr.',
            
    'email'      => 'foo@bar.com'
        
    ),
        array(
            
    'department' => 'bad',
            
    'firstname'  => 'Jack',
            
    'title'      => 'Dr.',
            
    'email'      => 'bar@foo.com'
        
    ),
        array(
            
    'department' => 'good',
            
    'firstname'  => 'Jane',
            
    'title'      => 'Ms.',
            
    'email'      => 'foo@bar.net'
        
    )
    );
    $depttts 'good';
    ?>
    <script type='text/javascript'>
    <?php
    foreach($members as $memb)
    {
        if (
    $memb['department'] == $depttts){
            
    $data = array(
                
    "NAME" => $memb['firstname'],
                
    "TITLE" => $memb['title'],
                
    "CONTACT" => $memb['email'],
                
    "RESEARCH" => "Ph.D."
            
    );
            
    $json json_encode($data);
            echo 
    "contacts.push($json);\n";
        }
    }
    ?>
    </script>
    Output:
    Code:
    <script type='text/javascript'>
    contacts.push({"NAME":"John","TITLE":"Mr.","CONTACT":"foo@bar.com","RESEARCH":"Ph.D."});
    contacts.push({"NAME":"Jane","TITLE":"Ms.","CONTACT":"foo@bar.net","RESEARCH":"Ph.D."});
    </script>
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  9. #9
    Join Date
    Jun 2012
    Posts
    18
    Works perfectly thanks!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles