Monday, February 27, 2012

Import CVS , Excel or XML file into MySQL with php

When we want to insert data into MYSQL which is already present in .CVS (comma separated values) or XML or Excel files. Reading the file row by row (say in PHP) and inserting into MYSQL is very poor style of coding which is inefficient and takes a lot of time. MYSQL has an inbuilt query and I will be elaborating about this.

Convert xml to cvs by php

I have used php 5.0 to convert xmx to cvs. Here "SimpleXML" reads an entire xml into an object that we can iterate through his properties.To write to the csv output file we will use "fputcsv". "fputcsv" formats a line as csv and writes it to the file.

Code of xml file (as the name "cars.xml") is here:

Create favicon (*.ico) with imagick in php

Here is an example that will take any image format supported by Image Magick (is there one that is not?) and create a 16 pixel favicon image from it. The cropThumbnailImage() method does the work of resizing and cropping in a single swoop while the setFormat() method converts the file to .ico format.

Code is here:

Multiple file upload

Uploading multiple files with PHP requires checking for many possible errors. This script allows the setting of how many upload fields will be displayed in the HTML form and the maximum allowable filesize to upload. The php.ini file also contains an ini option named upload_max_filesize which has a default value of 2M, or two megabytes. This value is taken into consideration also when checking for errors.

The errors are stored in an array called messages and for each file upload a message is generated based on any errors or success of the upload. The form itself was validated with the w3c validator in accordance with the DOCTYPE.

Code with details is:

Calculates the age in php

This simple function calculates the age, in years, of a date supplied in any format supported by the strtotime() funcition, which means, basically, any date you can sanely imagine.

Code of function is:

Twenty Four Hour to Twelve Hour

Recently I have created robotic mailing system for my client. But when it needed to convert a bunch of 24 hour times to 12 hours, the times were in different formats but I needed them in a format that was easily human readable. This function will convert a twenty four hour time to twelve hour time. Feel free to modify it to your own needs.

Code is here:

Convert Unix Timestamp To MySQL Timestamp

PHP date functions such as strtotime() and time() and date() all return a value as a UNIX TIMESTAMP. This is great for date manipulation but becomes a problem when we need a date to INSERT into a MySQL database. The format of a MySQL TIMESTAMP is yyyy-mm-dd hh:mm:ss and to meet this need a simple PHP can be utilized to convert any UNIX TIMESTAMP into a MySQL TIMESTAMP.

Code and use of function is:

Thursday, February 9, 2012

Image converter class in php

Image converter is so important to a designer and developer. When you want to upload images in host you should convert it in a desired file system. I have create a class in php to convert image to all kind of image from source file system. By this you can create a online image converter. If you create online image converter using this code or develop this please mail me.

It is so easy to call as the code:
[php]

<?php
require("class.imageconverter.php");

$img = new ImageConverter("2000.gif","wbmp");

?>

[/php]
All of code is here:

Thursday, February 2, 2012

Using PHP/MySQL with Google Maps API

Google map api
Process for using google map api in php:
-> Create a table in database.
-> A file to create a dynamic xml creator code in php
-> Index file to load and display the map including location indicator

Create table in database:
[php]
CREATE TABLE `markers` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`name` VARCHAR( 60 ) NOT NULL ,
`address` VARCHAR( 80 ) NOT NULL ,
`lat` FLOAT( 10, 6 ) NOT NULL ,
`lng` FLOAT( 10, 6 ) NOT NULL ,
`type` VARCHAR( 30 ) NOT NULL
) ENGINE = MYISAM ;
[/php]

Insert the values of attributes in databases table.
as by code:
[php]
INSERT INTO `markers` (`name`, `address`, `lat`, `lng`, `type`) VALUES ('Mr.Insafeta', 'Mohammadpor, Dhaka, Bangladesh', '47.608941', '-122.340145', 'Home');
[/php]

Create database connect page in php (dbinfo.php):