Monday, March 31, 2014

How To Get Base URL With PHP?

You can easily get base URL with PHP with few steps. Following function can be useful when you need base URL for instance to show correct images, load stylesheets and scripts.
function home_base_url(){   

// first get http protocol if http or https

$base_url = (isset($_SERVER['HTTPS']) &&

$_SERVER['HTTPS']!='off') ? 'https://' : 'http://';

// get default website root directory

$tmpURL = dirname(__FILE__);

// when use dirname(__FILE__) will return value like this "C:\xampp\htdocs\my_website",

//convert value to http url use string replace, 

// replace any backslashes to slash in this case use chr value "92"

$tmpURL = str_replace(chr(92),'/',$tmpURL);

// now replace any same string in $tmpURL value to null or ''

// and will return value like /localhost/my_website/ or just /my_website/

$tmpURL = str_replace($_SERVER['DOCUMENT_ROOT'],'',$tmpURL);

// delete any slash character in first and last of value

$tmpURL = ltrim($tmpURL,'/');

$tmpURL = rtrim($tmpURL, '/');

// check again if we find any slash string in value then we can assume its local machine

    if (strpos($tmpURL,'/')){

// explode that value and take only first value

       $tmpURL = explode('/',$tmpURL);

       $tmpURL = $tmpURL[0];


// now last steps

// assign protocol in first value

   if ($tmpURL !== $_SERVER['HTTP_HOST'])

// if protocol its http then like this

      $base_url .= $_SERVER['HTTP_HOST'].'/'.$tmpURL.'/';


// else if protocol is https

      $base_url .= $tmpURL.'/';

// give return value

return $base_url; 


// and test it

echo home_base_url();

Output will be like this
local machine : http://localhost/my_website/ or https://myhost/my_website 

public : or
use home_base_url function at index.php of your website and define it and then you can use this function to load script, css and content via url like
echo '<script src="'.home_base_url().'js/script.js" type="text/javascript"></script>'."\n";
will create output like this :
<script src="" type="text/javascript"?></script>

Tuesday, March 25, 2014

Smooth Scrolling to Anchor with jQuery

The days of abruptly jumping to an anchor link within a page are over. There exists different solutions to scroll smoothly.

What Smooth Scrolling to Anchor means ?
You click on a link, and the page smoothly and gracefully scrolls down to a specific section of content. This transition is easy on the eyes, it’s appealing, and damnit – it’s just plain cool!

We’ll utilize jQuery’s built-in function scrollTop() to do our heavy lifting.
This example works if your HTML is set up like so:
// content goes here

<a name="top" />
<a href="#bottom">Link</a>

// more content

<a href="#top">Link</a>
<a name="bottom" />

// more content

$(function() {
  // scroll handler
  var scrollToAnchor = function( id ) {
    // grab the element to scroll to based on the name
    var elem = $("a[name='"+ id +"']");
    // if that didn't work, look for an element with our ID
    if ( typeof( elem.offset() ) === "undefined" ) {
      elem = $("#"+id);
    // if the destination element exists
    if ( typeof( elem.offset() ) !== "undefined" ) {
      // do the scroll
      $('html, body').animate({
        scrollTop: elem.offset().top
      }, 1000 );
  // bind to click event
  $("a").click(function( event ) {
    // only do this if it's an anchor link
    if ( $(this).attr("href").match(/^#/) ) {
      // prevent default propagation
      // scroll to the location
      var href = $(this).attr('href').replace('#', '');
      scrollToAnchor( href );
      // if we have pushState
      if ( history.pushState ) {
        history.pushState( null, null, '#'+href );
      // fallback to prevent jitter
      return false;

Just updated this again and added pushState in, so that it keeps track of the hash change in modern browsers. If a user clicks away after clicking your anchor link, it’ll take them back to the anchor position they were at instead of making them rescroll to that position.
Alternative 2
 <a href="#target" class="scrollMe">Go to </a>
 <div id="target">Lorem Ipsum...</div>
$('a[href*=#]:not([href=#])').click(function() {
     if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {

       var target = $(this.hash);
       target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
       if (target.length) {
           scrollTop: target.offset().top - 20
         }, 1000);
         return false;

Friday, March 14, 2014

How to Show/Hide Read More link With jQuery?

Sometimes you have long text and few space to show it, with jQuery and CSS is very simple to hide part of the text and create a "Read more" link.
So, this is the HTML, please note that the value of links DIV ID is replicated as CLASS in the links and in the extra text:

<div class="section">
<div id="lorem">
Lorem ipsum dolor sit amet
<a class="read lorem" href="">(Read more...)</a>
 <a class="read hide lorem" href="">(Hide...)</a>

<div class="more lorem">
Praesent commodo, tellus vehicula facilisis laoreet,enim tellus sagittis metus, vel posuere risus tellus vel quam. Praesent fermentum, risus nec sagittis volutpat

<div class="section">
<div id="nullam">
Nullam fringilla hendrerit nisi et varius.
<a class="read nullam" href="">(Read more...)</a>
 <a class="read hide nullam" href="">(Hide...)</a>

<div class="more nullam">
Mauris blandit ullamcorper purus, sit amet imperdiet tortor suscipit et. Nulla luctus magna eu dolor tempor pharetra. Sed egestas, massa id consectetur


With CSS we hide one of the links and the extra text.
<style type="text/css">
.more, .hide{ display:none;}
Then the jQuery!
$(".read").click(function (){
    var i=$(this).parent().attr("id"); //We get the ID in the parent DIV 
    $("a."+i).toggle();  //switch the link with toggle()

Source: You can view the demo @

Thursday, March 6, 2014

How To Send Email To Multiple Recipients With Attachments Using Codeigniter ?

Sending email is very simple in Codeigniter. The following piece of code will email with attachments to all recipients at once. It will prevent showing all recipients address in email, and will email it separately for each recipient.
//loading necessary helper classes

//setting path to attach files 
$path = set_realpath('assets/your_folder/');
$file_names = directory_map($path);

foreach ($list as $name => $address)
    //if you set the parameter to TRUE any attachments will be cleared as well  
    $this->email->subject('Here is your info '.$name);
    $this->email->message('Hi '.$name.' Here is the info you requested.'); 
    foreach($file_names as $file_name)


Here $list contains array of Recipient name and email ID. Make sure to use clear(TRUE) at the beginning of each iteration.
Example used above is taken from

Tuesday, March 4, 2014

How To Send Email With An Attachment In Codeigniter?

You need to use following two CodeIgniter's robust classes.
1. Email Library : CodeIgniter's robust Email Class library to make sending email very simple and easy.
2. Path Helper : The Path Helper file contains functions that permits you to work with file paths on the server.

Here is a basic example demonstrating how you might send email with attachment. Note: This example assumes you are sending the email from one of your controllers.

$this->email->from('', 'Your Name');

$this->email->subject('Email Test');
$this->email->message('Testing the email class.'); 

/* This function will return a server path without symbolic links or relative directory structures. */
$path = set_realpath('uploads/pdf/');
$this->email->attach($path . 'yourfile.pdf');  /* Enables you to send an attachment */


echo $this->email->print_debugger();

The above email sending example is used in CodeIgniter User Guide Version 2.1.4.

Firefox And IE Custom Select Dropwdown Problem Solved

This will make a custom dropdown works across all the browsers. So putting it all together the following should be sufficient.

/* IE FIX! */
select::-ms-expand {
    display: none;
/* Firefox FIX! */
select {
    outline : none;
    overflow : hidden;   
    appearance: none;
    -webkit-appearance: none;
    -moz-appearance: none;  
     text-indent : 0.01px;/* this is important! */
    text-overflow: ''; /* this is important! */