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 4 of 4
  1. #1
    Regular Coder
    Join Date
    Jun 2006
    Posts
    225
    Thanks
    6
    Thanked 3 Times in 3 Posts

    Construct Javascript array of objects

    I want to construct a Javascript array of objects using a set of data from MySQL. The format should be as follows:
    Code:
    var arr = [{ //Begin array literal, begin object literal
    id: 1,
    title: "Test",
    date: new Date(2008,1,1,1,0,0)
    }, //Close object literal, move to next array element
    {
    id: 2,
    title: "Test2",
    date: new Date(2008,1,1,1,0,0)
    }] //Close array literal, note no comma on the last array element
    The array is constructed through a while loop of the MySQL result. My problem is that I don't want to have a comma at the end before I close the array. This is the code I have at the moment:

    PHP Code:
    //MySQL data has already been retrieved
    echo 'var arr = [';
      while(
    $row mysql_fetch_array($result)) {
        echo 
    '{ id: '.$row['id'];
        echo 
    ', title: "'.$row['title'].'"';
        echo 
    ', date: new Date('.$row['date'].') },'//Note comma, results in a comma at the end of the array
      
    }
    echo 
    '];';

    How can I stop the final comma being added?

  • #2
    Senior Coder shyam's Avatar
    Join Date
    Jul 2005
    Posts
    1,563
    Thanks
    2
    Thanked 163 Times in 160 Posts
    PHP Code:
    //MySQL data has already been retrieved
    $first true;
    echo 
    'var arr = [';
      while(
    $row mysql_fetch_array($result)) {
        if ( !
    $first ) {
          echo 
    ',';
        } else {
          
    $first false;
        }
        echo 
    '{ id: '.$row['id'];
        echo 
    ', title: "'.$row['title'].'"';
        echo 
    ', date: new Date('.$row['date'].') }'//look ma, no comma
      
    }
    echo 
    '];';

    You never have to change anything you got up in the middle of the night to write. -- Saul Bellow

  • #3
    Regular Coder
    Join Date
    Dec 2007
    Location
    Nebraska
    Posts
    113
    Thanks
    0
    Thanked 2 Times in 2 Posts
    PHP Code:
    $first true;
    echo ;
    $objs = array();
      while(
    $row mysql_fetch_array($result)) {
          
    $objs[] =<<<JSOBJ
        {id:{$row['id']},title:"{$row['title']}",date:new Date({$row['date']})}
    JSOBJ;
      }
    echo 
    'var arr = ['.implode(',',$objs).'];';

    In PHP 5.x (I think it can also be installed in 4.x) there is a JSON extension that will convert PHP data structures to JSON and also do the reverse. If that extension is present, it would be much quicker to use it.
    Deliver yesterday, code today, think tomorrow.

  • #4
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    yeah, use json_encode or you're going to get caught out with any special characters that might be in your data.
    My thoughts on some things: http://codemeetsmusic.com
    And my scrapbook of cool things: http://gjones.tumblr.com


  •  

    Posting Permissions

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