Sunday, 22 May 2016

Extract zip file using PHP script

Following is PHP code to extract zip file on current position. 

<?php

$file = 'zip_filename.zip';
$path = './zip/';

$zip = new ZipArchive;
if ($zip->open($file) === TRUE) {
    $zip->extractTo($path);
    $zip->close();
    echo 'ok';
} else {
    echo 'failed';
}

?>

Sunday, 15 May 2016

CSS : Select element based on multiple classes

I want to apply my style rule only if the li has both .class1 and .class2 classes applied.
<li class='class1 class2'>
 

Solution  : 
.class1.class2 {
    /* style here */
}

CSS How :nth-child() Works

The :nth-child selector allows you to select one or more elements based on their source order, according to a formula. It is defined in the CSS Selectors Level 3 spec as a “structural pseudo-class”, meaning it is used to style content based on its relationship with parent and sibling elements.

Suppose we are building a CSS grid, and want to remove the margin on every fourth grid module:

<section class="grid">
  <article class="module">One</article>
  <article class="module">Two</article>
  <article class="module">Three</article>
  <article class="module">Four</article>
  <article class="module">Five</article>
</section>


Rather than adding a class to every fourth item (e.g. .last), we can use :nth-child:

.module:nth-child(4n) {
  margin-right: 0;
}


As you can see, :nth-child takes an argument: this can be a single integer, the keywords “even” or “odd”, or a formula. If an integer is specified only one element is selected—but the keywords or a formula will iterate through all the children of the parent element and select matching elements — similar to navigating items in a JavaScript array. Keywords “even” and “odd” are straightforward (2, 4, 6 etc or 1, 3, 5 respectively). The formula is constructed using the syntax an+b, where:

    “a” is an integer value
    “n” is the literal letter “n”
    “+” is an operator and may be either “+” or “-”
    “b” is an integer and is required if an operator is included in the formula


What the above CSS does, is select every third list item inside unordered lists. That is, the 3rd, 6th, 9th, 12th, etc.

To change background color

p:nth-child(2) {
    background: #ff0000;
}

It boils down to what is in between those parentheses. nth-child accepts two keywords in that spot: even and odd. Those should be pretty obvious. "Even" selects even numbered elements, like the 2nd, 4th, 6th, etc. "Odd" selects odd numbered elements, like 1st, 3rd, 5th, etc.

p:nth-child(odd) {
    background: #ff0000;
}

p:nth-child(even) {
    background: #0000ff;
}


As seen in the first example, nth-child also accepts expressions in between those parentheses. The simplest possible expression? Just a number. If you put simply a number in the parentheses, it will match only that number element. For example, here is how to select only the 5th element:

ul li:nth-child(5) {
  color: #ccc;
}

Let's get back to the "3n+3" from the original example though. How does that work? Why does it select every third element? The trick is understanding the "n" and algebraic expression that represents. Think of "n" as starting at zero and then a set of all positive integers. Then complete the expression. So the 3n is "3xn", and the whole expression together is "(3xn)+3". Now substituting in the zero and positive integers, we get:

(3 x 0) + 3 = 3 = 3rd Element
(3 x 1) + 3 = 6 = 6th Element
(3 x 2) + 3 = 9 = 9th Element
etc.


How about the :nth-child(2n+1)?

(2 x 0) + 1 = 1 = 1st Element
(2 x 1) + 1 = 3 = 3rd Element
(2 x 2) + 1 = 5 = 5th Element
etc.



Hey wait! That's the same as "odd", so probably don't need to use that one very often. But wait now. Haven't we exposed our original example as being overly complicated? What if instead of "3n+3", we used "3n+0", or even simpler "3n".

(3 x 0) = 0 = no match
(3 x 1) = 3 = 3rd Element
(3 x 2) = 6 = 6th Element
(3 x 3) = 9 = 9th Element
etc.


So as you can see, the matches are exactly the same, no need for the "+3". We can use negative n values, as well as use subtraction in the expressions. For example, 4n-1:

(4 x 0) - 1 = -1 = no match
(4 x 1) - 1 = 3 = 3rd Element
(4 x 2) - 1 = 7 = 7th Element
etc.


Using "-n" values seems a little weird, because if the end result is negative there is no match, so you'll need to add to the expression to get it back positive again. As it turns out, this is a rather clever technique. You can use it to select the "first n elements" with "-n+3":

-0 + 3 = 3 = 3rd Element
-1 + 3 = 2 = 2nd Element
-2 + 3 = 1 = 1st Element
-3 + 3 = 0 = no match
etc.

