Monday, December 26, 2011

Downloading as csv/excel file in php using headers

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header("Content-Type: application/" );
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"".date("Y-m-d").".csv\";");
header("Content-Transfer-Encoding:� binary");
header("Content-Length: ".strlen($output));

Tuesday, October 18, 2011

Function for checking if file exists and if so returning a random file name

$filename = fileExists(FILE_PATH.$_FILES['photo_file']['name']);

function fileExists($file){
        $path_parts = pathinfo($file);
        $filename = $path_parts['filename'].".".$path_parts['extension'];
        return $filename;
        $path_parts = pathinfo($file);
        $filename = rand(100,1000).$path_parts['filename'].".".$path_parts['extension'];
        $file = $path_parts['dirname']."/".$filename;
        return fileExists($file);

Monday, September 26, 2011

for getting the text in a span and write it to the parent div

Check the source code of this section..

Friday, July 29, 2011

Php ini settings for large file uploads.

max_execution_time ?
max_input_time ?
memory_limit ?M
post_max_size ?M
upload_max_filesize ?M

Thursday, July 28, 2011

A simple and useful function for escaping user inputs.

After some testing I have developed a function for escaping user inputs. the escaped values can be directly used for inserting to database. It is working fine for me for the last few years..

function safe_escape($string)
$string = htmlentities($string);
$string = mysql_real_escape_string($string);
$string = htmlentities($string);
return $string;

Monday, July 11, 2011

Sorting a multidimensional array in PHP - intelligent method

It is not at all easy to sort a multidimensional array.. After spending hours of time we have find out a solution for it.. check the following:

    [0] => Array
            [message_email] =>
            [message_subject] => p message2
            [message_date] => 2011-06-02 00:00:00
            [message_id] => 4
            [message_name] => test name2
            [user_id] => 26
            [user_display] => Administrator

    [1] => Array
            [message_email] =>
            [message_subject] => test product sub1
            [message_date] => 2011-06-01 00:00:00
            [message_id] => 3
            [message_name] => tet name1
            [user_id] => 26
            [user_display] => Administrator


For eg: I need to sort this multidimensional array based on the message_name
in ascending order.

So I put

$sort = "message_name";
$order_by = "asc";

$asc = create_function ('$a,$b','return strcasecmp($a["'.$sort.'"], $b["'.$sort.'"]);');

$desc = create_function ('$a,$b','return strcasecmp($b["'.$sort.'"], $a["'.$sort.'"]);');






Hope this will help some one.. :)

Tuesday, July 5, 2011

Finding root parent of a child category in php

Using the below recursive function we can able to find the root parent category of a child category.

function getRootParent($id){
             $query = "select nav_parent_id from navigation_categories where nav_id='$id'";
            $result = $this->db->query($query);
                foreach($result->rows as $ids){
                        return $id;
                        return $this->getRootParent($ids['nav_parent_id']);
            return $id;

Listing subcategories or chilld categories using php

We can use the following function for getting the subcategories up to any level. It is using recursive technique.

function getSubCats($catid){
        $query= "select cat_id from category where cat_parent_id='$catid'";
        $rst =  $this->db->query($query);
            return null;
            foreach($rst->rows as $row) {
                $categories[] = $row['cat_id'];
                $children = $this->getSubCats($row['cat_id']);   
                    foreach($children as $child){
                        $categories[] = $child;   
            return $categories;