Skip to content

Instantly share code, notes, and snippets.

View achudars's full-sized avatar
🥔
every challenge is an opportunity

Aleks achudars

🥔
every challenge is an opportunity
View GitHub Profile
@achudars
achudars / web-snippet-041 ( Pure CSS bubble talk )
Last active December 17, 2015 00:19
Pure CSS bubble talk
/* CSS */
.bubble {
font: 12px Arial;
display: block;
width: 200px;
position: relative;
margin-bottom: 18px;
color:#fff;
padding: 15px;
background: #000;
@achudars
achudars / web-snippet-040 ( Get File Size in PHP )
Last active December 17, 2015 00:19
Get File Size in reader-friendly units (KB, MB, etc.) using PHP
<?php
/**
* Get the size of a file in reader-friendly units (KB, MB, etc.)
* @param string $filename The name of the file to size
* @param int $precision The number of decimal places to show (default: 1)
* @return string|bool A string with the size of the file or false on error
* @author Sunny Walker <www.miraclesalad.com>
*/
function getFileSize($filename, $precision=1) {
$return = false;
@achudars
achudars / web-snippet-039 ( dropdown list with redirection using jQuery )
Last active December 17, 2015 00:19
Get all links within a container and to convert it to a dropdown list with redirection using jQuery
/* use : $(selector).convertToList()
it's better to follow it with .remove() to remove the original container */
(function ($) {
$.fn.convertToList = function () {
var that = this;
this.before(
$('<select><option>Please select</option></select>').
change(function () {
window.location = $(this).val();
@achudars
achudars / web-snippet-038 ( Get page load time via Javascript and jQuery )
Last active December 17, 2015 00:19
Get page load time via Javascript and jQuery
//calculate the time before calling the function in window.onload
var beforeload = (new Date()).getTime();
function getPageLoadTime(){
//calculate the current time in afterload
var afterload = (new Date()).getTime();
// now use the beforeload and afterload to calculate the seconds
seconds = (afterload-beforeload) / 1000;
// Place the seconds in the innerHTML to show the results
$("#load_time").text('Page load time :: ' + seconds + ' sec(s).');
@achudars
achudars / web-snippet-037 ( jQuery Ajax & Get JSON Array )
Last active December 17, 2015 00:19
jQuery Ajax & Get JSON Array
$.ajax({
type: 'POST',
url: 'url.php',
data: 'nombre=' + $('#nombre').val() + '&' + 'edad=' + $('#edad').val(),
dataType: 'json',
success: function(data){
var html = '';
$.each(data,function(index,user){
html+= '<div class="user"><p>' + index + '</p>';
html+= '<p>' + user.user_edad + '</p></div>';
@achudars
achudars / web-snippet-036 ( Hide address bar using JavaScript )
Last active December 17, 2015 00:19
Hide address bar using JavaScript
function hideAddressBar(){
if(!window.location.hash){
if(document.height < window.outerHeight){
document.body.style.height = (window.outerHeight + 50) + 'px';
}
setTimeout( function(){ window.scrollTo(0, 1); }, 50 );
}
}
window.addEventListener("load", function(){ if(!window.pageYOffset){ hideAddressBar(); } } );
@achudars
achudars / web-snippet-035 ( Getting all URL parameters using JavaScript )
Last active December 17, 2015 00:19
Getting all URL parameters using JavaScript
// this object gets filled with params
var params = {};
// split and map parameters
location.search.substr(1).split('&').forEach(function(p) {
p = p.split('=');
params[p[0]] = decodeURIComponent(p[1] || 1);
});
@achudars
achudars / web-snippet-034 ( Display Twitter Count in PHP )
Last active December 17, 2015 00:19
Display Twitter Count for a specific screen name using PHP
<?php
$url = "http://api.twitter.com/1/users/show.json?screen_name=Aleks_Cudars&include_entities=true";
$feed = file_get_contents($url);
$twitter_decoded = json_decode($feed);
echo $twitter_decoded->followers_count;
@achudars
achudars / web-snippet-033 ( Create a list of child pages in PHP )
Last active December 17, 2015 00:19
Create a list of child pages and display their content on the parent page including the featured image using PHP
<?php
$child_pages = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_parent = ".$post->ID." AND post_type = 'page' ORDER BY menu_order", 'OBJECT');
?>
<?php if ( $child_pages ) : foreach ( $child_pages as $pageChild ) : setup_postdata( $pageChild ); ?>
<div class="child-thumb">
<?php echo get_the_post_thumbnail($pageChild->ID, 'thumbnail'); ?>
<a href="<?php echo get_permalink($pageChild->ID); ?>" rel="bookmark" title="<?php echo $pageChild->post_title; ?>"><?php echo $pageChild->post_title; ?></a>
</div>
@achudars
achudars / web-snippet-032 ( Get Current URL using PHP )
Last active December 17, 2015 00:19
Get Current URL using PHP
function getUrl() {
$url = @( $_SERVER["HTTPS"] != 'on' ) ? 'http://'.$_SERVER["SERVER_NAME"] : 'https://'.$_SERVER["SERVER_NAME"];
$url .= ( $_SERVER["SERVER_PORT"] !== 80 ) ? ":".$_SERVER["SERVER_PORT"] : "";
$url .= $_SERVER["REQUEST_URI"];
return $url;
}