RSS

Monthly Archives: November 2011

[jQuery][error][resolved] GB_CURRENT is null | popup when called from other than onclick didn’t close the popup.

While using Popup jquery plugin, i need to show the popup on text selection instead of that conventional onclick of a link as

getSelected = function() {
var t = '';
if (window.getSelection) {
t = window.getSelection();
} else if (document.getSelection) {
t = document.getSelection();
} else if (document.selection) {
t = document.selection.createRange().text;
}
return t;
}
mouseup = function() {
var st = getSelected();
if (st != '')
{
GB_showCenter('Show popup',url,480,720);
}

}

The issue was when the popup is there in result of onclick of a link, it functions well but when it get called on text selection then its fine except the close function, it gives “GB_CURRENT is null” error and didn’t close the popup window.
I tried multiple ways and the most appropriate was to hide the link and to trigger onclick of the link automatically like

jQuery("div-a").prepend("<div style='display:none'>
<a id='popup' href='{url}' 
onclick=\"return GB_showCenter('Popup', this.href,480,720)\"
;>show popup</a></div>");
document.getElementById('popup').onclick();

Even this didn’t work.
Finally i hide the close link of greybox and added custom close link instead as

<img src="{basepath}/greybox/w_close.gif">
<a style="text-decoration:none;color:grey" 
href={url} onclick="top.window.location=this.href;">Close
</a>

and it closes the popup 🙂

 
Leave a comment

Posted by on November 23, 2011 in javascript

 

Tags: , , , , ,

[php][tutorial] directory sorting by file type and last modified date

Following code is there to list all the files within the specified directory by sorting files by file type and last modified date respectively:

<?php
function sort_by_filetype($src='.')
{
$dirFiles = array();
if ($handle = opendir($src)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
$extn = explode('.',$file);
$type = $extn[count($extn)-1];
$dirFiles[$type.$file] = $file;
}
}
closedir($handle);
}
ksort($dirFiles);
return $dirFiles;
}
function sort_by_filemtime($src='.')
{
$dirFiles = array();
if ($handle = opendir($src)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
$date = filemtime($file);
$dirFiles[$date.$file] = $file;
}
}
closedir($handle);
}
ksort($dirFiles);
return $dirFiles;
}
$src = '.';
echo "<br><strong>Sort By File Type</strong>";
$dirFiles = sort_by_filetype($src);
foreach($dirFiles as $file)
{
echo "<br>".$file;
}

echo "<br><strong>Sort By Time</strong>";
$dirFiles = sort_by_filemtime($src);
foreach($dirFiles as $file)
{
echo "<br>".$file;
}
?>

Thats it 🙂

 
Leave a comment

Posted by on November 18, 2011 in php

 

Tags: , , ,

[mysql][error][solution] mysqldump creates empty file

Mysqldump command is used to take backup of the database. I faced an issue of empty backup file creation while running mysqldump command through php script. To debug this issue following steps are there:

1) Search mysqldump.exe or simply mysqldump in your WAMP, MAMP, LAMP, XAMPP etc directory.
2) First of all run the command in command line window (prompt). For instance mysqldump is residing at c:/lamp/bin/mysql/mysql5.5.8/bin, then from command prompt run

c:/lamp/bin/mysql/mysql5.5.8/bin/mysqldump > 
c:/lamp/backups/backupfilename.sql

and the backup file will be created with following content
Usage: mysqldump [OPTIONS] database [tables]
OR     mysqldump [OPTIONS] –databases [OPTIONS] DB1 [DB2 DB3…]
OR     mysqldump [OPTIONS] –all-databases [OPTIONS]
For more options, use mysqldump –help
3) For taking backup of specific database some parameters are to be specified. Following command is there for taking backup of a specific database

c:/lamp/bin/mysql/mysql5.5.8/bin/mysqldump 
--opt --host=$server --user=$user --password=$password 
$database_name > c:/lamp/backups/backupfilename.sql

Thus, you are done with taking backing from command line by using mysqldump command.

4) Same command is used there within php script for taking backup. Exec() is the php function used for running such commands. In a php file, paste the following code

<?php
exec("c:/lamp/bin/mysql/mysql5.5.8/bin/mysqldump --opt 
--host=$server --user=$user --password=$password 
$database_name > c:/lamp/backups/backupfilename.sql");
?>

And the backup file will be created. To print the error code use

string exec ( string $command [, array &$output [, int &$return_var ]] )

and $return_var will have the error code.

5) Last step is to generalize this command for WAMP, MAMP, LAMP, XAMPP etc as per the location of mysqldump.exe file. Recursively trace all the directories within WAMP, MAMP, LAMP, XAMPP etc in order to find mysqldump.exe path.

Following code is there to do so.

function getDirectory( $path = '.'){

$ignore = array( 'cgi-bin', '.', '..','htdocs','www' );
$dh = @opendir( $path );
while( false !== ( $file = readdir( $dh ) ) ){
if( !in_array( $file, $ignore ) ){
if( is_dir( "$path/$file" ) ){
getDirectory( "$path/$file");
} else {
if($file == 'mysqldump')
{
$_SESSION['mysqldump_location'] = $path.'/';
break;
}
}
}
}
closedir( $dh );
}
$_SESSION['mysqldump_location'] = '';
getDirectory($root);
$src = $root."/backup.sql";
$cmd = $_SESSION['mysqldump_location']."mysqldump 
--opt --host=$host --user=$user --password=$pass $name >"
.$src;
exec($cmd,$output,$rtn);

Thats it 🙂

 
1 Comment

Posted by on November 18, 2011 in mysql

 

Tags: , , , ,

[wordpress][tutorial km tip] How to Call plugin function at front end? How to display record listing at front end?

To hook any plugin function at back end, following code is there

add_action('admin_menu', 'action_here');
function action_here() {
add_users_page('Customized Functionality', 'Customized Functionality', 'administrator',
'action_here', 'action_here_function_call');
}
function  action_here_function_call()
{
// functionality definition here.
}

But how to hook  action_here_function_call() at front end without modifying theme.
As in drupal we have ‘Pages’ which execute php code when ‘PHP Filter’ module is enabled, i need the same in wordpress, but wordpress didn’t filter php code by default.
Then I get Exec-PHP plugin, when it get activated any php code in post, pages etc get executed.

Simply create new wordpress page ({base_url}wp-admin/post-new.php?post_type=page) and paste following code in html view:

<?php

action_here_function_call()

?>

Through Custom Menu ({base_url}wp-admin/nav-menus.php) customized tab for the page can be created.
Following are few considerations while executing php code through Exec-PHP plugin:
1) Try to avoid php tags <?php ?> within the function as much as you can, because i observed that tags, within the particular function called at front end, break the code.
2) Avoid extra spaces in javascripting. If there is any js in your function then to execute it remove extra spaces.

Have a nice learning with opensource cms 🙂

 
Leave a comment

Posted by on November 4, 2011 in wordpress

 

Tags: , , , , , ,

[php][tutorial] Simplest Pagination in PHP

Here is simplest code for pagination in php. Comments are there to make code self explanatory.

<?php

$data = array('1'=>'1','2'=>'2','3'=>'3','4'=>'4',
'5'=>'5','6'=>'6','7'=>'7','8'=>'8','9'=>'9','10'=>'10',
'11'=>'11','12'=>'12','13'=>'13','14'=>'14','15'=>'15',
'16'=>'16','17'=>'17','18'=>'18','19'=>'19','20'=>'20',
'21'=>'21');

$num_data = count($data);
if(!empty($_GET["start"])){
$start = $_GET['start'];
}else{
$start  = 0;
}

$limit           = 5; // Records per page
$current      = $start + $limit;
$back          = $start - $limit;
$next          = $start + $limit;
//Previous Page
$nav = '';
if($back >=0) {
$nav .= "<a href='?start=$back'>&laquo; Previous</a>
&nbsp;&nbsp;";
}

// Show navigation links
$x    = 1;
for($i=0;$i < $num_data;$i=$i+$limit){
if($i != $current){
$nav .= "<a href='?start=$i'>$x</a> ";
}else {
$nav .= "<b>$x </b>"; // bold selected page
}
$x    = $x+1;
}
// Next Page
if($current < $num_data) {
$nav .= "<a href='?start=$next'>Next &raquo;</a>";
}

$listing = '';
for($i= ($start+1); $i<= ($start+$limit) ; $i++)
{
$listing .= $data[$i]."<br>";
}

echo "<div align=center>".$listing."Pages: ".$nav."</div>";

?>

Thats it 🙂

I think its the simplest code for pagination in php, if you think its not, then please provide yours 🙂

Note: For showing the current number of records, following code is there

if($start+$limit >$num_data)
$c = $num_data;
else
$c = ($start+$limit);

echo "Records: ".($start+1)."-".$c." of ".$num_data; 
//1-10 of 20 etc
 
Leave a comment

Posted by on November 3, 2011 in php

 

Tags: , , , , , , , ,

[wordpress][error][solution] filemtime or filectime gives 1293858000 (i.e. 01-01-1970 05:00:00)

It takes hours to debug the issue i faced while listing all the files (residing in nested set of directories) with file’s last modified date and file size. It displays 01-01-1970 05:00:00 as a last modified date for all the files and empty file size. I was coding wordpress plugin and the issue arises there. When i paste the code in custom php file it works fine and get correct last modified date and size.
File path was correct as i could see the file content and filename correctly, then whats the magic behind that static date and empty size.

It was all about relative and absolute path.

I was getting file last modified date by filemtime($filename) and file size by filesize($file).
I just make the file path absolute by concatenating complete physical path before $filename as file last modified date by filemtime($physical_path.$filename) and file size by filesize($physical_path.$file).
And alhamdullilah it gets functional 🙂

Note: In most of the cases when built-in function is not correctly displaying the result then ist of all print $_SERVER[‘SCRIPT_FILENAME’]; to check out the path of the executed code (as in wordpress, code in plugin binded by admin related hooks is get called by specific file within wp-admin directory, so specially in wordpress every hook is called by its own file so for relative path one should need to know that particular file or have to use absolute path)

 
Leave a comment

Posted by on November 1, 2011 in wordpress

 

Tags: , , , , , ,

 
%d bloggers like this: