If you want to generate documents from your data (database, xml or any other source), I’ve found that odtPHP does a very good job. So here is a simple tutorial on how to use this library.
1. Download odtPHP from http://www.odtphp.com/index.php?i=download
2. Extract the zip file somewhere where you like, you will see there will be two folders “library” and “tests”. Rename the “library” folder to “odtPHP” and put it in your codeigniter “plugin” folder.
3. In the plugin folder create the file “odf_pi.php”, and put this content inside
require_once("odf/odf.php");
4. For the library to work, you need a temporary folder where odfPHP can write to when generating the document. Create a folder in the root of your application, name it for example “tmp”, then open your index.php file of your CI app, and put this line
define('TMP_PATH', str_replace(SELF, '', __FILE__) . 'tmp/');
just beneath this line
define('BASEPATH', $system_folder.'/');
5. Open file “odf.php”, you’ve got this array at the beginning of the file
protected $config = array(
'ZIP_PROXY' => 'PclZipProxy',
'DELIMITER_LEFT' => '{',
'DELIMITER_RIGHT' => '}',
'PATH_TO_TMP' => null
);
replace
with
'PATH_TO_TMP' => TMP_PATH
With this final step, the configuration is done. Now here is a simple function on how to use the plugin.
When creating the template with Open Office, make sure you put the variables to be filled, like in this example
function code to export the generated document
$this->load->plugin('odf');
//path of the template file
$odf = new odf("assets/odt/template.odt");
//fill the template with the variables
$odf->setVars('title', 'Hello World!!!');
//export the file
$odf->exportAsAttachedFile('export.odt');
Last night (very late night), I decided to start my first official project.
Check it out here FB.Auth
Yesterday, half of my day instead of doing productive work, I spent it searching to find a solutions so that IE (Internet Explorer), stops caching ajax get results. Found several suggestions, but the one that worked for me was this
<meta http-equiv='expires' content='0'>
<meta http-equiv='pragma' content='no-cache'>
Hope it helps anyone as it did me.
Just yesterday I saw a post on the Codeigniter forum, it was a question about setting the timezone every time a query is run. I had a similar problem before so this is the solution I used
class Master_model extends Model{
public function __construct(){
parent::__construct();
$this->set_timezone();
}
public function set_timezone(){
$this->db->query("SET time_zone='+0:00'");
}
}
class Some_model extends Master_model{
public function __construct(){
parent::__construct();
}
}
By using this method I can write custom functions and logic that I can use in other models without repeating the same code every time.
See you in the next post.
When developing different kind of applications, there will come a time when you’ll be deploying your application in different versions of php (4.x or 5.x). Several times I had to go through the php classes and modify the php contructor from
/**
* PHP 4 Compatible Constructor
*/
function myClass() {
//some code here
}
to
/**
* PHP 5 Constructor
*/
function __construct() {
//some code here
}
and lately to save myself from the stupidity, I combined these two to this
/**
* PHP 4 Compatible Constructor
*/
function myClass() {
$this->__construct();
}
/**
* PHP 5 Constructor
*/
function __construct() {
//some code here
}
With this little change your application will be a bit more near compatibility to php4 and php5.
See you on the next post.
CHAR and VARCHAR data types are pretty similar but with some differences between them.
CHAR is saved in fixed length (the same length you used when declaring the column), whilst VARCHAR is saved in variable length. Lets take an example, using this table
CREATE TABLE lengths(
column_char char(5),
column_varchar varchar(5)
);
If I insert two values in the table, like
INSERT INTO lengths(column_char, column_varchar) VALUES('yes', 'yes');
In the column that saves the data in VARCHAR the size will be 3 bytes, while in CHAR it will be 5 bytes (why the hell?? Remember fixed length and variable length). Depending on MySQL version CHAR and VARCHAR might be handled differently upon insertion and extraction, for further details visit the MySQL documentation site.
Many times I’ve seen that file contents are saved in a VARCHAR, please avoid that use BLOB data type, that’s why it’s been invented.
See you in the next post.
I am starting with String data types of MySQL.
Categorized under string data types in MySQL, we have these types
- CHAR and VARCHAR types
- BINARY and VARBINARY types
- BLOB and TEXT types
- ENUM type
- SET type
Following the next posts, I will explain each of them and their usage.
See you in the next post.
Today while working I needed to count an array, I knew the function count(), everything was working, until the array was empty, in which case count() function was returning 1. It was wrong, in which case, a solution to all this misunderstanding was this simple code snippet
if(is_array($list_of_something))
{
$count = count($list_of_something);
//some other code
}
MySQL v5.x has a long list of data types it can manage. Starting from numbers, characters, strings, blobs etc.
There are 3 main categories of data types that MySQL supports and manages
- String types
- Numeric types
- Date and Time types
Starting today I will try to explain each of the data types in detail.
MySQL
See you in the next post.
As every application there is a starting point, some prefer drawing, some designing etc., as the first step of application development. I prefer separating the modules, and based on the module to build the database.
I will be using MySQL v5.1. UTF-8 will be the encoding for multilanguage support, and InnoDB storage engine (since it gives support for table relationships and foreign keys).
Lets start with the first and most important module of the blog
User Module
Here is a list of requirements for the user module (as I’ve imagined them)
- User authentication will be done by email
- User will have a role that will define its rights in the application
- User will have a thumbnail
- Every user will have to activate the account by email (except the first user)
- User will have a last login time
- User update date and time and by whom it was updated
Extracted from these requirements this is the user table schema
/*Table structure for table `user` */
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`user_id` smallint(6) NOT NULL AUTO_INCREMENT,
`user_first_name` varchar(100) COLLATE utf8_bin DEFAULT NULL,
`user_last_name` varchar(100) COLLATE utf8_bin DEFAULT NULL,
`user_email` varchar(30) COLLATE utf8_bin NOT NULL,
`user_password` varchar(50) COLLATE utf8_bin NOT NULL,
`user_hash` varchar(50) COLLATE utf8_bin NOT NULL,
`user_role_id` tinyint(4) NOT NULL,
`user_status_id` tinyint(4) NOT NULL,
`user_last_login` datetime DEFAULT NULL,
`user_updated` datetime DEFAULT NULL,
`user_updated_by` smallint(6) DEFAULT NULL COMMENT 'user_id',
PRIMARY KEY (`user_id`),
KEY `FK_user_user_role` (`user_role_id`),
KEY `FK_user_user_status` (`user_status_id`),
KEY `FK_user_user` (`user_updated_by`),
CONSTRAINT `FK_user_user` FOREIGN KEY (`user_updated_by`) REFERENCES `user` (`user_id`) ON UPDATE CASCADE,
CONSTRAINT `FK_user_user_role` FOREIGN KEY (`user_role_id`) REFERENCES `user_role` (`user_role_id`) ON UPDATE CASCADE,
CONSTRAINT `FK_user_user_status` FOREIGN KEY (`user_status_id`) REFERENCES `user_status` (`user_status_id`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
/*Data for the table `user` */
/*Table structure for table `user_role` */
DROP TABLE IF EXISTS `user_role`;
CREATE TABLE `user_role` (
`user_role_id` tinyint(4) NOT NULL AUTO_INCREMENT,
`user_role` enum('USER_ADMIN','USER_POWER','USER_LIMITED') COLLATE utf8_bin NOT NULL,
`user_role_name` varchar(100) COLLATE utf8_bin NOT NULL,
PRIMARY KEY (`user_role_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
/*Data for the table `user_role` */
/*Table structure for table `user_status` */
DROP TABLE IF EXISTS `user_status`;
CREATE TABLE `user_status` (
`user_status_id` tinyint(4) NOT NULL AUTO_INCREMENT,
`user_status` enum('USER_ACTIVE','USER_PASSIVE','USER_BLOCKED') COLLATE utf8_bin NOT NULL,
`user_status_name` varchar(100) COLLATE utf8_bin NOT NULL,
PRIMARY KEY (`user_status_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
/*Data for the table `user_status` */
As you can see from the script here the roles I’ve saved them in the ENUM type, which isn’t recommended but since this isn’t the WordPress killer, I didn’t want to over-complicate it.
So this is the first part of the database, if you’ve got anything to suggest, criticize or just say hello don’t hesitate.
See you on the next post.
Categories: 1.7.x, CSS 2, CodeIgniter, Framework, HTML, Javascript, MySQL, PHP, Programming, RDBMS, SQL Tags: