Abbreviating Monetary Values 

Tested solution:

function CurrencyFormat($cash) {
// strip any commas
$cash = (0 + str_replace(',', '', $cash));


// filter and format it
if($cash>1000000000000){
return "$".round(($cash/1000000000000),1).'T';
}elseif($cash>1000000000){
return "$".round(($cash/1000000000),1).'B';
}elseif($cash>1000000){
return "$".round(($cash/1000000),1).'M';
}elseif($cash>1000){
return "$".round(($cash/1000),1).'K';
}


}


echo CurrencyFormat('1,560,000');//outputs $1.6M


[ view entry ] ( 1831 views )   |  print article
CSV file to MySQL with PHP 
//draft


$csv='...CSV file (string) exported from open office spread sheet';

$lines = explode("\n", $csv);

$array = array();
foreach ($lines as $line) {
$array =str_getcsv($line);

echo "INSERT INTO TestTable SET `Field1`='".addslashes($array[3])."',
`Field2`='".addslashes($array[3])."',";

}


[ view entry ] ( 1392 views )   |  print article
Adjacency List Hierarchy Array to HTML Unordered List 
<?php

/*
Sample data.
*/
$items = array(
array('id'=>1, 'title'=>'Home', 'parent_id'=>0),
array('id'=>2, 'title'=>'News', 'parent_id'=>1),
array('id'=>3, 'title'=>'Sub News', 'parent_id'=>2),
array('id'=>4, 'title'=>'Articles', 'parent_id'=>0),
array('id'=>5, 'title'=>'Article', 'parent_id'=>4),
array('id'=>6, 'title'=>'Article2', 'parent_id'=>4)
);

/*
Group by parent.
*/
$itemsByParent = array();
foreach ($items as $item)
{
if (!isset($itemsByParent[$item['parent_id']]))
$itemsByParent[$item['parent_id']] = array();

$itemsByParent[$item['parent_id']][] = $item;
}

/*
Print list recursively.
*/
function printList($items, $parentId = 0)
{
echo '<ul>';
foreach ($items[$parentId] as $item)
{
echo '<li>';
echo $item['title'];
$curId = $item['id'];
//if there are children
if (!empty($items[$curId]))
{
printList($items, $curId);
}
echo '</li>';
}
echo '</ul>';
}

/*
Finds top parent given node id
*/
function findTopParent($id,$ibp)
{
foreach($ibp as $parentID=>$children)
{
foreach($children as $child)
{
if($child['id']==$id)
{
if($child['parent_id']!=0)
return findTopParent($child['parent_id'],$ibp);
else
return $child;
}
}
}
}

/*
Get all parents, aka. breadcrumbs.
*/
function getAllParents($id,$ibp)
{
foreach($ibp as $parentID=>$nodes)
{
foreach($nodes as $node)
{
if($node['id']==$id)
{
if($node['parent_id']!=0)
{
$a=getAllParents($node['parent_id'],$ibp);
array_push($a,$node['parent_id']);
return $a;
}
else
return array();
}
}
}
}

/*
Gets all subnodes; children, grand children, etc...
*/
function getDescendants($id,$ibp)
{
if(array_key_exists($id,$ibp))
{
$kids=array();
foreach($ibp[$id] as $child)
{
array_push($kids,$child['id']);

if(array_key_exists($child['id'],$ibp))
$kids=array_merge($kids,getDescendants($child['id'],$ibp));
}
return $kids;
}
else
return array();//supplied $id has no kids
}

// Print it!!
printList($itemsByParent);

// Find top parent!!
print_r(findTopParent(6,$itemsByParent));
/*
Array
(
[id] => 4
[title] => Articles
[parent_id] => 0
)
*/

// Find path!!
print_r(getAllParents(3,$itemsByParent));
/*
Array
(
[0] => 1
[1] => 2
)
*/

print_r(getDescendants(1,$itemsByParent));
/*
Array
(
[0] => 2
[1] => 3
)
*/

print_r(getDescendants(4,$itemsByParent));
/*
Array
(
[0] => 5
[1] => 6
)
*/

print_r(getDescendants(0,$itemsByParent));
/*
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
)
*/


Output of printList:
  • Home
    • News
      • Sub News
  • Articles
    • Article
    • Article2


[ view entry ] ( 2167 views )   |  print article
jQuery: Check if an image succesfully loads 

$(function(){

//prevent image cache
$('#a_img').attr('src',$('#a_img').attr('src')+'?rand='+Math.floor(Math.random()*10000));

//check for loading errors
$('#a_img').load().error(function(){

/* Legacy support kicking in */
var LegacyImg='http://www.example.com/dbimages/tag/h1/img_name.jpeg';
$('#a_img').attr('src',LegacyImg);

});

})


[ view entry ] ( 1291 views )   |  print article
Linux: Configuring Static Routes 
//Tested:
route add -net 10.0.0.0 netmask 255.0.0.0 gw 172.16.1.177
route delete -net 10.0.0.0 netmask 255.0.0.0 gw 172.16.1.177

//
ip route add 192.168.1.0/24 dev eth0
ip route add default via 192.168.1.254
ip route delete 192.168.1.0/24 dev eth0


[ view entry ] ( 1807 views )   |  print article
IPSec Tunnels with VTIs 
VIRTUAL TUNNEL INTERFACES