Fix HTML characters encoding issues In WordPress

If you have switched web host, or changed the database for your WordPress blog, you might encounter an issue where foreign characters are not showing up properly in your site. Instead of the foreign characters, you will see plenty of question marks (???) in its place.  Well let's see how to fix them.

There can be a few causes that lead to displaying the wrong characters. First of all, you need to make sure that you are using the same character set in your wp-config.php file. Look for the following line and make sure it is not commented out:
define('DB_CHARSET', 'utf8');


Now, you need to check if your theme is using the character set that is set in wp-config.php file. Look for the following line in your HTML:
<meta charset="UTF-8" />
And lastly, you need to check that your database content is stored with the same character set encoding that you are using in your wp-config.php file and HTML. If you are using phpMyAdmin to manage your database, you will be able to see the current server collation in General Settings.

Changing the character set of complete WordPress database is not an easy task and should be done with a lot of care. Here are the steps to convert a database to your character set of choice.
1. First of all, we need to check the current character set of the database. Here’s the SQL query for checking:
SHOW VARIABLES LIKE "character_set_database";
2. If the character set is not what you want, you should take a backup of your database before you proceed further.
3. Run the following SQL query to change the characater set of complete database:
ALTER DATABASE MyDb CHARACTER SET utf8;
Change “MyDB” to the name of your database.
4. Now convert the “wp-posts” table to the character encoding you want:
ALTER TABLE wp_posts CHARACTER SET utf8;
Change the “wp_” if you are using a different prefix.
These steps will remove the question marks or other weird characters and will show proper foreign characters instead. If you have many columns in the WordPress database that are set to custom character set, you will need to change the character set of each column one by one.





Friday, 13 May 2016

jQuery click events not working on iPhone & iPad?

There is an issue with iOS not registering click/touch events bound to elements added after DOM loads. I found out about this problem when I had such a panel that I wanted to be entirely clickable.

Following are solutions
1) Using css style : I've found this the easy fix, simply add this to the css
<style>
    .clickable-div 
    {
         cursor: pointer;
    }
</style>
 
 
2) Using jQuery : use touchevent for exemple:
 
$('.clickable-div').on('click touchstart',function(){
    //your code
}); 
 

3) Haven't tested this fully but since iOS fires touch events, this could work, assuming you are in a jQuery setting.

$('a').on('click touchend', function(e) {
    var el = $(this);
    var link = el.attr('href');
    window.location = link;
});
 
The idea is that Mobile WebKit fires a touchend event at the end of a tap so we listen for that and then redirect the browser as soon as a touchend event has been fired on a link.

Thursday, 12 May 2016

Redirect HTTP to HTTPS

If you have a secure certificate (SSL) on your website, you can automatically redirect visitors to the secured (HTTPS) version of your website to make sure their information is protected.

Linux & cPanel

Linux-based accounts use .htaccess files to handle redirection.
Using the following code in your .htaccess file automatically redirects visitors to the HTTPS version of your site:


RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]


If you have an existing .htaccess file:
  • Do not duplicate RewriteEngine On.
  • Make sure the lines beginning RewriteCond and RewriteRule immediately follow the already-existing RewriteEngine On.

Windows & Plesk

Windows-based accounts use web.config files to handle redirection. 
Using the following code in your web.config file automatically redirects visitors to the HTTPS version of your site:

<configuration> 
<system.webServer> 
<rewrite>
 <rules>
            <rule name="HTTP to HTTPS redirect" stopProcessing="true"> 
            <match url="(.*)"></match> 
            <conditions>
                   <add input="{HTTPS}" pattern="off" ignoreCase="true"></add> 
             </conditions>
             <action type="Redirect" redirectType="Permanent" url="https://{HTTP_HOST}/{R:1}"></action> 
            </rule>
            </rules>
 </rewrite>
 </system.webServer> 
</configuration>



For WordPress

1. Go to the admin dashboard.
2. Point you mouse over Settings and click General.
3. Where it says WordPress Address (URL) and Site Address (URL) replace the http:// part with https:// for both of them.
4. Click Save Changes

Wednesday, 11 May 2016

jQuery : Scroll to URL HASH Tag / ID location on page when dynamic page height

www.example.com/index.html#foo

IF you want to smooth scroll using  #foo
It will not work directly when your page adding dynamic content and increasing page height.

Solution :
1)  On document ready find hash tag value in URL
2) Find whether it's actually exists in page or not
3) If you found that hash tag id anywhere within page then scroll to that section.

