Find the dates ( month start and end dates) between the given two dates using PHP

When you want to pull the dates between two dates, where the between dates are connecting dots. If you want to list the complete date ranges through months. For example, to find the dates between below dates

date1 = '2022-12-29';
date2 = '2023-02-20';

expected output would be 


Start DateEnd Date
2022-12-292022-12-31
2023-01-012023-01-31
2023-02-012023-02-20
 

<?php

/*
 * Returns array of month's start & end dates between provided dates.
 *
 * @param string $start_date
 * @param string $end_date
 * 
 *
 * @return array
 */
 function getDates($start_date, $end_date){
    $start_date_obj = new DateTime($start_date);
    $end_date_obj = new DateTime($end_date);
    
    $diff = $end_date_obj->diff($start_date_obj);
    $months = (($diff->y) * 12) + ($diff->m);
    
    $dates[] = array('start_date' => $start_date, 'end_date' => date('Y-m-t', strtotime($start_date)), 'suffix' => date('Y-m', strtotime($start_date)));
    $new_start_date = strtotime(date('Y-m-01', strtotime($start_date)));
    for($i = 1; $i <= $months; $i++){ 
        $new_start_date = strtotime("+1 months", $new_start_date);
        $dates[] = array('start_date' => date('Y-m-01', $new_start_date), 'end_date' => date('Y-m-t', $new_start_date) , 'suffix' => date('Y-m', $new_start_date));
    }
    $dates[] = array('start_date' => date('Y-m-01', strtotime($end_date)), 'end_date' => $end_date, 'suffix' => date('Y-m', strtotime($end_date)));
    
    return $dates;
}

$start_date = '2022-12-29';
$end_date = '2023-02-20';
$dates = getDates($start_date, $end_date);
print_r($dates);
?>



Output:
Array
(
    [0] => Array
        (
            [start_date] => 2022-12-29
            [end_date] => 2022-12-31
            [Ym] => 2022-12
        )

    [1] => Array
        (
            [start_date] => 2023-01-01
            [end_date] => 2023-01-31
            [Ym] => 2023-01
        )

    [2] => Array
        (
            [start_date] => 2023-02-01
            [end_date] => 2023-02-20
            [Ym] => 2023-02
        )

)

Sorting an array of objects using Javascript

 When working on one of my projects, I had to sort the elements of the JSON array of objects. I did this most of the time from server side but this time I had to do the client side sort.

Here is the solution what I found.

We've two functions one to sort by ascending and other for descending. It expects first parameter as array (of objects) and second parameter the key on basis of which we want to sort.

 
<script>
function sortByKeyDesc(array, key) {
	return array.sort(function (a, b) {
    	var x = a[key]; var y = b[key];
        return ((x > y) ? -1 : ((x < y) ? 1 : 0));
	});
}

function sortByKeyAsc(array, key) {
	return array.sort(function (a, b) {
    	var x = a[key]; var y = b[key];
        return ((x < y) ? -1 : ((x > y) ? 1 : 0));
	});
}
</script>
Here is the example

<script>
books = [{
  'BookID': 123,
  'BookName': 'Seven Habits of most effective people'
}, {
  'BookID': 234,
  'BookName': 'Why Leaders eat last'
}, {
  'BookID': 345,
  'BookName': 'How to influence people'
}, {
  'BookID': 456,
  'BookName': 'The Tipping point'
}];

sorted_books = sortByKeyAsc(books, 'BookName');
console.log(sorted_books);
</script>
Result will be

[{
  BookID: 345,
  BookName: "How to influence people"
}, {
  BookID: 123,
  BookName: "Seven Habits of most effective people"
}, {
  BookID: 456,
  BookName: "The Tipping point"
}, {
  BookID: 234,
  BookName: "Why Leaders eat last"
}]

Multiple Checkboxes Validation

