Check out my latest tutorial site at www.startutorial.com

simple easy way to extract string value

July 6th, 2011

This simple function will extract string between $start and $end in $text:

public function extract($text,$start,$end)
{

    if(preg_match('/'.$start.'(.*)'.$end.'/',$text,$matches)){

        return $matches[1];

    }

    return '';
}

smartDuration PHP snippet

June 30th, 2011

Find a period string by comparing two ISO 8601 (YYYY-MM-DD) dates.

function smartDuration($from,$to)
    {
	    //some variables
	    $seperator = '-';
	    $key        = 'to';
	    //from and to array
		$from   = explode('-',$from);
		$to     = explode('-',$to);

		//same year
		if($from[0]==$to[0]){
			array_shift($to);
			//same month
			if($from[1]==$to[0]){
				array_shift($to);
				//same day
				if($from[2]==$to[0]){
					array_shift($to);
					return implode($seperator,$from);
				}
			}
		}
		return implode($seperator,$from).(' '.$key.' ').implode($seperator,$to);
    } 

    $from = '2011-01-02';
    $to = '2012-01-03';
    echo smartDuration($from,$to);
    //2011-01-02 to 2012-01-03

    $from = '2011-01-02';
    $to = '2011-02-03';
    echo smartDuration($from,$to);
    //2011-01-02 to 02-03

    $from = '2011-01-02';
    $to = '2011-01-03';
    echo smartDuration($from,$to);
    //2011-01-02 to 03

    $from = '2011-01-02';
    $to = '2011-01-02';
    //2011-01-02

Why I want to revamp this site

June 29th, 2011

Let me know if you have other reasons which can motivate me, so I can finally go and rebuild this site.

Below are my reasons:

  1. It is ugly.
  2. I should write code from scratch(well, maybe with CakePHP), because I do not want it to be a WP blog.
  3. It is messy. Yes it is categorized with categories, but it is still messy due to the WP structure, I can only have pages and posts.

WordPress paging navigation (with numbers)

May 10th, 2011

I found this wordpress paging function from http://robertbasic.com/blog/wordpress-paging-navigation/.

However I found some bugs when using it with odd number as range.

After a few hours debugging, I solved the problem by modifying part of this scripts.

The code modified part is:

