Welcome Guest, Not a member yet? Register   Sign In
Xajax 0.5.3 in Code Igniter 1.5.4
#1

[eluser]Leonardo Radoiu[/eluser]
Hello!

For everyone out there who may need some help implementing Xajax 0.5.3 in the latest version of Code Igniter here is an example. First of all the xajax library file that will be loaded:

Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Xajax_lib {
function Xajax_lib() {
    require_once(BASEPATH.'/libraries/xajax_core/xajax.inc'.EXT);
}
}
?>

You need to place folder "xajax_core" from the Xajax 0.5.3 kit in the system/libraries folder of the Code Igniter framework.
Now, let's say we have a list of items and we want to fill a select box with this list, dinamically, using Xajax. We create a model named Item with a method that selects the items using, let's assume again, a webservice:

Code:
<?php
class Item extends Model {

var $_id = null;
var $_start = null;
var $_limit = null;
var $_sortField = null;
var $_sortOrder = null;

function __construct() {
    parent::Model();
        
    $this->load->library('Xajax_lib');
        $this->load->library('Nusoap_lib'); // see http://ellislab.com/forums/viewthread/59710/
}
    
function select_items($params) {
    $items = array();
        
    $objResponse = new xajaxResponse();
            
    $params['start'] = isset($params['start'])?$params['start']:0;
    $params['limit'] = isset($params['limit'])?$params['limit']:10;
    $params['sortField'] = isset($params['sortField'])?$params['sortField']:"title";
    $params['sortOrder'] = isset($params['sortOrder'])?$params['sortOrder']:"ASC";

    //we assume we have a web service located here:

    $this->soapclient = new soapclient('http://localhost/webservice.php/ItemWSVC/select_items/wsdl');
        
    if(!$this->soapclient->fault) {
        if(!$this->soapclient->getError()) {
            $templates = $this->soapclient->call (
            'selectItems',
            array($params['start'], $params['limit'], $params['sortField'], $params['sortOrder']),
            "urn:ItemWSDL",
            "urn:ItemWSDL#selectItems"
            );
        }
    }

    $script = "var items = [];\r\n";

    foreach($items as $key=>$value) {
        $script .= "items.push({id: ".$value['id'].", title: '".$value['title']."'});\r\n";    
    }

    $script .= "Item_SelectItems(items);\r\n"; // the js function we execute at the end
    $objResponse->script($script);
    return $objResponse;
}
}
?>

Please observe we don't register this function here. We'll do that later, below.
The js code for function Item_SelectItems (you need to load this function in the head section of the view in the xajax.js file):

Code:
/* I use jquery.selectboxes.js plugin to fill the select box */
function Item_SelectItems(items) {
    for (i = 0; i < items.length; i++) {
        $('#itemId').addOption(items[i].id, items[i].title);
    }
    $('#itemId').selectOptions("0");
}

Now, we need a controller named mypage.php that registers the xajax function:

Code:
class MyPage extends Controller {
function __construct() {
    parent::Controller();

    $this->load->model('Item');
    $this->load->library('Xajax_lib');
}

function index() {
        $this->xajax = new xajax();

        $this->xajax->registerFunction(array('select_items', &$this->Item, 'select_items'));
        $this->xajax->processRequest();

        $this->view->load('mypage');
}

Finally, the view named mypage.php that holds the form field and calls the js-fied xajax function.

Code:
&lt;html&gt;
&lt;head&gt;
&lt;script src="&lt;?php echo $this->config->item('base_url'); ?&gt;/js/jquery.js"&gt;&lt;/script&gt;
&lt;script src="&lt;?php echo $this->config->item('base_url'); ?&gt;/js/jquery.selectboxes.js"&gt;&lt;/script&gt;
&lt;script src="&lt;?php echo $this->config->item('base_url'); ?&gt;/js/xajax.js"&gt;&lt;/script&gt;
&lt;?php isset($this->xajax)?$this->xajax->printJavascript($this->config->item('base_url').'/js'):""; ?&gt;
&lt;/head&gt;

&lt;body&gt;

<select id="itemId" name="itemId">
    <option value="0">all items</option>
</select>

&lt;script type="text/javascript"&gt;
//&lt;![CDATA[
    var params = new Object();
    params.start = 0;
    params.limit = 10;
    params.sortField = "title";
    params.sortOrder = "ASC";
    xajax_select_items(params);
//]]>
&lt;/script&gt;

&lt;/body&gt;
&lt;/html&gt;

In the end I attach the code for the ItemWSVC web service:

Code:
class ItemWSVC extends Controller {
function __construct() {
    parent::Controller();

    $this->load->library("Nusoap_lib");

    $this->nusoap_server = new soap_server();
    $this->nusoap_server->configureWSDL("ItemWSDL", "urn:ItemWSDL");
    
    $this->nusoap_server->wsdl->addComplexType(
        "ItemsRecordset",
        "complexType",
        "array",
        "",
        "SOAP-ENC:Array",
        array(
            "id"=>array("name"=>"id", "type"=>"xsd:int"),
            "title"=>array("name"=>"title", "type"=>"xsd:string"),
            "description"=>array("name"=>"description", "type"=>"xsd:string")
        )
    );

    $this->nusoap_server->register(
        "selectItems",
        array("start" => "xsd:int", "limit" => "xsd:int", "sortField" => "xsd:string", "sortOrder" => "xsd:string"),
        array("return"=>"tns:ItemsRecordset"),
        "urn:ItemsWSDL",
        "urn:ItemsWSDL#selectItems",
        "rpc",
        "encoded",
        "Retrieves items recordset"
    );
}

function index() {
   if($this->uri->rsegment(3) == "wsdl") {
    $_SERVER['QUERY_STRING'] = "wsdl";
   } else {
    $_SERVER['QUERY_STRING'] = "";
   }

   $this->nusoap_server->service(file_get_contents("php://input"));
}
    
function select_items() {
   function selectItems($start, $limit, $sortField, $sortOrder) {
       // your code
   }
}
}
#2

[eluser]Avatar[/eluser]
I get an error where I try to do this tutorial.

Output has already been sent to the browser at /home2/user/public_html/system/libraries/xajax_lib.php:8. Please make sure the command $xajax->processRequest() is placed before this.
#3

[eluser]Leonardo Radoiu[/eluser]
Hi there!

Make sure that your included files don't have a white space outside the php code because that will make your header output before executing $xajax->processRequest():

Example:

Code:
verify that there is no white space in any included file right here -> &lt;? php

//some code

?&gt; <- verify that there is no white space in any included file right here

Moreover, even there is no white space at the start or the end of the files make sure that your editor is one in which you may set the file format to Unix file format (like Dreamweaver, Eclipse or Editplus) and not Notepad or Wordpad, applications that formats your file in PC format by default. If you edit your files with Notepad you will have to re-create them in an editor that knows about Unix file format.
#4

[eluser]Avatar[/eluser]
Smile This has got to be the second time. Smile thanks
#5

[eluser]Leonardo Radoiu[/eluser]
Well... glad to know I was helpful. You seem to be interested in cool-things technology. I like that. I believe that one can do some pretty good stuff with Xajax and Nusoap if he knows how. Good luck!
#6

[eluser]Avatar[/eluser]
Yes, this is very helpful. I was just starting to do webservices. I still quite don't understand exactly how the data is being passed around here. Not the webservice, but when xajax and jquery come into play. I've created a member model with some data and it still doesn't return my db values to fill the selectbox. Can you post an example on how I would use data from the websevice to fill a div or something with xajax and when the div gets click the data is refreshed? I hope this is not asking for too much. I've been using CI for roughly 1 year now and just recently joined this forum. The main thing that doesn't make sense in your example is how the params work. Thank you in advance, this is some neat stuff. Smile
#7

[eluser]Leonardo Radoiu[/eluser]
Hello!

I did a full example from scratch in this new topic: http://ellislab.com/forums/viewthread/60879/

The scripts are fully functional, I just test them on my computer. There may be some limitations in the forum editor, I believe some data were truncated on post but if you have problems I made an archive in this location: http://leonardo.kreativ.ro/sources/ci/xa...nusoap.rar

I also made a GIF schema on how you may use the model with/without XAJAX or with/without NUSOAP. I believe it's self-explanatory.

http://leonardo.kreativ.ro/wp-content/up..._xajax.gif
#8

[eluser]Avatar[/eluser]
NUSOAP/XAJAX is awesome technology, Thanks for posting your knowledge on it and how easy it is to integrate it into CI.
#9

[eluser]a&w[/eluser]
Thanks for posting this. These type of posts are very helpful. I've been hoping someone would post something similar for extjs.
#10

[eluser]djreed[/eluser]
Thanks for this! Can you clarify something for me? Where do you place this code. What does it do?:
Code:
&lt;?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Xajax_lib {
function Xajax_lib() {
    require_once(BASEPATH.'/libraries/xajax_core/xajax.inc'.EXT);
}
}
?&gt;

Thanks!

-Danny




Theme © iAndrew 2016 - Forum software by © MyBB