Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

Ajax Programming

An AJAX Simple Example for PHP


An AJAX Simple Example for PHP
Article from http://www.joyistar.com
  Introduction:
 AJAX WebShop 3 Beta2 supports PHP programming by integrating PHP5
development environment. Here we will give an simple example to show
you how to develop web applications by PHP in AJAX WebShop.

1, Notice
We will use Access database in this example. Of course, you can use
others you like. Database name: demo.mdb; Table name: product; Fields:
PRODUCT_ID, PRODUCT_NAME, PRODUCT_PRICE and PRODUCT_AREA.

2, Example
Start AJAX WebShop 3 to new a project by "File->New Project"

Give a project name in the popup window and select the web server as
PHP.

New a PHP Service by "File->New .PHP Service" and input the
required content. Select the "ServiceType" as "Query Data".
Click "OK", WebShop will generate codes automatically.

Input the codes below(Notice: demo.mdb should be the same directory
with the source code files.):
$connstr = "DRIVER=Microsoft Access Driver
(*.mdb);DBQ=".realpath($_SERVER['PATH_TRANSLATED'])."\demo.mdb";
$sql     = "select * from product";
$sqlcount     =  "select count(*) from product";

The source code of testquery.php:
1.      Fill data to $xmlRequest in columns:
                //fill metadata fields
        for ($i=1; $i<=$fields_count; $i++) {
            $fieldname = odbc_field_name($result_id, $i);
            $datatype  = odbc_field_type($result_id, $i);
            $xmlRequest->addField($fieldname, $datatype);
        }

2. Fill data to $xmlRequest in rows:
        //fill data rows
        for ($i=0; $i<$record_count; $i++) {
                        odbc_fetch_row($result_id);
                        if($i>=$recNo && $i<$recNo+$maxRows) {
                                $xmlRequest->append();
                        for ($j=1; $j<=$fields_count; $j++) {
                                        $xmlRequest->setValueByIndex($j-1, odbc_result($result_id, $j));
                                }
                        }
                        if($i>=$recNo+$maxRows) break;
                }
3.    Get the paged parameters, $xmlRequest->recNo is the first record,
$xmlRequest->maxRows is the total records number in a page and to get
the total records number by executing "select count(*) from
product":
                $sqlcount     =  "select count(*) from product";
                $result_id    = @odbc_do($connid, $sqlcount);
        if($result_id==null)
           throw new Exception($sqlcount);
                odbc_fetch_row($result_id);
                $record_count =  odbc_result($result_id,1);
                $xmlRequest->setRecordCount($record_count);
                $recNo        = $xmlRequest->recNo;
                $maxRows      = $xmlRequest->maxRows;
                if($maxRows==-1) $maxRows = $record_count;
After the steps above, we need a web page to display the result. Select
"File-> New Page" and fill the file name.

Drag these components to the page from "Data Access" and
"DBControl". These components include "DataSet",
"DBNavigator", "DBEdit" and "DbGrid". Select the component
to edit it's properties.

DataSet1: Set "OpenURL" as "demo/simple_query.php" and Start
WebServer by "Run->Start WebServer". The Apache HttpServer will
start. Add fields in "Fields" by click the "..." to pop up the
Field window and right click to "Refresh" to get the fields name
and select all, click "Ok". Set the Active as "True".

Set the "DataSet" as "DataSet1" in other components to finish
the data binding with database. Be attention that you need to set the
DataField of DBEdit.

Click "Run" to view the result.

Appendix: Source codes:
<?php
/**
 * Title:
 * Description:
 * Copyright: Copyright (c) 2006
 *
 * $Author:
 * $Date:
 * $Revision:
 */
        header("Content-Type:text/xml");
        require_once('../joyistar/XmlRequest.php');
        $postdata   = file_get_contents("php://input");
        $xmlRequest = new XmlRequest();
        $xmlRequest->open($postdata);
        try {
                $connstr = "DRIVER=Microsoft Access Driver
(*.mdb);DBQ=".realpath($_SERVER['PATH_TRANSLATED'])."\demo.mdb";
                $sql     = "select * from product";
                $connid  = @odbc_connect($connstr,"","",SQL_CUR_USE_ODBC ) or die
("db connect error!");

                $sqlcount     =  "select count(*) from product";
                $result_id    = @odbc_do($connid, $sqlcount);
        if($result_id==null)
           throw new Exception($sqlcount);
                odbc_fetch_row($result_id);
                $record_count =  odbc_result($result_id,1);
                $xmlRequest->setRecordCount($record_count);
                $recNo        = $xmlRequest->recNo;
                $maxRows      = $xmlRequest->maxRows;
                if($maxRows==-1) $maxRows = $record_count;

                $result_id   = @odbc_do($connid, $sql);
        if($result_id==null)
           throw new Exception($sql);
                $fields_count = odbc_num_fields($result_id);
                //fill metadata fields
        for ($i=1; $i<=$fields_count; $i++) {
            $fieldname = odbc_field_name($result_id, $i);
            $datatype  = odbc_field_type($result_id, $i);
            $xmlRequest->addField($fieldname, $datatype);
        }
        //fill data rows
        for ($i=0; $i<$record_count; $i++) {
                        odbc_fetch_row($result_id);
                        if($i>=$recNo && $i<$recNo+$maxRows) {
                                $xmlRequest->append();
                        for ($j=1; $j<=$fields_count; $j++) {
                                        $xmlRequest->setValueByIndex($j-1, odbc_result($result_id, $j));
                                }
                        }
                        if($i>=$recNo+$maxRows) break;
                }
                odbc_close($connid);

                echo $xmlRequest->getXml();
        }
        catch (Exception $e) {
                $xmlRequest->setError($e->getTraceAsString());
                echo $xmlRequest->getXml();
                throw $e;
        }