if($max_page > $range){
//modified by The-Di-Lab
$start= ($paged - $range <= 0)?1:($paged - $range);
$end  = ($max_page - $paged >=$range)?$paged+$range: $max_page;
for($i = $start; $i <= $end; $i++){
echo "<li><a href='" . get_pagenum_link($i) ."'";
if($i==$paged) echo "class='current'";
echo ">$i</a></li>";
}

Below is the final code:

<?php
/**
* A pagination function
* @param integer $range: The range of the slider, works best with even numbers
* Used WP functions:
* get_pagenum_link($i) - creates the link, e.g. http://site.com/page/4
* previous_posts_link(' « '); - returns the Previous page link
* next_posts_link(' » '); - returns the Next page link
* code modified by The-Di-Lab
* www.the-di-lab.com
*/
function get_pagination($range = 4){
// $paged - number of the current page
global $paged, $wp_query;
// How much pages do we have?
if ( !$max_page ) {
$max_page = $wp_query-&gt;max_num_pages;
}
// We need the pagination only if there are more than 1 page
if($max_page &gt; 1){
if(!$paged){
$paged = 1;
}
// On the first page, don't put the First page link
if($paged != 1){
echo "&lt;a href=" . get_pagenum_link(1) . "&gt; First &lt;/a&gt;";
}
// To the previous page
previous_posts_link(' « ');
// We need the sliding effect only if there are more pages than is the sliding range
if($max_page > $range){
//modified by The-Di-Lab
$start= ($paged - $range <= 0)?1:($paged - $range);
$end  = ($max_page - $paged >=$range)?$paged+$range: $max_page;
for($i = $start; $i <= $end; $i++){
echo "<li><a href='" . get_pagenum_link($i) ."'";
if($i==$paged) echo "class='current'";
echo ">$i</a></li>";
}
}
// Less pages than the range, no sliding effect needed
else{
for($i = 1; $i &lt;= $max_page; $i++){
echo "&lt;a href='" . get_pagenum_link($i) ."'";
if($i==$paged) echo "class='current'";
echo "&gt;$i&lt;/a&gt;";
}
}
// Next page
next_posts_link(' » ');
// On the last page, don't put the Last page link
if($paged != $max_page){
echo " &lt;a href=" . get_pagenum_link($max_page) . "&gt; Last &lt;/a&gt;";
}
}
}
?>;

 

jQuery multiple ajax call

May 9th, 2011

You need to be very careful if you are calling jquery ajax in your event handler.

As ajax is async, if you or your user triggers the event too fast, it may cause multiple ajax call.

For example, if you did this

$('#mybutton').click(function(){
$.ajax({
blah: blah,
blah: blah,
success: function(data){
doSomeStuffWithData(data);
}
})
});

Now click on the “#mybutton” continuously on IE browsers.

You may find some very weird results depends on what your doSomeStuffWithData() function does.

If your function works fine, you can skip this post now. If you find something weird, that is when you need to continue.

The solution to the issue is to queue your ajax call.

How? Thanks to http://stackoverflow.com/questions/1058158/can-somebody-explain-jquery-queue-to-me

Put below codes into your JavaScript file.

// jQuery on an empty object, we are going to use this as our Queue
var ajaxQueue = $({});

$.ajaxQueue = function(ajaxOpts) {
// hold the original complete function
var oldComplete = ajaxOpts.complete;

// queue our ajax request
ajaxQueue.queue(function(next) {

// create a complete callback to fire the next event in the queue
ajaxOpts.complete = function() {
// fire the original complete if it was there
if (oldComplete) oldComplete.apply(this, arguments);

next(); // run the next query in the queue
};

// run the query
$.ajax(ajaxOpts);
});
};

And modify your event handler to use $.ajaxQueue instead of $.ajax .

$('#mybutton').click(function(){
$.ajaxQueue ({
blah: blah,
blah: blah,
success: function(data){
doSomeStuffWithData(data);
}
})
});

From mediodesign.ca

January 2nd, 2011

Marc-Andre Larin
Medio Design
Mediodesign.ca

“If you are looking for an amazing support, great price and quick reply to your questions, go for The-Di-Lab.”

We have been working with The-Di-Lab for the last few months for a very important web application for our company.

The-Di-Lab made it easy by replying to our questions/comments everyday, wich made the communication easy. Even after our last payment was made, the support was as good as it was when the project was in progress. We never thought working with programmers only by e-mails was that easy. The-Di-Lab understood our needs really quickly.

The price they gave us was amazing. Low price, great service. We will deal with The-Di-Lab for more projects for sure.

How to delete Facebook account permanently

November 30th, 2010

If you ever wonder how to delete a Facebook account permanently, here is a guide for you.

  1. Backup your Facebook data.
    If you don’t want to lose all your data, you should backup all your data which includes your photo albums, friends, profile information, status updates and son. To do this, sadly Facebook does not have any official utility. However you can find tons of Facebook applications which will do this for you.Go to http://www.facebook.com/apps/directory.php, and type in “facebook backup” in the search box, you should get lots of backup utilities. So choose anyone and backup your data.backup
  2. After you have successfully backup, login your Facebook account and go to URL http://www.facebook.com/help/contact.php?show_form=delete_account, you should be able to see the page as below:step1
  3. After reading the statement above, please click submit button, you will be asked last time to confirm your operation.step2
  4. That’s all, you should receive emails from Facebook stated your account will be deleted after 14 days.

From rfrye.com

August 14th, 2010

Rob Frye
rob@rfrye.com

“The-Di-Lab exceeded our expectations with a great product and fast turn around every time”


We have been working with The-Di-Lab for about 6 months and have completed 3 projects together. Each project has had its own challenges and learning that went along with it. The-Di-Lab exceeded our expectations with a great product and fast turn around every time.

We recently found ourselves in a time crunch on a project. We had a lot of changes to make to a website, but didn’t want to get down and dirty in the changes before we could get past the annoying browser issues we had. After contacting The-Di-Lab we knew they would be able to meet our fast timeline. Not only did The-Di-Lab meet the deadline but they beat it by 24 hours giving us extra time to finess the project and have a smooth deployment.

z-index ie7 problem

August 2nd, 2010

When using z-index on my multiple level menu today, I discovered z index is not working properly in IE7. After trying all different method, I had z-index fixed, and it is finally working fine.

Two common reasons why z-index is not working in ie7 according to my experience.

1> z-index only works on positioned elements (position:absolute, position:relative, or position:fixed), so please make sure you have given the correct positions value before you use z-index.
so you have to give {position:absolute},  or {position:relative} , or {position:fixed} to the element which you want to apply z-index property.

2>When working in ie7,  you need to take note that, z-index is applied from parent to children. and it calculates the position on the same level. So you need to make sure you are applying z-index in correct element, best practice would be applied them on parent elements.

I had z index ie7 fixed, and I hope my post would help you solve your website’s bug too.

Colorbox: resize iframe dynamically

July 30th, 2010

As Colorbox can not auto calculate iframe’s width and height, therefore developer has to give a specific width and height when using Colorbox with iframe.

And here is my solution to resize iframe dynamically, meaning using this workaround, you can leave it for Colorbox to calculate iframe’s width and height and resize it.


$(".colorbox").colorbox(

{iframe:true,

innerWidth:0,

innerHeight:0,

scrolling:false,

onComplete:function(){

$.colorbox.resize(

{

innerHeight:($('iframe').offset().top + $('iframe').height()),

innerWidth:($('iframe').offset().left + $('iframe').width())

}

);

}

}

);

What we did here is firstly we create Colorbox with 0 width and 0 height, when it is loaded, we calculate the document size of the iframe content and resize it using Colorbox’s API

Get Adobe Flash playerPlugin by wpburn.com wordpress themes