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/vnd.ms-excel" );
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){
  
    if(!file_exists($file)){
        $path_parts = pathinfo($file);
        $filename = $path_parts['filename'].".".$path_parts['extension'];
        return $filename;
    }else{
        $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..
testhiiii

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)
{
if(get_magic_quotes_gpc()){
$string = htmlentities($string);
}else{
$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:


Array
(
    [0] => Array
        (
            [message_email] => test3@test.com
            [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] => test@test.com
            [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.'"]);');

if($order_by=="asc")

usort($rows,$asc);

else

usort($rows,$desc);




print_r($rows)




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){
                    if($ids['nav_parent_id']==0){
                        return $id;
                    }else{
                        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);
        if($rst->num_rows==0){
            return null;
        }else{
            foreach($rst->rows as $row) {
                $categories[] = $row['cat_id'];
                $children = $this->getSubCats($row['cat_id']);   
                if($children!=null){
                    foreach($children as $child){
                        $categories[] = $child;   
                    }
                }
            }
           
            return $categories;
        }

    }