Showing posts with label array. Show all posts
Showing posts with label array. Show all posts

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

Monday, January 7, 2013

PHP: Remove intersect element(s) in array

$array1 = array('abc', 'efg', 'hij', 'klm', 'nop', 'qrs');
$array2 = array('tuv', 'abc', 'wxyz', 'qrs', 'hij');


// Find intersection
$intersection = array_intersect($array1, $array2); 

echo '<pre>'.print_r($intersection, true).'</pre>';
/* output: 
Array
(
    [0] => abc
    [2] => hij
    [5] => qrs
)
*/

// Extract
if(!empty($intersection)){

   foreach ($intersection as $key => $value) {
      unset($$array1[$key]);
   }

}

echo '<pre>'.print_r($array1, true).'</pre>';
/* output: 
Array
(
    [1] => efg
    [3] => klm
    [4] => nop
)
*/

Refer site:
Unset - http://sg2.php.net/manual/en/function.unset.php
Array intersect - http://php.net/manual/en/function.array-intersect.php

Wednesday, September 28, 2011

JavaScript: Create simple array using loop

Code:
<script type="text/javascript">
// same with --> var value = [];
var value = new Array(8); 

for(var i=0; i<=8; i++) {
 value[i] = i;
}

// check output 
console.log(value);

// print output
for(i=0; i<=8; i++) { 
   document.write(value[i] + ", "); 
}
</script>
Output:
0, 1, 2, 3, 4, 5, 6, 7, 8
Refer: http://www.hunlock.com/blogs/Mastering_Javascript_Arrays

Monday, September 5, 2011

JavaScript: Simple show hide content II

JavaScript 1
<script language="javascript">
function show_tab(id){
 var tabId = [1,2,3];
 var idx = tabId.indexOf(id); // Find index which in the array
 if(idx!=-1) tabId.splice(idx, 1); // Remove element from array
 
 // show content
 document.getElementById('tab' + id).style.display = 'block';
 
 // hide content
 for(var i = 0; i < tabId.length; i++){
  document.getElementById('tab' + tabId[i]).style.display = 'none';
 }
}
</script>
Or: JavaScript 2
<script language="javascript">
function show_tab(id){  
 for(var i=1; i<4; i++){
  if(i == id){
   // show content
   document.getElementById('tab'+i).style.display='block';
  }else {
   // hide content
   document.getElementById('tab'+i).style.display='none';
  }
 }
}
</script>
HTML
<a href="javascript:void(0)" onclick="show_tab(1)">Tab 1</a> |
<a href="javascript:void(0)" onclick="show_tab(2)">Tab 2</a> |
<a href="javascript:void(0)" onclick="show_tab(3)">Tab 3</a>

<div id="tab1">content 1</div>
<div id="tab2" style="display:none;">content 2</div>
<div id="tab3" style="display:none;">content 3</div>

Refer:
Simple show hide content I
http://wolfram.kriesing.de/blog/index.php/2008/javascript-remove-element-from-array

Saturday, July 3, 2010

Ruby on Rails: Array : Nil item

@array = [ 1, nil, 3, nil, 5 ]

Remove nil item

@array.compact    #=> [ 1, 3, 5 ]


Count total of nil item

@array.length    #=> 5
@array.nitems    #=> 3
total_nil = @array.length - @array.nitems


Check the present of nil item

@array.include?nil    #=> true


Location of the nil item

@array.index(nil)    #=> 1



Refer site: http://ruby-doc.org/core/classes/Array.html


If you find this useful, would you like to buy me a drink? No matter more or less, it will be an encouragement for me to go further. Thanks in advance!! =)

Wednesday, November 11, 2009

Ruby on Rails : Array : Remove duplicate item

In views.rhtml
<% 
x = 0
arr_type = Array.new
@articles = Article.find(:all)
for article in @articles
arr_type[x] = article.type
x += 1
end
@type = arr_type.uniq*", "

# Output
@articles = research, research, product, product, research, calendar
@type = research, product, calendar
%>

Friday, October 30, 2009

Ruby on Rails : Loop Alphabet A to Z

Print alphabet from A to Z using Array.

Example code :
<%= ('A' .. 'Z').to_a*" " %>

Output :
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z