Author Topic: Json data  (Read 10544 times)

newparam

  • Newbie
  • *
  • Posts: 9
    • View Profile
Json data
« on: March 03, 2014, 12:03:30 pm »
Hi,
   I am using paramquery grid to display data from database.
As that cannot be done directly i am using php code to convert
db data into json format using the following code(there is no problem with the php code).

<?php
$db=mysql_connect("localhost", "database", "pswd") or die('Could not connect');
mysql_select_db("trial_db", $db) or die('');

$result = mysql_query("SELECT * from service") or die('Could not query');

$rows = array();
while($r = mysql_fetch_assoc($result)) {
  $rows[] = $r;
}

$datajs= json_encode($rows);
mysql_close($db);

?>

i am trying to use the output of this php code in the grid directly but that is not working.
<script>
$(document).ready(function() {
var data = <?php echo $datajs ?>;
         

      
         obj = { width: 1000, height: 250, title: "Service Information",resizable:true,draggable:false, editable:false};
         obj.colModel = [{ title: "Hostname", width: 200, dataType: "string" },
         { title: "SSH", width:200, dataType: "string" },
         { title: "HTTP", width:200, dataType: "string", align: "right" },
         { title: "user", width: 150, dataType: "integer", align: "right"},
         { title: "Procs", width: 150, dataType: "integer", align: "right"}];
         
         
         obj.dataModel = { data: data };
         $("#tabs-2").pqGrid(obj);

         
      });

      </script>


i am trying to use the explanation provided in this
http://www.dyn-web.com/tutorials/php-js/json.php

Please help me. I need to put the php code in the same page as the javascript, as the data needs to refresh every 10 minutes.
Thank you.

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6265
    • View Profile
Re: Json data
« Reply #1 on: March 03, 2014, 12:23:32 pm »
try this:

var data = JSON.parse(  '<?php echo $datajs ?>' ) ;

newparam

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Json data
« Reply #2 on: March 03, 2014, 12:39:35 pm »
its still not working..
the moment i replace
var data = [
        { rank: 1, company: 'Exxon Mobil', revenues: '339,938.0', profits: '36,130.0' },
        { rank: 2, company: 'Wal-Mart Stores', revenues: '315,654.0', profits: '11,231.0' },
        { rank: 3, company: 'Royal Dutch Shell', revenues: '306,731.0', profits: '25,311.0' },
        { rank: 4, company: 'BP', revenues: '267,600.0', profits: '22,341.0' },
        { rank: 5, company: 'General Motors', revenues: '192,604.0', profits: '-10,567.0' },
        { rank: 6, company: 'Chevron', revenues: '189,481.0', profits: '14,099.0' },
        { rank: 7, company: 'DaimlerChrysler', revenues: '186,106.3', profits: '3,536.3' },
        { rank: 8, company: 'Toyota Motor', revenues: '185,805.0', profits: '12,119.6' },
        { rank: 9, company: 'Ford Motor', revenues: '177,210.0', profits: '2,024.0' },
        { rank: 10, company: 'ConocoPhillips', revenues: '166,683.0', profits: '13,529.0' },
        { rank: 11, company: 'General Electric', revenues: '157,153.0', profits: '16,353.0' },
        { rank: 12, company: 'Total', revenues: '152,360.7', profits: '15,250.0' },
        { rank: 13, company: 'ING Group', revenues: '138,235.3', profits: '8,958.9' },
        { rank: 14, company: 'Citigroup', revenues: '131,045.0', profits: '24,589.0' },
        { rank: 15, company: 'AXA', revenues: '129,839.2', profits: '5,186.5' },
        { rank: 16, company: 'Allianz', revenues: '121,406.0', profits: '5,442.4' },
        { rank: 17, company: 'Volkswagen', revenues: '118,376.6', profits: '1,391.7' },
        { rank: 18, company: 'Fortis', revenues: '112,351.4', profits: '4,896.3' },
        { rank: 19, company: 'Cr

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6265
    • View Profile
Re: Json data
« Reply #3 on: March 03, 2014, 12:50:17 pm »
your data looks truncated.

newparam

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Json data
« Reply #4 on: March 03, 2014, 01:01:43 pm »
 revenues: '110,764.6', profits: '7,434.3' },
        { rank: 20, company: 'American Intl. Group', revenues: '108,905.0', profits: '10,477.0' }
        ];
with
var data = JSON.parse(  '<?php echo $datajs ?>' ) ;


the grid disappears and it displays nothing not even the headings.

Is there any other way of accessing from the database ?
*also i dont know why it is getting truncated. I have posted the remaining part.
« Last Edit: March 03, 2014, 01:04:29 pm by newparam »

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6265
    • View Profile
Re: Json data
« Reply #5 on: March 03, 2014, 01:24:38 pm »
looks like  '<?php echo $datajs ?>' is not returning a valid JSON data. Did you check browser console for errors.

if you are using unicode characters, you should use utf-8 in mysql

http://stackoverflow.com/questions/202205/how-to-make-mysql-handle-utf-8-properly


newparam

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Json data
« Reply #6 on: March 03, 2014, 02:11:32 pm »
I did check if mySQL is in UTF-8. It is in UTF-8.
The php file is displaying JSON. i have attached the screenshoot.
And the console is showing no errors for this.

Also the html file is showing an error saying
Uncaught SyntaxError: Unexpected token < >
for this line: var data = JSON.parse(  '<?php echo $datajs ?>' ) ;


I am placing the php code in the head tag. is that right?

newparam

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Json data
« Reply #7 on: March 03, 2014, 04:35:30 pm »
I modified my code a bit. It is displaying data but one character in a row.
   <script >

      $(document).ready(function() {

         
      var data = '<?php echo $datajs ?>';

         obj = { width: 1000, height: 250, title: "Service ",resizable:true,draggable:false, editable:false};
         obj.colModel = [{ title: "Hostname", width: 200, dataType: "string" },
         { title: "SSH", width:200, dataType: "string" },
         { title: "HTTP", width:200, dataType: "string", align: "right" },
         { title: "user", width: 150, dataType: "integer", align: "right"},
         { title: "Procs", width: 150, dataType: "integer", align: "right"}];
         
         
         obj.dataModel = { data: data };
         $("#tabs-2").pqGrid(obj);

         
      });

      </script>

the php code is the same.
on echo-ing datajs in the php code, it is returning json code.
But on echoing it in script its printing each character as a separate row.


paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6265
    • View Profile
Re: Json data
« Reply #8 on: March 03, 2014, 06:00:10 pm »
When you use JSON data, you have to mention dataIndx in your colModel.

http://paramquery.com/demos/grid_json

newparam

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Json data
« Reply #9 on: March 04, 2014, 09:58:37 am »
when i mention dataIndx I get a blank grid. without the characters.
But the count of the no of rows is same as when it was displaying individual characters.
 

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6265
    • View Profile
Re: Json data
« Reply #10 on: March 04, 2014, 11:33:55 am »
Not sure what's going on. Could you please post entire code of your code or create a fiddle.

Also post a dump of your remote JSON data

Code: [Select]
console.log(data);

newparam

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Json data
« Reply #11 on: March 04, 2014, 11:48:11 am »
solved it.
used this in script and added dataIndx to colmodel
   <?php echo 'var data ='.$datajs; ?>
it works now.
Thanks for the help.