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
    Regular Coder
    Join Date
    Oct 2012
    Location
    mother land --india
    Posts
    179
    Thanks
    39
    Thanked 2 Times in 2 Posts

    dynamic table in to array and encode as JSON

    Frnds,

    I am working on a project and as per its requirement I want below attributes to be fulfilled.

    a) I have a table as below

    ID name rollnumber group
    1 xyz 1123 A
    2 yzx 1124 B
    3 xza 1125 c

    now I want print this values in to array using while loop.

    expected output should be : [{ID:1, name:xyz, rollnumber:1123,group:A},
    {ID:2, name:yzx, rollnumber:1124,group:B},
    {ID:3, name:xza, rollnumber:1125,group:c}]


    Below is the code I have worked out but it is not giving output as above:

    PHP Code:
    $data mysql_query("SELECT * FROM table ") or die(mysql_error()); 
    while(
    $info mysql_fetch_array$data ))
     {
    $i=0;
        while(
    $i 4)
        {
           
    $json['aaData'][] = array(
            
    $VALUE => $info[$VALUE],
        );
        
    $i++;
        }
        }
        }    
        echo 
    json_encode($json); 
    please suggest

  • #2
    Regular Coder
    Join Date
    Dec 2011
    Posts
    106
    Thanks
    5
    Thanked 17 Times in 17 Posts
    PHP Code:
    $e = array();
    $data mysql_query("SELECT * FROM table ") or die(mysql_error()); 
    while(
    $info mysql_fetch_array$data ))
     {
    $e[] = $info;
      }    
    print 
    json_encode($e); 
    True courage is about knowing not when to take a life, but when to spare one. PDO Tutorial

  • Users who have thanked Strider64 for this post:

    nani_nisha06 (06-17-2014)


  •  

    Posting Permissions

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