?>

An AJAX Simple Example for PHP
Article from http://www.joyistar.com
  Introduction:
 AJAX WebShop 3 Beta2 supports PHP programming by integrating PHP5
development environment. Here we will give an simple example to show
you how to develop web applications by PHP in AJAX WebShop.

1, Notice
We will use Access database in this example. Of course, you can use
others you like. Database name: demo.mdb; Table name: product; Fields:
PRODUCT_ID, PRODUCT_NAME, PRODUCT_PRICE and PRODUCT_AREA.

2, Example
Start AJAX WebShop 3 to new a project by "File->New Project"

Give a project name in the popup window and select the web server as
PHP.

New a PHP Service by "File->New .PHP Service" and input the
required content. Select the "ServiceType" as "Query Data".
Click "OK", WebShop will generate codes automatically.

Input the codes below(Notice: demo.mdb should be the same directory
with the source code files.):
$connstr = "DRIVER=Microsoft Access Driver
(*.mdb);DBQ=".realpath($_SERVER['PATH_TRANSLATED'])."\demo.mdb";
$sql     = "select * from product";
$sqlcount     =  "select count(*) from product";

The source code of testquery.php:
1.      Fill data to $xmlRequest in columns:
                //fill metadata fields
        for ($i=1; $i<=$fields_count; $i++) {
            $fieldname = odbc_field_name($result_id, $i);
            $datatype  = odbc_field_type($result_id, $i);
            $xmlRequest->addField($fieldname, $datatype);
        }

2. Fill data to $xmlRequest in rows:
        //fill data rows
        for ($i=0; $i<$record_count; $i++) {
                        odbc_fetch_row($result_id);
                        if($i>=$recNo && $i<$recNo+$maxRows) {
                                $xmlRequest->append();
                        for ($j=1; $j<=$fields_count; $j++) {
                                        $xmlRequest->setValueByIndex($j-1, odbc_result($result_id, $j));
                                }
                        }
                        if($i>=$recNo+$maxRows) break;
                }
3.    Get the paged parameters, $xmlRequest->recNo is the first record,
$xmlRequest->maxRows is the total records number in a page and to get
the total records number by executing "select count(*) from
product":
                $sqlcount     =  "select count(*) from product";
                $result_id    = @odbc_do($connid, $sqlcount);
        if($result_id==null)
           throw new Exception($sqlcount);
                odbc_fetch_row($result_id);
                $record_count =  odbc_result($result_id,1);
                $xmlRequest->setRecordCount($record_count);
                $recNo        = $xmlRequest->recNo;
                $maxRows      = $xmlRequest->maxRows;
                if($maxRows==-1) $maxRows = $record_count;
After the steps above, we need a web page to display the result. Select
"File-> New Page" and fill the file name.

Drag these components to the page from "Data Access" and
"DBControl". These components include "DataSet",
"DBNavigator", "DBEdit" and "DbGrid". Select the component
to edit it's properties.

DataSet1: Set "OpenURL" as "demo/simple_query.php" and Start
WebServer by "Run->Start WebServer". The Apache HttpServer will
start. Add fields in "Fields" by click the "..." to pop up the
Field window and right click to "Refresh" to get the fields name
and select all, click "Ok". Set the Active as "True".

Set the "DataSet" as "DataSet1" in other components to finish
the data binding with database. Be attention that you need to set the
DataField of DBEdit.

Click "Run" to view the result.

Appendix: Source codes:
<?php
/**
 * Title:
 * Description:
 * Copyright: Copyright (c) 2006
 *
 * $Author:
 * $Date:
 * $Revision:
 */
        header("Content-Type:text/xml");
        require_once('../joyistar/XmlRequest.php');
        $postdata   = file_get_contents("php://input");
        $xmlRequest = new XmlRequest();
        $xmlRequest->open($postdata);
        try {
                $connstr = "DRIVER=Microsoft Access Driver
(*.mdb);DBQ=".realpath($_SERVER['PATH_TRANSLATED'])."\demo.mdb";
                $sql     = "select * from product";
                $connid  = @odbc_connect($connstr,"","",SQL_CUR_USE_ODBC ) or die
("db connect error!");

                $sqlcount     =  "select count(*) from product";
                $result_id    = @odbc_do($connid, $sqlcount);
        if($result_id==null)
           throw new Exception($sqlcount);
                odbc_fetch_row($result_id);
                $record_count =  odbc_result($result_id,1);
                $xmlRequest->setRecordCount($record_count);
                $recNo        = $xmlRequest->recNo;
                $maxRows      = $xmlRequest->maxRows;
                if($maxRows==-1) $maxRows = $record_count;

                $result_id   = @odbc_do($connid, $sql);
        if($result_id==null)
           throw new Exception($sql);
                $fields_count = odbc_num_fields($result_id);
                //fill metadata fields
        for ($i=1; $i<=$fields_count; $i++) {
            $fieldname = odbc_field_name($result_id, $i);
            $datatype  = odbc_field_type($result_id, $i);
            $xmlRequest->addField($fieldname, $datatype);
        }
        //fill data rows
        for ($i=0; $i<$record_count; $i++) {
                        odbc_fetch_row($result_id);
                        if($i>=$recNo && $i<$recNo+$maxRows) {
                                $xmlRequest->append();
                        for ($j=1; $j<=$fields_count; $j++) {
                                        $xmlRequest->setValueByIndex($j-1, odbc_result($result_id, $j));
                                }
                        }
                        if($i>=$recNo+$maxRows) break;
                }
                odbc_close($connid);

                echo $xmlRequest->getXml();
        }
        catch (Exception $e) {
                $xmlRequest->setError($e->getTraceAsString());
                echo $xmlRequest->getXml();
                throw $e;
        }
?>

Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc