Wednesday, May 20, 2015

PHP: Page pagination in array

Code snippet:
$page = !empty( $_GET['page'] ) ? (int) $_GET['page'] : 1;
$total = count( $yourDataArray ); //total items in array
$limit = 10; //per page    
$totalPages = ceil( $total/ $limit ); //calculate total pages
$page = max($page, 1); //get 1 page when $_GET['page'] <= 0
$page = min($page, $totalPages); //get last page when $_GET['page'] > $totalPages
$offset = ($page - 1) * $limit;
if( $offset < 0 ) $offset = 0;

$yourDataArray = array_slice( $yourDataArray, $offset, $limit );

echo 'Pages: ';
for($i = 1; $i <= $totalPages; $i++){
 
 if($i == $page){
  echo '<b>' . $i . '</b> ';
 }elseif($i != $page){
  echo '<a href="testpage.php?page=' . $i . '">' . $i . '</a> ';
 }
 
}
Output:
Pages: 1 2 3 4 

Refer: http://stackoverflow.com/questions/26451362/how-to-add-php-pagination-in-arrays

Tuesday, May 19, 2015

PHP: Group array to unique value

Existing array:
Array
(
    [0] => Array
        (
            [id] => 12
            [name] => John
            [description] => this is a description.
        )

    [1] => Array
        (
            [id] => 57
            [name] => John
            [description] => test description.
        )

    [2] => Array
        (
            [id] => 85
            [name] => Amy
            [description] => testing 123.
        )

)
Apply this:
$result = array();

foreach ($arr as $data) {

        $name = $data['name'];
        if (isset($result[$name])) {
                $result[$name][] = $data;
        } else {
                $result[$name] = array($data);
        }

}
Output:
Array
(
        [John] => Array
        (
                [0] => Array
                (
                        [id] => 12
                        [name] => John
                        [description] => this is a description.
                )

                [1] => Array
                (
                        [id] => 57
                        [name] => John
                        [description] => test description.
                )
        )
        [Amy] => Array
        (
                [0] => Array
                (
                        [id] => 85
                        [name] => Amy
                        [description] => testing 123.
                )
        )
)

Refer: http://stackoverflow.com/questions/12706359/php-array-group

Tuesday, March 4, 2014

PHP: Remove duplicate value in array

Basic array:

<?php
$input = array("a" => "green", "red", "b" => "green", "blue", "red");
$result = array_unique($input);
print_r($result);
?>

Output:
Array
(
    [a] => green
    [0] => red
    [1] => blue
)
Refer site: http://sg3.php.net/array_unique

Multi-dimensional array:
<?php
$date = array(
array('month' => 'Jan', 'year' => '2010'),
array('month' => 'Jan', 'year' => '2010'),
array('month' => 'Jan', 'year' => '2010'),
array('month' => 'Feb', 'year' => '2010'),
array('month' => 'Feb', 'year' => '2010'),
array('month' => 'Mar', 'year' => '2010'),
array('month' => 'Apr', 'year' => '2010'),
array('month' => 'Apr', 'year' => '2010')
);

print_r(super_unique($date,'month'));

function super_unique($array,$key) {

   $temp_array = array();

   foreach ($array as &$v) {

       if (!isset($temp_array[$v[$key]]))

       $temp_array[$v[$key]] =& $v;

   }

   $array = array_values($temp_array);

   return $array;
}
?>
Output:
Array
(
    [0] => Array
        (
            [month] => Jan
            [year] => 2010
        )

    [1] => Array
        (
            [month] => Feb
            [year] => 2010
        )

    [2] => Array
        (
            [month] => Mar
            [year] => 2010
        )

    [3] => Array
        (
            [month] => Apr
            [year] => 2010
        )

)
Refer site: http://stackoverflow.com/questions/307674/how-to-remove-duplicate-values-from-a-multi-dimensional-array-in-php

Wednesday, February 19, 2014

Web Tools: Find Facebook ID

For integrations of certain Facebook social plugins, like the "Like" button and "Like box", and others, Facebook requires that you know your Facebook numeric user ID.



Refer site: http://findmyfacebookid.com/

Sunday, February 9, 2014

Modernizr plugin: Progress button

Modernizr is a JavaScript library that detects HTML5 and CSS3 features in the user's browser. (Browser doesn't support: IE8 and below.)



Refer site: http://tympanus.net/Development/ProgressButtonStyles/