by
faheem on May 15th, 2010
It has been long since written on my blog, how lazy I m, why not get some treatment form the people around me. Why not developers all around chop ma head and screw me up for being so lazy. But mind you don’t try this, coz I belong to the area where people are treated so hard that nobody can even think of. Now don’t ask me where do I belong to.
Its Saturday, off, but I m on and m in great mode. I don’t know why m I feelen so happy though we lost our semi last night. I’m juz chilling.
Gaad daym, what I am written on my technical blog, it shon’t be like this. So lemme move to the topic and put aside this mess and hassles.
Since yesterday I’m try to get this work for me, I’s unable yesterday, but today it really worked for me. Nothing else but COM did the job for me. I m going to share this code with you and m sure that it will serve you lot. Don’t tell anyone that I found this code on the web otherwise I will punch you down and kick you skinny ass.
The code here will read contents form .doc, save those to .txt file and then read contents from that txt file. Here is the code.
$word = new COM("word.application") or die("Unable to instantiate Word");
$word->Documents->Open($file);
$new_filename = substr($file,0,-4) . ".txt";
// the '2' parameter specifies saving in txt format
$word->Documents[1]->SaveAs($new_filename,2);
$word->Documents[1]->Close(false);
$word->Quit();
//$word->Release();
$word = NULL;
unset($word);
$fh = fopen($new_filename, 'r');
// this is where we exit Hell
$res = fread($fh, filesize($new_filename));
fclose($fh);
unlink($new_filename);
echo $res;
So what we’re doing here.
Instantiate COM, open file, save content as .txt file, open .text file and get contents. That’s it.
Juz give it a try and you will find it quite helpful.
by
faheem on February 13th, 2010
You may have heard about FPDF, TCPDF. These are used to generate PDF document in PHP. But very few of you may have heard about LiveDocx, can be found here http://www.livedocx.com/. The most cool news I’m going to tell you that Zend Framework 1.10.0 provides clean and simple interface to LiveDocx API. LiveDocx is soap based service. If you are interested more in LiveDocx, following the link I’ve already provided you.
In this article I am gonna tell you how to create word processing documents and PDF using Zend_Service_LiveDocx. I am sure you will find it very helpful.
You can create you desired document by following these two steps.
1) Create template file
2) Fetch records from your database, as you usually do and assigned them to the template.
1) Creating template file
You can create your template file using Microsoft Word, openOffice and save then as .doc or docx or .rtf format. Keep in mind that creating template file in MS Word and openOffice may differ. In my example I am using Microsoft Word.
Open Microsoft word and create new document.
Before creating the document, I want to show you the resultant template that I’ve created for this example is bellow.

The sign “I” at the stat and end means that I want to repeat this block many time.
And the result file, populated with the data will like this.

