Part of the EllisLab Network
   
 
Xajax 0.5.3 in Code Igniter 1.5.4
Posted: 30 August 2007 05:50 PM   [ Ignore ]  
Summer Student
Total Posts:  18
Joined  08-29-2007

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:

<?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:

<?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://codeigniter.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):

/* 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:

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.

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

<
body>

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

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

</body>
</
html>

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

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
   
}
}
}
Profile
 
 
Posted: 14 September 2007 01:49 PM   [ Ignore ]   [ # 1 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  209
Joined  09-07-2007

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.

 Signature 

smarty-hmvc-enviroment-variables
http://starnixalpha.blogspot.com/2008/11/templated-modulated-cms.html
My Blog On Blogger

Profile
 
 
Posted: 14 September 2007 02:09 PM   [ Ignore ]   [ # 2 ]  
Summer Student
Total Posts:  18
Joined  08-29-2007

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:

verify that there is no white space in any included file right here -> <? php

//some code

?> <- 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.

Profile
 
 
Posted: 14 September 2007 02:17 PM   [ Ignore ]   [ # 3 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  209
Joined  09-07-2007

smile This has got to be the second time. smile thanks

 Signature 

smarty-hmvc-enviroment-variables
http://starnixalpha.blogspot.com/2008/11/templated-modulated-cms.html
My Blog On Blogger

Profile
 
 
Posted: 14 September 2007 05:03 PM   [ Ignore ]   [ # 4 ]  
Summer Student
Total Posts:  18
Joined  08-29-2007

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!

Profile
 
 
Posted: 14 September 2007 05:13 PM   [ Ignore ]   [ # 5 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  209
Joined  09-07-2007

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

 Signature 

smarty-hmvc-enviroment-variables
http://starnixalpha.blogspot.com/2008/11/templated-modulated-cms.html
My Blog On Blogger

Profile
 
 
Posted: 15 September 2007 12:10 PM   [ Ignore ]   [ # 6 ]  
Summer Student
Total Posts:  18
Joined  08-29-2007

Hello!

I did a full example from scratch in this new topic: http://codeigniter.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/xajax_nusoap/ci_xajax_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/uploads/2007/09/ci_nusoap_xajax.gif

Profile
 
 
Posted: 16 September 2007 12:32 AM   [ Ignore ]   [ # 7 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  209
Joined  09-07-2007

NUSOAP/XAJAX is awesome technology, Thanks for posting your knowledge on it and how easy it is to integrate it into CI.

 Signature 

smarty-hmvc-enviroment-variables
http://starnixalpha.blogspot.com/2008/11/templated-modulated-cms.html
My Blog On Blogger

Profile
 
 
Posted: 16 September 2007 02:51 PM   [ Ignore ]   [ # 8 ]  
Lab Assistant
RankRank
Total Posts:  233
Joined  04-03-2007

Thanks for posting this.  These type of posts are very helpful.  I’ve been hoping someone would post something similar for extjs.

Profile
 
 
Posted: 13 May 2009 03:01 PM   [ Ignore ]   [ # 9 ]  
Summer Student
Total Posts:  18
Joined  05-06-2009

Thanks for this! Can you clarify something for me? Where do you place this code. What does it do?:

<?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);
}
}
?>

Thanks!

-Danny

Profile
 
 
Posted: 13 May 2009 03:20 PM   [ Ignore ]   [ # 10 ]  
Summer Student
Total Posts:  18
Joined  08-29-2007

Hello!

The code you are asking about is placed in a file called Xajax_lib.php (or whatever you like, as long as you modify the class name and the constructor) an it includes the main file of the xajax library in a CodeIgniter style, in order to be placed under the system/libraries/Xajax_lib.php (if you look inside system/libraries you may probably see other files named in the same manner).

As you know the Xajax kit includes numerous files inside. If you want to mantain a clean system/libraries folder (as I do) you would like to keep the third-party libraries contained in their own folders, for example: system/libraries/xajax_core/* or system/libraries/xajax-0.5.3/* and so on.

My choice was to put the xajax files in system/libraries/xajax_core/ as you see. The advantage of the Xajax_lib.php is that you can move from an old version to a new one simply by changing the ‘require_once’ line in this file (and eventually add any other lines that makes your new xajax version compatible with your project) and thus you have more control over it.

Profile
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 819, on March 11, 2010 11:15 AM
Total Registered Members: 120453 Total Logged-in Users: 41
Total Topics: 126533 Total Anonymous Users: 5
Total Replies: 665341 Total Guests: 317
Total Posts: 791874    
Members ( View Memberlist )