Dynamic data in the PIM demo using PHP and MySQL

After reading up some questions on the forum about howto do dynamic data inside an Backbase enabled application I decided to write some post about this, explaining step by step how it's done.

Introduction

In principle the Backbase code doesn't know anything about dynamic data (e.g. data generated on the server). So it doesnt matter if you use PHP, JSP, ASP, Perl, Ruby, etc... The only thing Backbase does understand is XML (and JSON, but that's a other story). So it's important that your backend script deliver valid XML to the client, containing all the namespaces / prefixes / closing tags required to make an XML-document.

Modify the PIM, change from static to dynamic

In your PIM directoy you can find the index.xml file which contains the following includes to static xml files.

<!--
The data for the data source is included here.
-->

<xi:include href="data/contactsActors.xml" />
<xi:include href="data/contactsDirectors.xml" />
<xi:include href="data/contactsProducers.xml" />

We need to simple change these url's to dynamic data like:

<!--
The data for the data source is included here.
-->

<xi:include href="data/contacts.php?section=actors" />
<xi:include href="data/contacts.php?section=directors" />
<xi:include href="data/contacts.php?section=producers" />

Creating the contacts.php

So now we need to create the contacts.php file which is loaded by the PIM, otherwise we get errors that the file cannot be found. This contacts.php should output in the same data format as the static files, otherwise it will not work directly. So lets start with setting up the global structure:

<?php
//Define the variable just like static
echo '<e:variable name="varData_'.$_GET['section'].'" xmlns:e="http://www.backbase.com/2006/xel">';
echo '<e:data type="text/xml">';
echo '<contacts>';

// DATA HERE!

echo '</contacts>';
//Close the variable
echo '</e:data>';
echo '</e:variable>'; ?>

This already works but is not show any data because their is none, so lets add the dynamic database section

Getting dynamic data inside contacts.php

This is what you also normally should do, just create a connection to a database, query some table and loop over the resultset, an example can be:

<?php
//Define the variable just like static
echo '<e:variable name="varData_'.$_GET['section'].'" xmlns:e="http://www.backbase.com/2006/xel">';
echo '<e:data type="text/xml">';
echo '<contacts>';

if( $db = mysql_connect( 'localhost', 'user', 'password') ){
        if( mysql_select_db('database_name', $db) ){
                //Some dangerous direct inclusion of the $_GET['section']
                $section = $_GET['section'];
                $result = mysql_query("SELECT * FROM `people` WHERE  `occupation` = '$section'", $db );

                while ($row = mysql_fetch_assoc($result)) {
                        echo '<contact id="'.$row['id'].'">';
                                echo '<id>'.$row['id'].'</id>';
                                echo '<first>'.$row['firstname'].'</first>';
                                echo '<middle>'.$row['middle'].'</middle>';
                                echo '<last>'.$row['lastname'].'</last>';
                                echo '<organization>'.$row['org'].'</organization>';
                                echo '<email>'.$row['email'].'</email>';
                                echo '<phone>'.$row['phone'].'</phone>';
                        echo '</contact>';
                }
        }else {
                //Some debugging
                echo '<contact id="0"><first>Could not select database</first></contact>' ;
        }
} else {
        //Some debugging
        echo '<contact id="0"><first>Could not connect to server.</first></contact>' ;
}
echo '</contacts>';
//Close the variable
echo '</e:data>';
echo '</e:variable>'; ?>

So basically this is it. Very simple just use dynamic XML instead of static XML

Comments

for absolute begginers to AJAX

if you could give a detailed view of the following example by showing how the client side and server side code would look like in real situation, it would have been great, just like in the example given by mike potter on the following page http://www.adobe.com/devnet/flex/articles/flex2_php.html

the format of the example shown on that linked page will be greatly appreciated by all the BackBase community!

Good example !!!

I thank you by the excelent explanation. I am a beginner and i need some help like that.

$_GET

I had some problems when I had a phpscript with two $_GET "variables". It seems that the character "&" will cause errors. So When I called "generalinc.php?func=xml&db=project" it didn't work. Is there a workaround or do I have to use only one "$_GET"?

I'm also exploring Backbase

I'm also exploring Backbase and am wanting to expand the PIM with a MySql database.
In this example is doesn't show how to set up the database.
Only showing how to retrieve the data from a database called "People" where the table is called "Occupation".
I can't find in the file index.xml where it states above mentioned part that needs to be changed.
A little more defined example would be more helpfull

well...

what about updating data back to the server ?
what about using databinding on the client side
(instead of using )?
is there some documentation about this, i can't seem to find it anywhere ...

Insert and Update

Very nice, but to make really useful is necessary
show how Insert and Update with this.

Thanks

How can I dynamically load xml content into a b:select?

I wrote a piece of software some months back using backbase 3.3 and I made use of b:select which got it's content from an xml file that was created by another php code.

Now fast forward to today, my boss decides he wants to make the code public (as in no longer designated to his laptop) and hence I have to save the software on two servers (of which one is the backup).

Now the problem I have is that my php code has to create dynamic content for b:select as opposed what I had before where b:select read the content from the generated xml files.

So how do I do this?

Your answers will be very much welcomed.

Thanks in advance.