This is a regular problem and solution is hardly available on google. When we use multiple checkboxes of same group (same name with square brackets) and you've to read the values from server side, then below solution will help you. Here I'm using jQuery (https://jquery.com/) and jQuery Validate plugin (https://jqueryvalidation.org/)

For an example, I've to ask a user which of the listed book they are interested to know about


 
 <form id="BooksForm" method="post" name="BooksForm">
 <p>Books you are interested in </p>
    <input class="Books" id="Book1" name="Books[]" type="checkbox" value="1" /> The Inner Game of Tennis <br />
    <input class="Books" id="Book2" name="Books[]" type="checkbox" value="1" /> Monk who sold his ferrari <br />
    <input class="Books" id="Book3" name="Books[]" type="checkbox" value="1" /> Leading Without Authority <br />
    <input class="Books" id="Book4" name="Books[]" type="checkbox" value="1" /> Life's Amazing Secrets <br />
    <label class='error' for='Books[]'></label>
    <input id="Submit" name="Submit" type="submit" value="Submit" />
 </form>
 
 <script crossorigin="anonymous" integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
 <script crossorigin="anonymous" src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.2/jquery.validate.js"></script>
 
 <script>
    $.validator.addMethod('Books', function (value, element) {
      return value > 0;
    }, 'Please select at least one checkbox');

	$('#BooksForm').validate({
      ignore: [],
      submitHandler: function(){
        alert($('.Books').val());
        return false;
      }
   });
</script>

PHP Code Review Guidelines

General
  •  The code works
  •  The code is easy to understand
  •  Follows coding conventions
  •  Names are simple and if possible short
  •  Names are spelt correctly
  •  Names contain units where applicable
  •  There are no usages of magic numbers
  •  No hard coded constants that could possibly change in the future
  •  All variables are in the smallest scope possible
  •  There is no commented out code
  •  There is no dead code (inaccessible at Runtime)
  •  No code that can be replaced with library functions
  •  Variables are not accidentally used with null values
  •  Variables are immutable where possible
  •  Code is not repeated or duplicated
  •  There is an else block for every if clause even if it is empty
  •  No complex/long boolean expressions
  •  No negatively named boolean variables
  •  No empty blocks of code
  •  Ideal data structures are used
  •  Constructors do not accept null/none values
  •  Catch clauses are fine grained and catch specific exceptions
  •  Exceptions are not eaten if caught, unless explicitly documented otherwise
  •  Files/Sockets and other resources are properly closed even when an exception occurs in using them
  •  null is not returned from any method
  •  == operator and === (and its inverse !==) are not mixed up
  •  Floating point numbers are not compared for equality
  •  Loops have a set length and correct termination conditions
  •  Blocks of code inside loops are as small as possible
  •  No methods with boolean parameters
  •  No object exists longer than necessary
  •  No memory leaks
  •  Code is unit testable
  •  Test cases are written wherever possible
  •  Methods return early without compromising code readability
  •  Performance is considered
  •  Loop iteration and off by one are taken care of

Architecture

  •  Design patterns if used are correctly applied
  •  Law of Demeter is not violated
  •  A class should have only a single responsibility (i.e. only one potential change in the software's specification should be able to affect the specification of the class)
  •  Classes, modules, functions, etc. should be open for extension, but closed for modification
  •  Objects in a program should be replaceable with instances of their subtypes without altering the correctness of that program
  •  Many client-specific interfaces are better than one general-purpose interface.
  •  Depend upon Abstractions. Do not depend upon concretions.

API

  •  APIs and other public contracts check input values and fail fast
  •  API checks for correct oauth scope / user permissions
  •  Any API change should be reflected in the API documentation
  •  APIs return correct status codes in responses

Logging

  •  Logging should be easily discoverable
  •  Required logs are present
  •  Frivolous logs are absent
  •  Debugging code is absent
  •  No print_rvar_dump or similar calls exist
  •  No stack traces are printed

Documentation

  •  Comments should indicate WHY rather that WHAT the code is doing
  •  All methods are commented in clear language.
  •  Comments exist and describe rationale or reasons for decisions in code
  •  All public methods/interfaces/contracts are commented describing usage
  •  All edge cases are described in comments
  •  All unusual behavior or edge case handling is commented
  •  Data structures and units of measurement are explained

Security

  •  All data inputs are checked (for the correct type, length/size, format, and range)
  •  Invalid parameter values handled such that exceptions are not thrown
  •  No sensitive information is logged or visible in a stacktrace

Week dates between two dates

To list week's start date & end date between given two dates. It also includes number of dates in every set. It allows you to list only those weeks having total seven days. Here the starting day of the week is Monday & the end day of the week is Sunday.
/*
     * Returns array of week's start & end dates with number of days between those.
     *
     * @param string $start_date
     * @param string $end_date
     * @param boolean $only_full_week
     *
     * @return array
     */
    function getWeekDates($start_date, $end_date, $only_full_week = false)
    {
        $stime = strtotime($start_date);
        $etime = strtotime($end_date);

        $weeks = array();

        $i = 0;
        $j = 1;
        while ($stime <= $etime) {
            if ($i == 0 && $j == 1) {
                $weeks[$i]['start_date'] = date('Y-m-d', $stime);
                $weeks[$i]['end_date'] = date('Y-m-d', $stime);
                $weeks[$i]['count'] = $j;
            } else if (date('N', $stime) == 1) {
                $j = 1;
                $weeks[$i]['start_date'] = date('Y-m-d', $stime);
                $weeks[$i]['end_date'] = date('Y-m-d', $stime);
                $weeks[$i]['count'] = $j;
            }

            if (date('N', $stime) == 7) {
                $weeks[$i]['end_date'] = date('Y-m-d', $stime);
                $weeks[$i]['count'] = $j;

                $i ++;
            }
            $j ++;
            $stime = strtotime('+1 day', $stime);
        }
        if ($only_full_week) {
            foreach ($weeks as $key => $week) {
                if ($week['count'] != 7) {
                    unset($weeks[$key]);
                }
            }
        }

        return array_values($weeks);
    }

Here is the example:

$start_date = '2018-12-23';
$end_date = '2019-01-08';
print_r(getWeekDates($start_date, $end_date));
Output:
Array
(
    [0] => Array
        (
            [start_date] => 2018-12-23
            [end_date] => 2018-12-23
            [count] => 1
        )

    [1] => Array
        (
            [start_date] => 2018-12-24
            [end_date] => 2018-12-30
            [count] => 7
        )

    [2] => Array
        (
            [start_date] => 2018-12-31
            [end_date] => 2019-01-06
            [count] => 7
        )

    [3] => Array
        (
            [start_date] => 2019-01-07
            [end_date] => 2019-01-07
            [count] => 1
        )

)
If we need only full weeks, include the third parameter as 'true'

$start_date = '2018-12-23';
$end_date = '2019-01-08';
print_r(getWeekDates($start_date, $end_date, true));
Output:
Array
(
    [0] => Array
        (
            [start_date] => 2018-12-24
            [end_date] => 2018-12-30
            [count] => 7
        )

    [1] => Array
        (
            [start_date] => 2018-12-31
            [end_date] => 2019-01-06
            [count] => 7
        )

)

Format Sentences, format number and remove extra spaces. All in one solution

We had a requirement, where we were asked to format the content from a big database table. And the format includes making sentences to sentence case, format simple numbers to comma separated numbers within those sentences and remove the extra spaces. Here is the solution. Hope this helps you.
function formatString($string){
 //number formatting
 $string1 = preg_replace_callback('/\d+/', function($match){return number_format($match[0]);}, $string);
 
 //removing extra spaces
 $string2 = preg_replace('/\s+/', ' ', $string1);
 
 //sentence case
 $sentences = preg_split('/([.?!]+)/', $string2, -1, PREG_SPLIT_NO_EMPTY|PREG_SPLIT_DELIM_CAPTURE);
    $new_string = '';
    foreach ($sentences as $key => $sentence) {
        $new_string .= ($key & 1) == 0?
            ucfirst(strtolower(trim($sentence))) :
            $sentence.' ';
    }
    return trim($new_string);
}

$str = "it was an awesome               day. i bought shares for 23000      and sold at 27000 in just 2        hours. i had a profit of 4000 INR. isn't it great?";
echo formatString($str);

Huge data Excel download using PHP

Ah! It was always a problem to download huge data in excel format. Tried the best library available 'PHPExcel' but the issue persists with memory limit and time taken to generate and download.

But here is the promising solution found after spending ages searching on Google. You can find it here. The author himself says "Never run out of memory with PHPExcel again".

You need to download it and include xlsxwriter.class.php where you want to fetch the data from DB and through it on to browser or save to a particular location.

Let's get started.
Below is the logic to generate the file and store in the current directory. If you want to store is at the desired location, specify the location for the method writeToFile.

include_once("xlsxwriter.class.php");
$writer = new XLSXWriter();
$data = array(
    array('year','month','amount'),
    array('2003','1','220'),
    array('2003','2','153.5'),
);

$writer = new XLSXWriter();
$writer->writeSheet($data);
$writer->writeToFile('output.xlsx');
In case if you want to download it on the fly use the below logic.
include_once("xlsxwriter.class.php");
$writer = new XLSXWriter();
$data = array(
    array('year','month','amount'),
    array('2003','1','220'),
    array('2003','2','153.5'),
);
$filename = 'output.xlsx';

$writer = new XLSXWriter();
$writer->writeSheet($data);

header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="' . $filename . '"');
header('Cache-Control: max-age=0');

$writer->writeToStdOut();
Through this library, you can even set the column type. Please visit the official URL for more information.

WYSIWYG super light editor with jQuery & Bootstrap

Yes! It is. Summernote is the super light WYSIWYG editor for the web. It supports most of the features tinyMCE or CKEditor does but with smallest Javascript & CSS files.

You can download Summernote from here. It is the official website for the same. It has very simple getting started tutorial. 

This is article will show you how to start with summernote and couple of fixes or plugin which you don't find on the official website.

Getting started:
If you want to download and start with, click here.
For CDN, place below snippet under head tag.


<!-- include libraries(jQuery, bootstrap) -->
<link href="http://netdna.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.css" rel="stylesheet">
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.js"></script> 
<script src="http://netdna.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.js"></script> 

<!-- include summernote css/js -->
<link href="http://cdnjs.cloudflare.com/ajax/libs/summernote/0.8.9/summernote.css" rel="stylesheet">
<script src="http://cdnjs.cloudflare.com/ajax/libs/summernote/0.8.9/summernote.js"></script>
  
Now create a textarea and attach summernote to it.

<textarea id="Description" name="Description"></textarea>

$(document).ready(function(){
   $('#Description).summernote();
});

When you don't want to show/use all the buttons in the toolbar, then you can choose/define like below
$('#Description').summernote({toolbar: [
       // [groupName, [list of button]]
       ['style', ['bold', 'italic', 'underline', 'clear']],
       ['font', ['strikethrough', 'superscript', 'subscript']],
       ['fontsize', ['fontsize']],
       ['para', ['ul', 'ol', 'paragraph']],
       ['insert', ['picture', 'link', 'video', 'table', 'hr']]
     ]});

That's it! But, if you are using summernote in Bootstrap modal, then there is a small problem. Whenever you click on Image, Video or link buttons another modal opens up with the options and while closing the second modal it will close all the modals. Quick fix: You can add this line of code as option to summernote.
$('#Description').summernote({
  dialogsInBody: true,
  toolbar: [ 
    // toolbar code if needed
  ]
});

Summernote inserts base64 code (image data URL) for any image inserted. If you don't want to store image data URL in the text (as it will hold huge data), then you can use below code:
$('#Description').summernote({
  dialogsInBody: true,
  toolbar: [ 
    // toolbar code if needed
  ],
  callbacks: {
      onImageUpload: function(image) {
          uploadImage(image[0]);
      }
  }
});

function uploadImage(image) {
    var data = new FormData();
    data.append("file", image);
    $.ajax({
        url: SITE_URL + "admin/upload-image",
        cache: false,
        contentType: false,
        processData: false,
        data: data,
        type: "post",
        success: function(url) {
            var image = $('').attr('src', url);
            $('#Descrpition').summernote("insertNode", image[0]);
        },
        error: function(data) {
            //console.log(data);
        }
    });
}

PHP code to upload an image to any directory and return URL for the image:

if (isset($_FILES['file']['name'])) {
    if (! $_FILES['file']['error']) {
        $name = md5(rand(100, 200));
        $ext = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
        $filename = $name . '.' . $ext;
        $destination = $this->getMediaPath() . 'images/' . $filename;
        $location = $_FILES["file"]["tmp_name"];
        move_uploaded_file($location, $destination);
        echo 'media/images/' . $filename;
    } else {
        echo "Ooops!  Your upload triggered the following error:  " . $_FILES['file']['error'];
    }
}            

That's it...

Generate documentation for PHP Code using phpDocumentor

What is phpDocumentor?
phpDocumentor is the simple and best tool to generate API documentation for PHP source code. 


Downloading & installing phpDocumentor:
There are several dependencies to install phpDocumentor you can find them on official website i.e., here

To install phpDocumentor using PEAR type below command
  • In windows, run command prompt as administrator. Goto xampp/php folder and type
    pear channel-discover pear.phpdoc.org
    pear install phpdoc/phpDocumentor
  • In linux, open terminal and type (if install xampp for linux then goto xampp/bin folder)
    pear channel-discover pear.phpdoc.org
    pear install phpdoc/phpDocumentor
Generate API using phpDocumentor:
  • In windows, goto  phpdoc.bat folder and run phpdoc  command for source directory to destination
     phpdoc -d <source directory of php code> -t <destination directory>
  • In Linux, goto [phpdocumentor installation folder]/bin/ and run command
     phpdoc -d <source directory of php> -t <destination directory>
Note:
If in case, installation method through PEAR gives you below error. Try downloading phar and proceed
Fatal error: Uncaught TypeError: Argument 1 passed to Monolog\ErrorHandler::handleException() must be an instance of Exception...

Download .phar from here
To generate API run the below command from php directory with the location of .phar file
php phpDocumentor.phar -d <source directory of php code> -t <destination directory>

If you find any error related to Graphviz, download and install software from here. Set the environment variable for Graphviz/bin directory for 'dot' executable file.

Google like Pagination

In the beginning of my career, every technical interview has this question; "How do you write pagination script in PHP?". I think you too faced the same issue. It was very important to answer this because every application is developed to list set of information and off-course you can't show every thing in a single page. It will irritate the user to scroll to the bottom of page to read.

So, the pagination. Divide the list of items in number of pages and navigate to each by clicking on each page number displayed. At the beginning, the pagination was simple was limited to 10-15 pages, but after some time in real time the requirement has grown to 10 multiple pages. Solution was to show first few page numbers, last few page numbers and in middle show the ellipsis(...). As you navigate to any page it would show definite page numbers to both the sides.


/**
 * Pagination file
 * 
 * This file provides the google like pagination solution
 * 
 * @category Saral
 * @package Pager
 * @version  0.1
 * @since  0.1
 */

/**
 * Pager class
 *
 * Class is used generate google like pagination links
 *
 * @category Saral
 * @package Pager
 * @version Release: 0.1
 * @since 26.Nov.2013
 * @author Sailesh Jaiswal
 */
class Pager
{

    /**
     *
     * css class name for links container
     *
     * @var string
     */
    private $class_pagination = 'pagination';

    /**
     *
     * css class name for default style for links
     *
     * @var string
     */
    private $class_default = 'page';

    /**
     *
     * css class name for disabled style for links
     *
     * @var string
     */
    private $class_disabled = 'disabled';

    /**
     *
     * css class name for current/selected link style
     *
     * @var string
     */
    private $class_current = 'current';

    /**
     *
     * used to set the css classes for links
     *
     * @param string $pagination            
     * @param string $default            
     * @param string $disabled            
     * @param string $current            
     */
    function setStyles($pagination = '', $default = '', $disabled = '', $current = '')
    {
        if ($pagination != '') {
            $this->class_pagination = $pagination;
        }
        if ($default != '') {
            $this->class_default = $default;
        }
        if ($disabled != '') {
            $this->class_disabled = $disabled;
        }
        if ($current != '') {
            $this->class_current = $current;
        }
    }

    /**
     *
     * generates pagination links
     *
     * @param string $url   url of the page         
     * @param integer $current_page   current page         
     * @param integer $total_records   total records         
     * @param integer $rpp            records per page
     * @param integer $adjacents      how many pages to show both the side of the current page in case if there are plenty of pages      
     * @return string
     */
    function showLinks($url, $current_page, $total_records, $rpp = 5, $adjacents = 2)
    {
        $class_pagination = $this->class_pagination;
        $page = $this->class_default;
        $current = $this->class_current;
        $disabled = $this->class_disabled;
        
        $pagination = "";
        $prev = $current_page - 1; // previous page is page - 1
        $next = $current_page + 1; // next page is page + 1
        $lastpage = ceil($total_records / $rpp); // lastpage is = total pages / items per page, rounded up.
        $lpm1 = $lastpage - 1; // last page minus 1
        $prev = $url . '?page=' . $prev;
        if ($lastpage > 1) {
            $pagination .= "<div><ul class=\"$class_pagination\">";
            // previous button
            if ($current_page > 1)
                $pagination .= "<li><a href=\"$prev\" class=\"$page\"> << previous</a></li>";
            else
                $pagination .= "<li><span class=\"$disabled\"> << previous</span></li>";
            
            // pages
            if ($lastpage < 7 + ($adjacents * 2)) // not enough pages to bother breaking it up
            {
                for ($counter = 1; $counter <= $lastpage; $counter ++) {
                    $i = $url . '?page=' . $counter;
                    if ($counter == $current_page)
                        $pagination .= "<li class=\"$current\"><a>$counter</a></li>";
                    else
                        $pagination .= "<li><a href=\"$i\" class=\"$page\">$counter</a></li>";
                }
            } elseif ($lastpage > 5 + ($adjacents * 2)) // enough pages to hide some
            {
                // close to beginning; only hide later pages
                if ($current_page < 1 + ($adjacents * 2)) {
                    for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter ++) {
                        $i = $url . '?page=' . $counter;
                        if ($counter == $current_page)
                            $pagination .= "<li class=\"$current\"><a>$counter</a></li>";
                        else
                            $pagination .= "<li><a href=\"$i\" class=\"$page\">$counter</a></li>";
                    }
                    $lpm11 = $url . '?page=' . $lpm1;
                    $lastpage1 = $url . '?page=' . $lastpage;
                    $pagination .= "<li><a href='javascript: void(0)'>...</a></li>";
                    $pagination .= "<li><a href=\"$lpm11\" class=\"$page\">$lpm1</a></li>";
                    $pagination .= "<li><a href=\"$lastpage\" class=\"$page\">$lastpage</a></li>";
                } // in middle; hide some front and some back
                elseif ($lastpage - ($adjacents * 2) > $current_page && $current_page > ($adjacents * 2)) {
                    $pagination .= "<li><a href=\"$url?page=1\" class=\"$page\">1</a></li>";
                    $pagination .= "<li><a href=\"$url?page=2\" class=\"$page\">2</a></li>";
                    $pagination .= "<li><a href='javascript: void(0)'>...</a></li>";
                    for ($counter = $current_page - $adjacents; $counter <= $current_page + $adjacents; $counter ++) {
                        $i = $url . '?page=' . $counter;
                        if ($counter == $current_page)
                            $pagination .= "<li class=\"$current\"><a>$counter</a></li>";
                        else
                            $pagination .= "<li><a href=\"$i\" class=\"$page\">$counter</a></li>";
                    }
                    $lpm11 = $url . '?page=' . $lpm1;
                    $lastpage1 = $url . '?page=' . $lastpage;
                    $pagination .= "<li><a href='javascript: void(0)'>...</a></li>";
                    $pagination .= "<li><a href=\"$lpm11\" class=\"$page\">$lpm1</a></li>";
                    $pagination .= "<li><a href=\"$lastpage1\" class=\"$page\">$lastpage</a></li>";
                } // close to end; only hide early pages
                else {
                    $pagination .= "<li><a href=\"$url?page=1\" class=\"$page\">1</a></li>";
                    $pagination .= "<li><a href=\"$url?page=2\" class=\"$page\">2</a></li>";
                    $pagination .= "<li><a href='javascript: void(0)'>...</a></li>";
                    for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter ++) {
                        $i = $url . '?page=' . $counter;
                        if ($counter == $current_page)
                            $pagination .= "<li class=\"$current\"><a>$counter</a></li>";
                        else
                            $pagination .= "<li><a href=\"$i\" class=\"$page\">$counter</a></li>";
                    }
                }
            }
        
            // next button
            if ($current_page < $counter - 1) {
                $next = $url . '?page=' . $next;
                $pagination .= "<li><a href=\"$next\" class=\"$page\">next >></a></li>";
            } else {
                $pagination .= "<li><span class=\"$disabled\">next >></span></li>";
            }
            $pagination .= "</ul></div>\n";
        }
        return $pagination;
    }
}

$total_records = 100; //total records that you want to show as paginated
$page = $_GET['page'];
$pager = new Pager;
echo $pager->showLinks("http://example.com/posts.php", $page, $total_records, 10, 2);

Validation and Submission of form using jQuery :: Easiest Solution

In any application, form validation and submission to server is so common and redundant; there are multiple ways of doing it. And the best and easiest way that I do use or recommend is jQuery solution. 

Most popular jQuery plugins are the best solution, they have the best solution ever. 

One, jQuery Validator written and maintained by Jorn Zaefferer, a member of the jQuery team, lead developer on the jQuery UI team and maintainer of QUnit.

Two, jQuery Form by malsup, it has various plugins and solutions. Another plugin that I like from same developer is jQuery BlockUI. (give a try).


 
 
 
 
 
Name:
Email:
Note: To avoid unexpected behavior, maintain same form and fields names and id selector values. Need further explanation, let me know.

Email already exists using jQuery

It is the common requirement for any web application either it is PHP, .net, JSP etc., which has users involved. When creating/adding user from admin or end user registration we should check whether username or email address already exists. It will help to have unique registrations to our application and avoid confusion between various users. 

I see many developers struggle with this by submitting the form to server side script and validate their from database, in case username/email address already exists they take the pain to go back to previous page or form and show the error message with other form data filled. 

Few use AJAX to send the information onblur or onkeyup or onkeydown but face difficulties to stop form submission. Here is the simple solution that would help in validating username or email address existence without much hassle. 

Solution comes from popular jQuery Validator plugin I'm using this from ages. 

HTML Code:
<html>
    <head>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.16.0/jquery.validate.js"></script>
        <script>
              $(function(){
                    $('#UserForm').validate({
                           rules:{
                                   Name: {required: true},
                                   EmailID: { required: true, email: true, 
                                     remote: {
                                        url: "http://localhost/user/check-email",
                                        type: "post",
                                        data: {
                                              EmailID: function() {
                                                  return $( "#EmailID" ).val();
                                              },
                                              UserID: $('#UserID').val()
                                        }
                                     }
                                 }
                           }   
                    });
              });
        </script>
    </head>
    <body>
        <form action="" id="UserForm" method="post" name="UserForm">
            Name: <input id="Name" name="Name" type="text" />
            Email ID: <input id="EmailID" name="EmailID" type="text" />
            <input id="UserID" name="UserID" type="hidden" value="0" />
            <input id="Submit" name="Submit" type="submit" value="Submit" />
        </form>
</body>
</html>

 The remote block will do the trick, it sends the current field value to server side URL "http://localhost/user/check-email". This URL can be any file URL where it sends EmailID value using POST method and there you've to write SQL query to search in DB and if exists print 'false' otherwise 'true'. (Note: don't write return true or return false, just print true or false in quotes)

We can pass UserID as zero for new insertion for update UserID can be unsigned integer so that you can check if EmailID is same for other than selected UserID.