Cisco® IPSec VTIs are a new tool that customers can use to configure IPSec-based VPNs between site-to-site devices. IPSec VTI tunnels provide a designated pathway across a shared WAN and encapsulate traffic with new packet headers, which helps to ensure delivery to specific destinations. The network is private because traffic can enter a tunnel only at an endpoint. In addition, IPSec provides true confidentiality (as does encryption) and can carry encrypted traffic.With IPSec VTIs, users can provide highly secure connectivity for site-to-site VPNs and can be combined with Cisco AVVID (Architecture for Voice, Video and Integrated Data) to deliver converged voice, video, and data over IP networks.

BENEFITS:

• Simplifies management---Customers can use the Cisco IOS® Software virtual tunnel constructs to configure an IPSec virtual tunnel interface, thus simplifying VPN configuration complexity, which translates into reduced costs because the need for local IT support is minimized. In addition, existing management applications that can monitor interfaces can be used for monitoring purposes.

• Supports multicast encryption---Customers can use the Cisco IOS Software IPSec VTIs to transfer the multicast traffic, control traffic, or data traffic---for example, many voice and video applications---from one site to another securely.

• Provides a routable interface---Cisco IOS Software IPSec VTIs can support all types of IP routing protocols. Customers can use these VTI capabilities to connect larger office environments---for example, a branch office, complete with a private branch exchange (PBX) extension.

• Improves scaling---IPSec VTIs need fewer established security associations to cover different types of traffic, both unicast and multicast, thus enabling improved scaling.

• Offers flexibility in defining features---An IPSec VTI is an encapsulation within its own interface. This offers flexibility of defining features to run on either the physical or the IPSec interface.

Source (as of 2013):
http://www.cisco.com/en/US/technologies ... Paper.html


[ view entry ] ( 1830 views )   |  print article
Setting NTP in Cisco 1721 router. 
router#show ver
Cisco IOS Software, C1700 Software (C1700-ADVENTERPRISEK9-M), Version 12.4(7), RELEASE SOFTWARE (fc6)...

router(config)# ntp server 1.gr.pool.ntp.org
router(config)#clock timezone PST -7 //Los Angeles :)

router#show clock
18:21:43.570 PST Sat Oct 12 2013

router# show ntp associations
router# show ntp status



[ view entry ] ( 2107 views )   |  print article
PHP: Caching resized images in Memcached. 

function resize_image($file, $w, $h, $crop=FALSE) {
list($width, $height) = getimagesize($file);
$r = $width / $height;
if ($crop) {
if ($width > $height) {
$width = ceil($width-($width*abs($r-$w/$h)));
} else {
$height = ceil($height-($height*abs($r-$w/$h)));
}
$newwidth = $w;
$newheight = $h;
} else {
if ($w/$h > $r) {
$newwidth = $h*$r;
$newheight = $h;
} else {
$newheight = $w/$r;
$newwidth = $w;
}
}
$src = imagecreatefromjpeg($file);
$dst = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($dst, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

return $dst;
}
header('Content-Type: image/jpeg');

$memcache = new Memcache;
$memcache->connect('127.0.0.1', 11211) or die ("Could not connect to Memcached.");


$key = md5('Snake_River_(5mb).jpg');
$get_result = $memcache->get($key);

if ($get_result) {
echo $get_result;
}
else {
$img = resize_image('Snake_River_(5mb).jpg', 300, 300);

ob_start();
imagejpeg($img);


$memcache->set($key, ob_get_contents(), 0, 10); // Cache thumbnail for 10 secs.



}
imagedestroy($img);


[ view entry ] ( 2474 views )   |  print article
PHP: Caching MySQL queries in Memcached 
Using PHP memcache client:
	
public function doFetchRecords(){

$memcache = new Memcache;
$memcache->connect('127.0.0.1', 11211) or die ("Could not connect to Memcached.");

$SQL="SELECT * FROM statistics";
$key = md5($SQL);
$get_result = $memcache->get($key);

if ($get_result) {
echo '<!--Memcache Data-->';
return $get_result;
}
else {
$this->db->setQuery($SQL);
$data=$this->db->executeQuery(true);
$memcache->set($key, $data, 0, 30); // Store the result of the query for 30 seconds
return $data;
}
}


[ view entry ] ( 2989 views )   |  print article
MySQL: Search and replace a string in a column. 
MySQL database has a handy and simple string function REPLACE() that allows table data with the matching string (from_string) to be replaced by new string (to_string). This is useful if there is need to search and replace a text string which affects many records or rows, such as change of company name, postcode, URL or spelling mistake.

The syntax of REPLACE is REPLACE(text_string, from_string, to_string)

MySQL reference describes REPLACE as function that returns the string text_string with all occurrences of the string from_string replaced by the string to_string, where matching is case-sensitive when searching for from_string. text_string can be retrieved from the a field in the database table too. Most SQL command can be REPLACE() function, especially SELECT and UPDATE manipulation statement.

For example:
update TABLE_NAME set FIELD_NAME = replace(FIELD_NAME, ‘find this string’, ‘replace found string with this string’);

update client_table set company_name = replace(company_name, ‘Old Company’, ‘New Company’)


The above statement will replace all instances of ‘Old Company’ to ‘New Company’ in the field of company_name of client_table table.

Another example:

SELECT REPLACE(‘www.mysql.com’, ‘w’, ‘Ww’);


Above statement will return ‘WwWwWw.mysql.com’ as result.

http://www.mydigitallife.info/how-to-fi ... using-sql/

[ view entry ] ( 1901 views )   |  print article

<<First <Back | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | Next> Last>>


2024 By Angel Cool