Thursday, August 27, 2009

The text to be printed is contained in a div



The id of the div is passed to the function.

Click to print div 1

On clicking the link the following sequence of events happens.

1. A new window is opened
2. The contents of the div are dynamically written to the new window
3. Data is sent to the printer
4. The new window is automatically closed (IE only)

Other areas of a page can be printed simply by defining another div and link



Click to print div 2


start the javascrpt tab


close the javascrpt tab

Tuesday, August 25, 2009

Resources Libraries Helpers Plugins For Code Igniter

Here you will find a lots of libraries,helpers plugins for CI


http://codeigniter.com/wiki/Summary_of_Resources_Libraries_Helpers_Plugins.../


Thanks
Jawed

Using Zend Framework with CodeIgniter

If you ever wanted to integrate CodeIgniter and Zend Framework, you might have come across this tutorial by Daniel Vecchiato.

Whilst Daniel has done a great job demonstrating the possibility of using the two frameworks together, concerns have been made: do we actually need to use hooks?

As I understand it, hooks are used to extend the core functionalities of CodeIgniter (as explained in the user guide). Obviously Zend Framework and CodeIgniter are two different systems and there is no intention for us to extend CodeIgniter’s core functionality with Zend Framework.

Using hooks can be dangerous as it’s system-wide, and it modifies the system behaviour.

What I have done is to simply use CodeIgniter’s library structure to load the Zend Framework resources. Below is the tutorial.

Assuming you already have CodeIgniter installed. If not please refer to the user guide for installation.

1. Download Zend Framework from the official website.
2. Unzip the Zend Framework package, and copy the Zend folder (under Library) to your CodeIgniter installation’s application/libraries/. You can actually place the folder anywhere, but remember to alter the script accordingly (read the comments in the script!).
3. Place the library script (provided at the end of the post) in application/libraries/
4. Done! That’s all you need to do. Now, let us see an example of using the library.

Usage Sample


01.02.
03.class Welcome extends Controller {
04.
05. function Welcome()
06. {
07. parent::Controller();
08. }
09.
10. function index()
11. {
12. $this->load->library('zend', 'Zend/Service/Flickr');
13. // newer versions of CodeIgniter have updated its loader API slightly,
14. // we can no longer pass parameters to our library constructors
15. // therefore, we should load the library like this:
16. // $this->load->library('zend');
17. // $this->zend->load('Zend/Service/Flickr');
18.
19. $flickr = new Zend_Service_Flickr('YOUR_FLICKR_API_KEY');
20.
21. $results = $flickr->tagSearch('php');
22. foreach ($results as $result)
23. {
24. echo $result->title . '
';
25. }
26. //$this->load->view('welcome_message');
27. }
28.}
29.?>

Library Script

Copy the code and paste it to a new file called Zend.php in application/libraries/.

01.02.
03./**
04. * Zend Framework Loader
05. *
06. * Put the 'Zend' folder (unpacked from the Zend Framework package, under 'Library')
07. * in CI installation's 'application/libraries' folder
08. * You can put it elsewhere but remember to alter the script accordingly
09. *
10. * Usage:
11. * 1) $this->load->library('zend', 'Zend/Package/Name');
12. * or
13. * 2) $this->load->library('zend');
14. * then $this->zend->load('Zend/Package/Name');
15. *
16. * * the second usage is useful for autoloading the Zend Framework library
17. * * Zend/Package/Name does not need the '.php' at the end
18. */
19.class CI_Zend
20.{
21. /**
22. * Constructor
23. *
24. * @param string $class class name
25. */
26. function __construct($class = NULL)
27. {
28. // include path for Zend Framework
29. // alter it accordingly if you have put the 'Zend' folder elsewhere
30. ini_set('include_path',
31. ini_get('include_path') . PATH_SEPARATOR . APPPATH . 'libraries');
32.
33. if ($class)
34. {
35. require_once (string) $class . EXT;
36. log_message('debug', "Zend Class $class Loaded");
37. }
38. else
39. {
40. log_message('debug', "Zend Class Initialized");
41. }
42. }
43.
44. /**
45. * Zend Class Loader
46. *
47. * @param string $class class name
48. */
49. function load($class)
50. {
51. require_once (string) $class . EXT;
52. log_message('debug', "Zend Class $class Loaded");
53. }
54.}
55.
56.?>


Happy coding! Oh and don’t forget, Zend Framework is PHP 5 only, so it won’t work on your PHP 4 installation.
Update: Using it with Kohana
Update: The following instructions are deprecated, please follow the updated one here.