Following is jquery code for that.

<script type="text/javascript">
    $( document ).ready(function() {

        var hashnew = document.URL.substr(document.URL.indexOf('#')+1)
              
        if ($("#"+hashnew).length) {
               
                 $('html, body').animate({
                       scrollTop: $("#"+hashnew).offset().top
                 }, 2000);
   
        }
   
    });
</script>

Difference between break and continue in PHP?

What is the difference between break and continue in PHP?

1) break ends a loop completely.
2) continue just shortcuts the current iteration and moves on to the next iteration.







while ($condition_true) {   <--------------------┐
               continue;    --- goes back here --┘
               break;       ----- jumps here ----┐
}                                                |
       <-----------------------------------------┘

 

WordPress : Cannot modify header information - headers already sent by wp-includes/pluggable.php

Warning: Cannot modify header information - headers already sent by wp-includes/pluggable.php on line 



Solution : Remove BLANK SPACE from end of function.php in wordpress theme folder.





Indent code in Dreamweaver

Indent code in Dreamweaver

I've inherited some code which has loads of randomly placed spaces and line breaks. With Dreamweaver it is possible to Indent code.


PHP : Fetch YouTube video id and image path from video url

<form method="post" action="" >

    <div class="divcls">
        <div align="center" style="vertical-align:middle;">
            <input type="text" class="txtcls" value="<?=$_REQUEST['url']?>" name="url" id="url" placeholder="Enter Search URL here...." />
            <button title="Submit" class="styled-button button-01" type="submit">Submit</button>
        </div>
    </div>
</form>

<div class="divans">
<?php

if(isset($_REQUEST['url']) && $_REQUEST['url'] != "")
{
    echo $video_details = $_REQUEST['url'];
   
    $feat_image = "";
    if (preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $video_details, $match)){
        $video_id = $match[1];
}
     
    if($video_id != ""){
    ?>
      <br/><br/><hr/><br/>
     
         <a href="http://img.youtube.com/vi/<?php echo $video_id;?>/0.jpg" target="_blank" >
              http://img.youtube.com/vi/<?php echo $video_id;?>/0.jpg</a>
      </a>   
      <br/><br/><hr/><br/>
     
         <a href="http://img.youtube.com/vi/<?php echo $video_id;?>/1.jpg" target="_blank" >
      http://img.youtube.com/vi/<?php echo $video_id;?>/1.jpg
      </a>
         <br/><br/><hr/><br/>
     
         <a href="http://img.youtube.com/vi/<?php echo $video_id;?>/2.jpg" target="_blank" >
        http://img.youtube.com/vi/<?php echo $video_id;?>/2.jpg
         </a>
     
      <br/><br/><hr/><br/>
      <a href="http://img.youtube.com/vi/<?php echo $video_id;?>/3.jpg" target="_blank" >
         http://img.youtube.com/vi/<?php echo $video_id;?>/3.jpg
      </a>
    <?   
    }
   
}
?>
</div>

How to remove blank lines in Dreamweaver

I was recently working on a website in Dreamweaver and had both PHP and CSS files with loads of blank lines of code, which needed to be deleted in order to make the files smaller.

So instead of doing it manually one at a time… which would have taken ages. I used a nice little tip in Dreamweaver.


1. Open the document you need to change.
2. Click CMD + F (Mac) or CTRL + F (PC).
3. In the Find and Replace window change the following:
Find in: to Current document
Search: to Source code
Find: input [\r\n]{2,}
Replace: input \n
Options: tick Use regular expression
4. Finally select Replace All.

PHP + LINUX : Change files and folders permission

Note : Put this is directory where you want to change permission for all files and folders.
This will work for sub directories also.


<?php
exec("chmod 755 -R pms",$r,$o);
?>

Bootstrap 3 - How to center the modal window

Note : Put code after bootstrap.min.js

<script>
$(function() {
    function reposition() {
        var modal = $(this),
            dialog = modal.find('.modal-dialog');
        modal.css('display', 'block');
       
        // Dividing by two centers the modal exactly, but dividing by three
        // or four works better for larger screens.
        dialog.css("margin-top", Math.max(0, ($(window).height() - dialog.height()) / 2));
    }
    // Reposition when a modal is shown
    $('.modal').on('show.bs.modal', reposition);
    // Reposition when the window is resized
    $(window).on('resize', function() {
        $('.modal:visible').each(reposition);
    });
});
</script>