USING A STANDARD WEB SERVER FOR DATA ACCESS WITH HMG AND JMG APPS (PART II/V)

THE FUN BEGINS  🙂

In the previous post, we added records… now we will retrieve it.

For simplicity, we will retrieve all the records from our test table.

THE SERVER PROCEDURE TO GET DATA

It will be called ‘select.php’ and will be located at the ‘php’ subfolder on our server.

For simplicity, we will retrieve the all the rows on our test table, so, no parameters are required.

 <?php
 
 $db = mysqli_connect( '127.0.0.1', 'root', '');

 if( ! $db ) :
 die( mysql_error() ) ;
 endif;

 if( ! mysqli_select_db($db, 'test')):
 die( mysql_error() );
 endif;
 
 $query = mysqli_query( $db, "select * from people" );

 if( !$query ) :
 die("error SQL");
 endif;
 
 $table = array();

 while( $row = mysqli_fetch_array( $query, MYSQLI_NUM ) ):
 $table[] = $row;
 endwhile;

 $string = json_encode( $table );
 
 mysqli_free_result( $query );
 mysqli_close($db);

 echo $string;

?>

RETRIEVING RECORDS FROM OUR HMG CLIENT APP. This will be easier than adding records, since the server procedure will not require parameters. Since our server procedure send the data to us in JSON format, we will convert it, to an array to fill a Grid.

Function main_get_data_action

// Lets assume the server are local
// and the server procedures are in the 'php' subfolder.

cServer := '127.0.0.1'
cGet := cServer + '/php/select.php'

// Call remote server procedure (no parameters required)

oHttp := TIpClientHttp():new( "http://127.0.0.1/php/select.php" )

// Check for error
 
IF .NOT. oHttp:open()
    MsgStop ( "Connection not available!" )
    oHttp:close()
    Return
EndIf

// Get returned data

cData := oHttp:readAll()

// Create an array to store our recordset

aRecordSet := {}

// Decode our JSON string retrieved from server and store
// in the array

hb_jsondecode( cData , @aRecordSet )

oHttp:close()

// Fill a grid with our recordset

Main.Grid_1.DeleteAllItems

FOR I := 1 TO LEN( aRecordSet )
    Main.Grid_1.AddItem ( { aRecordSet[I][1] , aRecordSet[I][2] , aRecordSet[I][3] } )
NEXT I

MSGINFO('Done! ( ' + ALLTRIM(STR(LEN( aRecordSet ))) + ' records returned).' )

Return Nil


RETRIEVING RECORDS FROM OUR JMG CLIENT APP

We fill a table widget with our server data.

Table widget ‘load’ method will automatically decode JSON server data, making the things easier.

function ajaxRead()
{ 

// First, we call the select.php server procedure 
// to get our recordset. 
// The process is ASYNCHRONOUS, meaning that our 
// client program still runing
// WITHOUT WAIT for request completion.

// For this example, the server will be in 
// the local machine:
// The server procedure is in the 'php' sub-folder

var server = '127.0.0.1';
var request = $.ajax( { url: "http://" + server + "/php/select.php" } );

// Then we define the required callback functions 
// for the events 'done' and 'fail'.
// One of this will be automatically executed, 
// when our request be successfully finished or when fail.

request.done(function( jsonData )
{
    doMethod('table2','load',jsonData);
});

request.fail(function(jqXHR, textStatus)
{
    alert( "Request failed: " + textStatus );
});

}

TO BE CONTINUED… (delete, update and THE FULL WORKING CODE!)

You can discuss about this post on the HMG SourceForge forum Here

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

3 Responses to USING A STANDARD WEB SERVER FOR DATA ACCESS WITH HMG AND JMG APPS (PART II/V)

  1. Serge says:

    Hi Roberto,

    I changed ‘127.0.0.1’ to ‘localhost’ in order to get it working (in function ajaxRead() )
    This works for me:

    var request = $.ajax( { url: “http://localhost/JMG/select.php” } );

    In HMG-prg both work fine :
    oHttp := TIpClientHttp():new( “http://localhost/JMG/select.php” )
    oHttp := TIpClientHttp():new( “http://127.0.0.1/JMG/select.php” )
    Greetings, Serge

    Like

  2. hmgthinking says:

    The JMG code is the AJAX demo. On it (I’ve re-tested now) it works with 127.0.0.1 on my machine… strange…

    Like

  3. esgici says:

    Thanks Roberto 😀

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s