Now as you have opened and created document, move forward and insert table as I have inserted in the above example. Put [ in the first row and then click “Insert” in you menu bar and click “Field”. This will open up the following dialog.

Select “Mail Merge” from the “Categories” dropdown and select “MergeField” as show in the following screenshot.

in the “Field name” textbox write “pkey” as show in the following diagram.

Insert all your “MergeField” in the document and create the document you desired. Now if you want to repeat entire block you’ve created or part of it, click at the beginning of that block and select “Insert” form your menu bar and click “Bookmark” as shown in the following diagram.

This will open the following dialog.

I’ve written “blockstart_issue” in the “Bookmark name” text field and pressed “Add” button. Now go to the end of the document or the block you want to repeat, and insert another bookmark using the same method discussed above. But this time the name must be “blockend_issue”. The start bookmark must be like “blockstart_example” and end bookmark should be named “blockend_example”. You can place any text in place of “example” as I’ve put “issue” in my case.
Now as you have defined your template, save it at appropriate place.
2) Fetch records from you database, as we usually do and assigned them to the template.
You have successfully created template file, now its time to instantiate Zend_Service_LiveDocx in your controller as
$phpLiveDocx = new Zend_Service_LiveDocx_MailMerge();
$phpLiveDocx->setUsername("username")
->setPassword("password");
Keep in mind that Zend_Service_LiveDocx is based on soap service, so you will need to get register at http://www.livedocx.com/. When you register with the username and password you will need to set your username and password in place of username and password in theabove two lines.
Next step is to assign values to “MergeField”, you defined while creating your template file. This can simply be done as
$phpLiveDocx->assign(‘pkey’,’TC-4’);
But as I have put bookmarks at the start and end of the entire block, it means that I want that the entire block be repeated for me. So I will first create an associative array as
$issuesData = array(
array (
‘pkey’ => ‘TC-6’,
‘summary’ => ‘Pagination not working in administration area’,
‘created’ => ‘2010/2/12’
……….
…….
)
array (
‘pkey’ => ‘TC-7’,
‘summary’ => ‘Validation not working’,
‘created’ => ‘2010/2/12’
……….
…….
)
);
And now assign this associative array as
$phpLiveDocx->assign(‘issue’, $issuesData);
Here issue is what we defined at the time of putting merge fileds “blockstart_issue” and “blockend_issue”
When you assign data to your “MergeField”, you will need to retrieve the desired document by the following statements.
$document = $phpLiveDocx->retrieveDocument('doc');
unset($phpLiveDocx);
Once the document is generated using the above statement, defined appropriate header and print the created document. I’m using the following statement.
header("Content-Type: application/vnd.ms-word");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("content-disposition: attachment;filename=report.doc");
print $document;
Note: Bookmark will not be visible when you put them in your template. In order to view them select “Tools” from menu bar and then “Options”. Check bookmark checkbox in the view tab in the popup dialog.
by
faheem on July 13th, 2009
While working with AJAX, you will need to get data after sending request to the server. You can use three ways to get the data form the server.
1. In xml Format
2. HTML Format
3. Json Format
Though xml has several advantages, however processing data at the client will put extra burden.
HTML, can easily be handle, is however more resource or you can say bandwidth extensive.
So JSON fall in the middle of both.
PHP provide a very easy way to create JSON response. Once you get data at client side you will need much later code to process response than xml document.
Let’s first look at the server side.
For example we have the following array.
$data = array(
'items'=>array(
'firstname'=>'faheem',
'lastname'=>'abbas',
'address'=>'pakistan'
)
);
To create JSON response, simply write
echo json_encode($data);
This will give the following result
{"items":{"firstname":"faheem","lastname":"abbas","address":"pakistan"}}
Now at the client side to send AJAX request, you will need to create the following code.
Keep in mind that we are using prototype for making AJAX request.
That’s it.
by
faheem on July 2nd, 2009
Session data by default is stored in /tmp directory on the server in the form of text files. It wouldn’t be a good practice to store session information in the default directory because users can easily get these information. You must either change the directory, or the best practice is to store the information the database. Storing session data in the database not only increase your application security, but also provide you an ability to fetch session related information easily from the database.
PHP provide very easy mechanism and functions for storing session information in the database.
In order to store information in the database you will need to have a table containing fields such sessionId for storing unique session Id, sessionData for storing session data and accessDate that store date when session was last accessed.
Once table is successfully created, saving session requires only two steps.
1. Creating function that interact with and store data in the database.
2. Tell PHP to use the functions defined by using session_set_atuo_save() method.
Now let’s discuss these two steps
1. Creating functions for interacting with database
Before using session_set_auto_save() method, we will need to define six method. These method will then be passed as argument to the session_set_auto_save() method. These six methods are each called on the even taken place. Events are fired when session is started, session is closed, session data is read, session data is written, session data is destroyed, and the six argument/function is used for garbage collection. Garbage collection is used to free resources etc.
Now let’s define and discuss functions for each of these events.
(i) Defining a function which will be called when session is started.
$dbConn = null;
function open_session()
{
global $dbConn;
$dbConn = mysqli_connect('host','username','password','dbname') or die('cannot connect to the database');
}
This method is called each time session is started. The function contains code for connecting to the database only. You can place whatever you wish.
(ii) Defining function called when session is closed
function close_session()
{
global $dbConn;
$dbConn = mysqli_close();
}
(iii) Defining function called when session is read
function read_session($sid)
{
global $dbConn;
$q = sprintf('SELECT data FROM sessions WHERE id="%s"', mysqli_real_escape_string($dbConn, $sid));
$r = mysqli_query($dbConn, $q);
if (mysqli_num_rows($r) == 1) {
list($data) = mysqli_fetch_array($r, MYSQLI_NUM);
return $data;
} else {
return '';
}
}
This function take current sessionId as argument and read the data from the database.
(iv) Defining function called when session data is written
function write_session($sid, $data)
{
global $dbConn;
$q = sprintf('REPLACE INTO sessions (id, data) VALUES ("%s", "%s")',
mysqli_real_escape_string($dbConn, $sid), mysqli_real_escape_string($dbConn,
$data));
$r = mysqli_query($dbConn, $q);
return mysqli_affected_rows($dbConn);
}
This function takes two argument sessions current Id and the data to be written. The code written here inserts session id and data into the database table and return number of affected rows.
(v) Defining function, called when session is destroyed
function destroy_session($sid)
{
global $dbConn;
$q = sprintf('DELETE FROM sessions WHERE id="%s"',
mysqli_real_escape_string($dbConn, $sid));
$r = mysqli_query($dbConn, $q);
$_SESSION = array();
return mysqli_affected_rows($dbConn);
}
This function take single argument sessionId and delete the row based on this id.
(vi) Defining function that is called when resource need to be freed.
function clean_session($expire)
{
global $dbConn;
$q = sprintf('DELETE FROM sessions WHERE DATE_ADD(last_accessed, INTERVAL %d SECOND) < NOW()',
(int)$expire);
$r = mysqli_query($dbConn, $q);
return mysqli_affected_rows($dbConn);
}
This function is used to delete session data from the database based on the $expire argument it accepts.
Now as we have defined all the required function, its time to call session_set_auto_save() method.
Before starting the session you would need to call session_set_auto_save() method as
session_set_auto_save('open_session','close_session','read_session','write_session','destroy_session','clean_session');
session_start();
…….