Thursday, July 14, 2011

A php sqlite to json converter page

Next I build basically a quick converter page to convert an sqlite query result into json

function jsonOutput($query)
{
  #json convert
  $ofirst = true;
  print "[\n";
  foreach ($query as $row)
  {
    if($ofirst) $ofirst = false;
    else       print ",\n";
    
    $first = true;
    print "{";
    foreach ($row as $key => $value)
    {
      if($first) $first = false;
      else       print ",";
      if(is_numeric($value))
        print $key . ': ' . $value;
      else
        print $key . ': "' . $value . '"';
    }
    print "}";
  }
  print "\n]\n";
}

3 comments:

  1. what's wrong with json_encode()

    ReplyDelete
  2. Simple nothing... Apart from the fact that it didnt work/exist(cant remember which) in the system i was using. Im all for libs and builtin functions they save me alot of time and sweat but it has to be at your finger tips, documented and findable in the documentation in less time that it will take me to hack it together or its pointless.

    ReplyDelete
  3. Anyway thanks for pointing it out. It is always good practice to clean up code when you can or learn better..

    ReplyDelete