PHP Snippets
1 ... « 6 7 8 9 10 »make_page_links
Information
(PHP Version >= 3)Functions used in this snippet: intval, ceil, min, max.
Description
str make_page_links( int page, int num_pages, str link [, int find [, int offset ]])
make_page_links will make numbered links for multiple pages of data. The function uses the current page of the pages that are available. The pages are split up by how many total entries there are (num_pages) and how many entries per page should be shown (find defaults to 10).
offset is the offset of numbering to show for the links (optional).
Snippet
<?php
function make_page_links($page,$num_pages,$link,$find=10,$offset=3){
$num_pages = intval(ceil($num_pages / $find));
if($num_pages == 1){
return '';
}
if($page < 1){
$page = 1;
}
if($page > $num_pages){
$page = $num_pages;
}
$ret = '';
if($page > $offset){
$ret = '<a href="' . $link . '">1</a> ... ';
}
if($page > 1){
$ret .= '<a href="' . $link . ($page - 1) . '/">«</a> ';
}
$max = min($page + ($offset - 1), $num_pages);
for($i = max(1, $page - ($offset - 1)); $i <= $max; $i++){
$ret .= ($i == $page ? '<strong>' . $i . '</strong>' : '<a href="' . $link . ($i != 1 ? $i . '/' : '') . '">' . $i . '</a> ');
}
if($page < ($num_pages)){
$ret .= '<a href="' . $link . ($page + 1) . '/">»</a> ';
}
if($page < ($num_pages - $offset + 1)){
$ret .= '... <a href="' . $link . $num_pages . '/">' . $num_pages . '</a>';
}
return $ret;
}
?>
Example
make_page_links Can be used in the following way:
<?php
// be on page 3 of 300 entries with a link to index.php
echo make_page_links('3', 300, '/index.php?page=');
// <12345> ... 30
echo make_page_links(7, 300, '/index.php?page=');
// 1 ... <56789> ... 30
echo make_page_links(1, 300, '/index.php?page=', 30, 5);
// 12345> ... 10
?>
rating
Information
(PHP Version >= 4.3.0)Functions used in this snippet: file_exists, is_readable, is_array, unserialize, file_get_contents, array_key_exists, array_sum, sizeof, array_merge, fopen, fwrite, serialize, fclose.
Description
str rating( str file, str get [, mixed add ] )
Uses file to store ratings for different values. The values are stored by their get key. If the add parameter is set, a new value will be added to get with the value of add.
Snippet
<?php
function rating($file,$get,$add=NULL){
global $_array;
if(!file_exists($file) || !is_readable($file)){
return 0;
}
$_array = is_array($_array) ? $_array : unserialize(file_get_contents($file));
$_array = is_array($_array) ? $_array : array();
if($add === NULL){
if(array_key_exists($get, $_array)){
$rating = array_sum($_array[$get]) / sizeof($_array[$get]);
}else{
return 0;
}
}else{
$add = is_array($add) ? $add : array($add);
$_array[$get] = array_merge($_array[$get],$add);
$rating = array_sum($_array[$get]) / sizeof($_array);
$fp = fopen($file,'w');
fwrite($fp,serialize($_array));
fclose($fp);
}
return $rating;
}
?>
Example
rating Can be used in the following way:
<?php
// add a vote of 5 to something with an id of 4
echo rating('file.txt', '4', '5'); // returns the average (5 if not previously set)
// add another vote
echo rating('file.txt', '4', '1'); // returns the average (3 using the example from above again (5+1/2))
// get the rating of something
echo rating('file.txt','4');
// returns the rating (using example from above 3 would return again);
echo rating('file.txt', 'newpost');
// gets the rating for the field stored as newpost
?>
filesize_tag
Information
(PHP Version >= 4)Functions used in this snippet: floor, array_key_exists, implode, array_reverse, sizeof.
Description
str filesize_tag( int size[, bool full] )
Takes a filesize of size and turns it into its textual form. If the optional field full is set to true, the complete filesize will be returned.
Snippet
<?php
function filesize_tag($size,$full=false){
$sizes = array();
$tags = array(' B', ' KB', ' MB', ' GB', ' TB');
$i = 0;
$sizes[$i] = $size;
$i++;
while($size > 1024){
$size = floor($size / 1024);
$sizes[$i] = $size;
$i++;
}
foreach($sizes as $key=>$size){
$subtract = array_key_exists(($key + 1), $sizes) ? $sizes[$key + 1] * 1024 : 0;
$sizes[$key] = $sizes[$key] - $subtract . $tags[$key];
}
return $full ? implode(', ',array_reverse($sizes)) : $sizes[sizeof($sizes) - 1];
}
?>
Example
filesize_tag Can be used in the following way:
<?php
echo filesize_tag(123456); // 120 KB
echo filesize_tag(123456789); // 117 MB
echo filesize_tag(12345,true); // 12 KB, 57 B
echo filesize_tag(filesize('somefile.txt')); // size of the file...
?>
gradient_text
Information
(PHP Version >= 3.0.6)Functions used in this snippet: str_replace, strlen, hexdec, substr, dechex, implode.
Description
str gradient_text( str str, str color1, str color2, str html )
Outputs a string as a gradient from color color1 to color color2. It outputs the colors based on the tag of type html.
Snippet
<?php
function gradient_text($str, $color1, $color2, $html='span'){
$color1 = str_replace('#', '', $color1);
$color2 = str_replace('#', '', $color2);
$color1 = strlen($color1) === 3 ? $color1{0} . $color1{0} . $color1{1} . $color1{1} . $color1{2} . $color1{2} : $color1;
$color2 = strlen($color2) === 3 ? $color2{0} . $color2{0} . $color2{1} . $color2{1} . $color2{2} . $color2{2} : $color2;
$endstr = '<' . $html . ' style="color:#' . $color1 . ';">' . $str{0} . '</' . $html . '>';
$colors[0] = hexdec(substr($color2, 0, 2));
$colors[1] = hexdec(substr($color2, 2, 2));
$colors[2] = hexdec(substr($color2, 4, 2));
$colors[3] = hexdec(substr($color1, 0, 2));
$colors[4] = hexdec(substr($color1, 2, 2));
$colors[5] = hexdec(substr($color1, 4, 2));
for($i=1; $i<strlen($str) - 1; $i++){
if($str{$i} != ' '){
$color = array();
$color[] = dechex(($colors[0] - $colors[3]) / strlen($str) * $i + $colors[3]);
$color[] = dechex(($colors[1] - $colors[4]) / strlen($str) * $i + $colors[4]);
$color[] = dechex(($colors[2] - $colors[5]) / strlen($str) * $i + $colors[5]);
$color[0] = strlen($color[0]) < 2 ? $color[0] . $color[0] : $color[0];
$color[1] = strlen($color[1]) < 2 ? $color[1] . $color[1] : $color[1];
$color[2] = strlen($color[2]) < 2 ? $color[2] . $color[2] : $color[2];
$endstr .= '<' . $html . ' style="color:#' . implode($color,'') . '">' . $str{$i} . '</' . $html . '>';
}else{
$endstr .= ' ';
}
}
$endstr .= '<' . $html . ' style="color:#' . $color2 . ';">' . $str{(strlen($str)-1)} . '</' . $html . '>';
return $endstr;
}
?>
Example
gradient_text Can be used in the following way:
<?php
echo gradient_text('red will turn into blue', '#FF0000', '#0000FF');
// out puts "red will turn into blue" as a gradient from red to blue
echo gradient_text('red will turn into blue', 'f00', '00f', 'font');
// outputs the same as above, but uses a font tag instead of span
// ( i dunno why you would do this but just in case you really want to go against good coding standards)
?>
get_world_pop
Information
(PHP Version >= 3.0.12)Functions used in this snippet: date, strtotime, time, number_format.
Description
str get_world_pop( [int seconds[, bool english]] )
calling get_world_pop with no arguements will return a formatted string of the estimated current world population at that second. If you supply the seconds parameter it will supply the world population for the date as long as it is between the years of 2005 and 2048. If the english value is set to false the formatted string will have periods instead of commas seperating thousands and hundreds etc.
Snippet
<?php
function get_world_pop($seconds=NULL,$english=true){
$pops = array(
"2005" => array( "start" => 6451058790, "added" => 74427813),
"2006" => array( "start" => 6525486603, "added" => 74629207),
"2007" => array( "start" => 6600115810, "added" => 74940532),
"2008" => array( "start" => 6675056342, "added" => 75228043),
"2009" => array( "start" => 6750284385, "added" => 75466071),
"2010" => array( "start" => 6825750456, "added" => 75688866),
"2011" => array( "start" => 6901439322, "added" => 75802963),
"2012" => array( "start" => 6977242285, "added" => 75615963),
"2013" => array( "start" => 7052858248, "added" => 75167390),
"2014" => array( "start" => 7128025638, "added" => 74490498),
"2015" => array( "start" => 7202516136, "added" => 73766792),
"2016" => array( "start" => 7276282928, "added" => 73055605),
"2017" => array( "start" => 7349338533, "added" => 72230253),
"2018" => array( "start" => 7421568786, "added" => 71289623),
"2019" => array( "start" => 7492858409, "added" => 70235773),
"2020" => array( "start" => 7563094182, "added" => 69180831),
"2021" => array( "start" => 7632275013, "added" => 68144353),
"2022" => array( "start" => 7700419366, "added" => 67028250),
"2023" => array( "start" => 7767447616, "added" => 65862519),
"2024" => array( "start" => 7833310135, "added" => 64679285),
"2025" => array( "start" => 7897989420, "added" => 63596858),
"2026" => array( "start" => 7961586278, "added" => 62635490),
"2027" => array( "start" => 8024221768, "added" => 61689308),
"2028" => array( "start" => 8085911076, "added" => 60746079),
"2029" => array( "start" => 8146657155, "added" => 59800227),
"2030" => array( "start" => 8206457382, "added" => 58925303),
"2031" => array( "start" => 8265382685, "added" => 58132001),
"2032" => array( "start" => 8323514686, "added" => 57333600),
"2033" => array( "start" => 8380848286, "added" => 56512857),
"2034" => array( "start" => 8437361143, "added" => 55666984),
"2035" => array( "start" => 8493028127, "added" => 54846652),
"2036" => array( "start" => 8547874779, "added" => 54058633),
"2037" => array( "start" => 8601933412, "added" => 53249683),
"2038" => array( "start" => 8655183095, "added" => 52414904),
"2039" => array( "start" => 8707597999, "added" => 51542658),
"2040" => array( "start" => 8759140657, "added" => 50686755),
"2041" => array( "start" => 8809827412, "added" => 49847392),
"2042" => array( "start" => 8859674804, "added" => 48957732),
"2043" => array( "start" => 8908632536, "added" => 48019825),
"2044" => array( "start" => 8956652361, "added" => 47040969),
"2045" => array( "start" => 9003693330, "added" => 46074635),
"2046" => array( "start" => 9049767965, "added" => 45123225),
"2047" => array( "start" => 9094891190, "added" => 44148176),
"2048" => array( "start" => 9139039366, "added" => 43161841));
$yearly = 31535999; // seconds in a year usually
if($seconds !== NULL){
$year = date('Y',$seconds);
$thisyear = $seconds - strtotime('jan 1 ' . $year . ' 00:00');
}else{
$year = date('Y');
$thisyear = time() - strtotime('jan 1 ' . $year . ' 00:00');
}
$sep = $english ? ',' : '.';
return number_format(($pops[$year]['start'] + (($thisyear / $yearly) * $pops[$year]['added'])),0,'.',$sep);
}
?>
Example
get_world_pop Can be used in the following way:
<?php
echo get_world_pop();
// returns the current world population formatted like 6,578,968,451
echo get_world_pop(strtotime('July 6 2010'));
// returns the world estimated world populating on july 6 2010
echo get_world_pop(NULL,false);
// returns the current world population formatted like 6.578.968.451
?>
1 ... « 6 7 8 9 10 »