Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-10 of 443 total  RSS 

Array of country list in PHP with Zend Framework

<?php
header('Content-Type: text/html; charset=utf-8');
require_once 'Zend/Locale.php';

$locale = new Zend_Locale('en_US');

$countries = ($locale->getTranslationList('country', 'en'));
asort($countries, SORT_LOCALE_STRING);

echo "<pre>";
print_r($countries);
echo "</pre>";

GeoCode

//Mysql!

set @orig_lat=38.70032 ;
set @orig_lon=-9.38603;

/*Meters*/
set @dist=10;

set @mylat = @orig_lat;
set @mylon = @orig_lon;

/*1º = 69miles = 111Km or 111000 meters*/
set @unit = 111000;

set @lon1 = @mylon - @dist/abs(cos(radians(@mylat))*@unit);
set @lon2 = @mylon + @dist/abs(cos(radians(@mylat))*@unit);
set @lat1 = @mylat - (@dist/@unit);
set @lat2 = @mylat + (@dist/@unit);

SELECT 
  id, slug, latitude, longitude, councilId, @lon1, @lon2, @lat1, @lat2, @orig_lat, @orig_lon, @dist, @mylat, @mylon 
from 
  entity
where
    latitude
      between
        @lat1 AND @lat2
  AND
    longitude
      between
        @lon1 AND @lon2;

Be careful with converting boolean values!!

//


php> $v = 'FALSE'

php> var_dump((bool)$v)
bool(true)

php> $v = 'false';

php> var_dump((bool)$v)
bool(true)

php> $v = '0';

php> var_dump((bool)$v)
bool(false)

Passing information between PHP and Perl

// description of your code here

<?php

apache_note('name', 'Fredrik Ekengren');

// Call perl script
virtual("/perl/some_script.pl");

$result = apache_note("resultdata");
?>


#!/usr/bin/perl
#some stuff...

# Get Apache request object
my $r = Apache->request()->main();

# Get passed data
my $name = $r->notes('name');

# some processing

# Pass result back to PHP
$r->notes('resultdata', $result);

Functions to add / remove nodes to / from an XML file using PHP.

// description of your code here

<?php
function removNode($myXML, $node, $attribute, $id) {
    $xmlDoc = new DOMDocument();
    $xmlDoc->load($myXML);
    $xpath = new DOMXpath($xmlDoc);
   
    if( $attribute!='' || $id!='' )
    $nodeList = $xpath->query('//'.$node.'[@'.$attribute.'="'.$id.'"]');
    else
    $nodeList = $xpath->query('//'.$node.'');
   
    if ($nodeList->length)
    {
        $node = $nodeList->item(0)  ;
        $node->parentNode->removeChild($node);
    }
    $xmlDoc->save($myXML) ;
}   
?>

Management Games

PHP Print todays date in PHP

// description of your code here


September 3rd, 2002
<? print date("F jS, Y"); ?>

mm/dd/yyyy
<? print date("m/j/Y"); ?>

mm/dd/yy
<? print date("m/j/y"); ?>

More Info: http://www.php.net/manual/en/function.date.php


Sports Games

PHP View Counter

<?php
session_start();

if(isset($_SESSION['views']))
$_SESSION['views']=$_SESSION['views']+1;
else
$_SESSION['views']=1;
echo "Page Views=". $_SESSION['views'];
?>

Create unique ID hash from args

/*
This is very util for cache file names
@example
cacheName(__CLASS__, $_REQUEST);
*/
function cacheName(){
    return base_convert(md5( serialize(func_get_args()) ),16, 36);
}

base58 conversion

Super quick note, hopefully sufficient info.

The format for the short photo URLs is

flic.kr/p/{short-photo-id}

A short photo id is a base58 conversion of the photo id. Base58 is like base62 [0-9a-zA-Z] with some characters removed to make it less confusing when printed. (namely 0, O, I, and l).

So that leaves an alphabet of: 123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ

I'm including below a variation of the code we use to do base conversion. Note it doesn't use modulus because PHP's modulus operator overflows for large numbers (like photo ids)


//Original post: http://www.flickr.com/groups/api/discuss/72157616713786392/



    function base_encode($num, $alphabet) {
    $base_count = strlen($alphabet);
    $encoded = '';
    while ($num >= $base_count) {
    $div = $num/$base_count;
    $mod = ($num-($base_count*intval($div)));
    $encoded = $alphabet[$mod] . $encoded;
    $num = intval($div);
    }

    if ($num) $encoded = $alphabet[$num] . $encoded;

    return $encoded;
    }

    function base_decode($num, $alphabet) {
    $decoded = 0;
    $multi = 1;
    while (strlen($num) > 0) {
    $digit = $num[strlen($num)-1];
    $decoded += $multi * strpos($alphabet, $digit);
    $multi = $multi * strlen($alphabet);
    $num = substr($num, 0, -1);
    }

    return $decoded;
    }

develop and production server on same php file

// same mysql connection file for development and production, or more servers
// can you use the global $_SERVER['HTTP_HOST'] to identify your current active server, simple huh??
// this simple scrip identify your server by address
// if you want more serves, just add a elseif clause

<?php
//first - your development server
if ($_SERVER['HTTP_HOST'] == "127.0.0.1") 
{
$hostname_content = "localhost";
$database_content = "YOUR_DATABASE";
$username_content = "YOUR_USER";
$password_content = "YOUR_PASS";
$conn = mysql_pconnect($hostname_content, $username_content, $password_content) or trigger_error(mysql_error(),E_USER_ERROR); 
}
//second- your production server
else 
{
$hostname_content = "YOUR_PRODUTION_SERVER";
$database_content = "YOUR_PRODUCTION_DATABASE";
$username_content = "YOUR_PRODUCTION_USER";
$password_content = "YOUR_PRODUCTION_PASS";
$conn = mysql_pconnect($hostname_content, $username_content, $password_content) or trigger_error(mysql_error(),E_USER_ERROR); 
}
?>
« Newer Snippets
Older Snippets »
Showing 1-10 of 443 total  RSS