Even though Kohana has the ability to load vendor classes, I still find it useful to use the library approach so that loading Zend Framework libraries will be transparent. :)

Usage is exactly the same as in CodeIgniter.

01.02.
03./**
04. * Zend Framework Loader
05. *
06. * Put the 'Zend' folder (unpacked from the Zend Framework package, under 'Library')
07. * in CI installation's 'application/libraries' folder
08. * You can put it elsewhere but remember to alter the script accordingly
09. *
10. * Usage:
11. * 1) $this->load->library('zend', 'Zend/Package/Name');
12. * or
13. * 2) $this->load->library('zend');
14. * then $this->zend->load('Zend/Package/Name');
15. *
16. * * the second usage is useful for autoloading the Zend Framework library
17. * * Zend/Package/Name does not need the '.php' at the end
18. */
19.class Zend
20.{
21. /**
22. * Constructor
23. *
24. * @param string $class class name
25. */
26. function __construct($class = NULL)
27. {
28. // include path for Zend Framework
29. // alter it accordingly if you have put the 'Zend' folder elsewhere
30. ini_set('include_path',
31. ini_get('include_path') . PATH_SEPARATOR . APPPATH . 'libraries');
32.
33. if ($class)
34. {
35. require_once (string) $class . EXT;
36. Log::add('debug', "Zend Class $class Loaded");
37. }
38. else
39. {
40. Log::add('debug', "Zend Class Initialized");
41. }
42. }
43.
44. /**
45. * Zend Class Loader
46. *
47. * @param string $class class name
48. */
49. function load($class)
50. {
51. require_once (string) $class . EXT;
52. Log::add('debug', "Zend Class $class Loaded");
53. }
54.}
55.
56.?>

Thursday, August 13, 2009

Sending mail via Mercury on localhost using codeigniter and XAMPP

This is my first time to use Mercury on XAMPP, I never had a mail project before so I guess I'm gonna blog it for future reference. Obviously, we will be using SMTP to send mails and POP3 to receive them. Since we do not have sendmail installed in Windows XP.

Using Mercury bundled witn XAMPP is so straight forward. I am assuming you have already installed XAMPP and Mercury Mail Transport is already running. I also assume you already know how to setup a mail client (eg Outlook, Outlook Express, ThunderBird, EudoraMail are few of them). Setting up a mail client is not covered here.

Requirements:

* XAMPP - Our webserver
* Codeigniter - PHP Framework
* Mail Client - Receive mails


Now, all we need to do is Add Users for testing:
1. Open XAMPP Control Panel and Click on Start on the Mercury Section
2. Click on the Admin Button to Add Users
3. On the Mercury Menu, go to Configuration -> Manage Local Users
4. To Add an account Click on ADD
5. Add your desired test Accounts by filling in the Username, Name and Password field
6. Then click OK, then click Close




Before testing, you might want to configure codeigniter's config to store your sender accounts. To do that:
1. Navigate to your codeigniter application->config folder
2. You may create you own config file or add them in the email.php config
3. if email.php does not exist you may create one.
4. You can use the basic config for sending smtp/pop3 below:


$config['protocol'] = 'smtp';
$config['smtp_host'] = 'localhost';


5. You may add your test account so you don't have to manually type them whenever you send.Take note, I used localhost.com as my host name. Mail clients may not permit you to add an account with just localhost so adding .com, .net, .org will do the trick.


$config['protocol'] = 'smtp';
$config['smtp_host'] = 'localhost';
$config['email_address'] = 'sender@localhost.com';
$config['email_name'] = 'Localhost test';


6. Remember not to close your 7. If you want to, you can also configure codeigniter to autoload the email config by adding it at the autoload.php file

We can now start sending mail with code igniter:
1. At the application->controllers create a controller class:

class Test_Mail extends Controller {
function Test_Mail()
{
parent::Controller();
}
function index()
{
// get config data
$from = $this->config->item('email_address', 'email');
$from_name = $this->config->item('email_name', 'email');
$to = 'tildemark@localhost.com';
$to_name = "To Name";
// we load the email library and send a mail
$this->load->library('email');
$this->email->from($from, $from_name);
$this->email->to($to, $to_name);
$this->email->subject('Email subject');
$this->email->message('Testing the email class.');
$this->email->send();
//to debug we can use print_debugger()
echo $this->email->print_debugger();
}
}

2. On your browser, type in http://localhost/codeigniter/test_mail
3. Check you mail client for new mails.
4. Thats it! We have successfully sent a mail using Mercury on XAMPP on in your local computer.