Archive

Archive for October, 2009

Put a module inside article

October 6, 2009 1 comment

You can insert a module inside an article using the {loadposition positionname}.
The plugin “content-Load Module” must be enabled.

Optimize php code

October 6, 2009 Leave a comment

1.) Multiple argument with echo:
You use echo probably like this
echo ‘first string’.$variable.’second string ‘;
Use this code it is faster
echo ‘first string’, $variable, ‘second string’;
Passing multiple argument is faster than concate string. echo is faster than print. print take one argument.

2 .) Use  single quotes:
In double quotes everything is parsed, with single quotes string is not parsed.
if you need to print only string then use
echo ‘string’;
than
echo “string”;
3.)Pre-increment is faster than post-increment:
++$var;
is faster than
$var++;

4.) Use split() instead of explode():
split() uses
regular expressions, while explode()  not.

5.) Use isset() instead of strlen():
Example:
( i ) $str  = “string”;
if(strlen($str) < 4){
echo “String length is less than 4”;
}

( ii ) $str   = “string”;
if(!isset($str{4}){
echo “String length is less than 4”;
}

faster than this:
$var++;

 

Upload file

October 6, 2009 1 comment

Add this code in   view/tmpl/
<form name=”upload” method=”post” enctype=”multipart/form-data”>
<input type=”file” name=”file_upload” />
<input type=”submit” name=”submit” value=”submit”/>
<input type=”text” name=”option” id=”option” value=”com_fileuplaod”/>
<input type=”text” name=”view” id=”view” value=”fileuplaod”/>
<input type=”text” name=”task” id=”task” value=”uploadFile”/>
</form>

Add this function in controller:

function uploadFile(){
$file = JRequest::getVar(‘file_upload’, null, ‘files’, ‘array’);
jimport(‘joomla.filesystem.file’);
$filename = JFile::makeSafe($file[‘name’]);
$src = $file[‘tmp_name’];
$dest = JPATH_COMPONENT . DS . “uploads” . DS . $filename;
if ( strtolower(JFile::getExt($filename) ) != ‘jpg’) {
if ( JFile::upload($src, $dest) ) {
//Redirect to a page of your choice
parent::display();
} else {
//Redirect and throw an error message
}
} else {
//Redirect and notify user file is not right extension
}
}

function uploadFile(){
$file = JRequest::getVar(‘file_upload’, null, ‘files’, ‘array’);
jimport(‘joomla.filesystem.file’);
$filename = JFile::makeSafe($file[‘name’]);
$src = $file[‘tmp_name’];
$dest = JPATH_COMPONENT . DS . “uploads” . DS . $filename;
if ( strtolower(JFile::getExt($filename) ) != ‘jpg’) {
if ( JFile::upload($src, $dest) ) {
//Redirect to a page of your choice
parent::display();
} else {
//Redirect and throw an error message
}
} else {
//Redirect and notify user file is not right extension
}
}
Categories: Joomla 1.5

Create excel file through php

October 3, 2009 Leave a comment

$html = array();
$html[] = “<table border=0>”;
$html[] = “<tr><td>Name</td>”;
$html[] = “<td>Yaju</td></tr>”;
$html[] = “</table>;
$html = implode(“\r\n”, $html);
$fileName = date(“d-m-Y”).’_mysql.xls’;
header(“Content-type: application/vnd.ms-excel”);
header(“Content-Disposition: attachment; filename=Client_Survey.xls”);
echo $html;

%d bloggers like this: