[ 9 posts ]

Gennady

  • Username: gvayl
  • Joined: Sat Apr 23, 2011 11:08 am
  • Posts: 5
  • Offline
  • Profile

Newbe...datatable and JSON

Post Posted: Sat Apr 23, 2011 11:15 am
+0-
I'm really new to all of this...so I apologize for stupid questions in advance.

What I'm able to do:
I have a mysql database and a php file that has a function that generates a results.json file. Anotherwords I can query the database and output results.

I don't know how to send the results to a datatable in yui 3. I tried looking at the datasource help and the base datatable examples but so far have not had any luck in understanding this. I'm really new to this so if someone could provide some guidance I'd really appreciate it.

Thanks!

Gennady

  • Username: gvayl
  • Joined: Sat Apr 23, 2011 11:08 am
  • Posts: 5
  • Offline
  • Profile

Re: Newbe...datatable and JSON

Post Posted: Mon Apr 25, 2011 3:34 am
+0-
I have now gone a bit farther. I'm using:
var myDataSource = new Y.DataSource.IO({source:"myScript.php"}),
myCallback = {
success: function(e){
alert(e.response);
},
failure: function(e){
alert("Could not retrieve data: " + e.error.message);
}
};

I'm getting a failure however. In order for DataSource.IO to get data from the php file I just have to echo that data right? So the PHP file will get a query from mysql and then echo it.

Todd Smith

YUI Contributor

  • Username: stlsmiths
  • Joined: Thu Nov 05, 2009 10:03 am
  • Posts: 675
  • GitHub: stlsmiths
  • Gists: stlsmiths
  • IRC: t_smith
  • Offline
  • Profile

Re: Newbe...datatable and JSON

Post Posted: Mon Apr 25, 2011 9:41 am
+0-
Hi Gennady,

Have you looked over the YUI 3 user guide, specifically the YQL example at http://yuiblog.com/sandbox/yui/3.3.0pr3/examples/datatable/datatable_dsget.html ?
This example uses a URL (which could be replaced with your php url) and a JSON schema response object (which could be replaced with your response results object and column names).

If you need more help, please ask

Todd

Todd Smith

YUI Contributor

  • Username: stlsmiths
  • Joined: Thu Nov 05, 2009 10:03 am
  • Posts: 675
  • GitHub: stlsmiths
  • Gists: stlsmiths
  • IRC: t_smith
  • Offline
  • Profile

Re: Newbe...datatable and JSON

Post Posted: Mon Apr 25, 2011 3:38 pm
+0-
Yes your php server DEFINITELY has to "echo" something. I do a lot of YUI 2 datatable work but haven't migrated my work over to YUI 3 yet. I haven't seen many postings on this forum regarding YUI 3 DT with remote calls other than YQL. I suggest you search the forum including keywords for 'YUI 3', 'DataTable', 'JSON' to see how others are doing it.

Having said all that, I just put together a quick demo of a YUI 3 DataTable and included the PHP source code for a back-end data server. A public link is available at http://blunderalong.com/yui/dtb/dt_json_io.html. This is pretty basic, but should show the fundamentals of using a DataSource.IO via JSON and what is required on the server-side.

Another good source for writing PHP back-ends for YUI 2.x (but same concept) is available on http://www.satyam.com.ar/yui/#phpjson.

Good luck,

Todd


Last edited by stlsmiths on Thu Mar 01, 2012 1:51 pm, edited 1 time in total.

Gennady

  • Username: gvayl
  • Joined: Sat Apr 23, 2011 11:08 am
  • Posts: 5
  • Offline
  • Profile

Re: Newbe...datatable and JSON

Post Posted: Sun May 01, 2011 9:52 am
+0-
Todd,
I get an error that the function db_lookup is undefined. Also when I do "require_once 'JSON.php';" it pastes the contents of JSON.php in the browser when I run it. Is this normal?

Thanks,

-Gennady

Gennady

  • Username: gvayl
  • Joined: Sat Apr 23, 2011 11:08 am
  • Posts: 5
  • Offline
  • Profile

Re: Newbe...datatable and JSON

Post Posted: Sun May 01, 2011 11:02 am
+0-
Ok...including the json.php file is fixed. Its no longer an issue.

My php file generates the following: Array{"replyCode":"k","replyText":"j","Results":{"pkService":"1","FirstName":"Jack","Middle":"S","LastName":"Jones"}}
I'm using firebug to read this.

I'm using different code though:
$query = "SELECT * FROM myTbl limit 20";
$result = mysql_query($query) or trigger_error(mysql_error().$query);

$row = mysql_fetch_assoc($result);
$returnValue = array(
'replyCode' => 'k',
'replyText' => 'j',
'Results' => $row
);
header("Content-Type: application/json");
header("Cache-Control: no-cache");
echo $returnValue;
echo json_encode($returnValue);

When I execute your example using the above code, I get an empty table...

Any ideas appreciated.

Thanks!

Todd Smith

YUI Contributor

  • Username: stlsmiths
  • Joined: Thu Nov 05, 2009 10:03 am
  • Posts: 675
  • GitHub: stlsmiths
  • Gists: stlsmiths
  • IRC: t_smith
  • Offline
  • Profile

Re: Newbe...datatable and JSON

Post Posted: Sun May 01, 2011 11:36 am
+0-
Hi Gennady,

I can tell by some of your questions that you may be a little confused about using PHP. The example link I provided earlier was meant to show my example implementation. You appear to have understood that "db_lookup" is a library function I wrote that simply returns an associative array from MySQL. Sorry, I should have been more clear about that.

From the server snippet you provided it looks like you may have an extraneous "echo $returnValue" statement which will mess things up. You only need to have the "echo json_encode($returnValue)" statement.

Also, from the example of the output you show it looks like you are only returning a single row with mysql_fetch_assoc, but the 'Results' object of the JSON that is returned is expected to be an array. So I recommend your server call be replaced with the following
Code:
while ($record = mysql_fetch_assoc ($result)){
    $row[] = $record;
}

where the $row[] assignment appends to the $row array.

I recommend again that you review some of Satyam's examples for writing YUI 2 DataTable with php servers, they are very helpful.
1. Essential reading http://www.satyam.com.ar/yui/
2. Also his YUILibrary articles http://yuiblog.com/blog/2008/10/15/datatable-260-part-one/
3. and http://yuiblog.com/blog/2008/10/27/datatable-260-part-two/

You haven't shown any of your YUI3 DataTable code so I can't really tell if there are issues there. Once you get your php server worked out if you have problems you may want to use a pastie service to post your code and we can review it.

Regards,
Todd

tom martin

  • Username: tomarty1
  • Joined: Sun May 01, 2011 8:13 pm
  • Posts: 1
  • Offline
  • Profile

Re: Newbe...datatable and JSON

Post Posted: Sun May 01, 2011 8:24 pm
+0-
Thanks a bunch, really helped me out!

Gennady

  • Username: gvayl
  • Joined: Sat Apr 23, 2011 11:08 am
  • Posts: 5
  • Offline
  • Profile

Re: Newbe...datatable and JSON

Post Posted: Sat May 07, 2011 6:25 pm
+0-
It worked Todd. My table now has data from the database! Thank you VERY much!
  [ 9 posts ]
Display posts from previous:  Sort by  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum