MySQL Backups 
//dump a database
mysqldump -u UserName -p --host=dev.barney.com db_name > db_name_backup.sql
//compressed dump
mysqldump -u UserName -p --host=dev.barney.com db_name | gzip > db_name_backup.sql


//restore database
mysql -u UserName -p target_db_name < db_name_backup.sql

//dump and restore
mysqldump -u UserName -p --host=dev.barney.com db_name | mysql -u UserName -p target_db_name



[ view entry ] ( 1347 views )   |  print article
Riak: Tasks 101 
//list all buckets
curl -i riak1.barney.com:8098/buckets?buckets=true

//putting/storing JSON in riak
curl -v -XPUT riak1.barney.com:8098/buckets/my_bucket/keys/my_test?returnbody=true \
-H "Content-Type: application/json" \
-d '{"TestName":"Monday test"}'

//listing keys in bucket
curl -i riak1.barney.com:8098/buckets/my_bucket/keys?keys=true

//fetch a key
curl -v riak1.barney.com:8098/buckets/my_bucket/keys/my_test


http://docs.basho.com/riak/latest/dev/using/basics/

[ view entry ] ( 1471 views )   |  print article
Mimicking MySQL LIMIT with PHP array_slice 

//SELECT * FROM tbl LIMIT 5,10; # Retrieve rows 6-15

$input = array("1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19");
print_r(array_slice($input,5,10));


Output:
Array
(
[0] => 6
[1] => 7
[2] => 8
[3] => 9
[4] => 10
[5] => 11
[6] => 12
[7] => 13
[8] => 14
[9] => 15
)


Complete Pagination Example:
function PaginateArray($input, $page, $show_per_page) {

$page = $page < 1 ? 1 : $page;

$start = ($page - 1) * ($show_per_page);
$offset = $show_per_page;

$outArray = array_slice($input, $start, $offset);

var_export($outArray);
}

$input = array("1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20");

//page 1
PaginateArray($input, 1, 3);//inputs: array, page, records per page
/*
array (
0 => '1',
1 => '2',
2 => '3',
)
*/

//page 2
PaginateArray($input, 2, 3);//inputs: array, page, records per page
/*
array (
0 => '4',
1 => '5',
2 => '6',
)
*/



[ view entry ] ( 1459 views )   |  print article
MongoDB CLI 101 Fundamentals 

[Angel@localhost ~]$ mongo
> show collections; //equivalent to rdbms tables
> use test; //use test db
> db.test.count();
> db.test.find(); //all documents, a document is like a rdbms row.
> db.test.find({_id:ObjectId("52c351ffc5c629a6ca901ae5")});// where _id equals to...
> db.test.find({_id:ObjectId("52c351ffc5c629a6ca901ae5")},{'_id':0}); // _id field is not displayed
> db.test.update({_id:ObjectId("52c351ffc5c629a6ca901ae5")},{'field-to-update':'new-value'});//updating a field
> db.test.update({_id:ObjectId("52c351ffc5c629a6ca901ae5")},{$set:{'Year':2014}}); //add new field



[ view entry ] ( 1522 views )   |  print article
Download/Extract HTML table data as CSV with JQuery and PHP 
//using table2csv plugin

//HTML:
<form action="getCSV.php" method ="post" >
<input type="hidden" name="csv_text" id="csv_text">
<input type="submit" value="Get CSV File"
onclick="getCSVData()"
</form>
<script>
function getCSVData(){
var csv_value=$('#tableID').table2CSV({delivery:'value'});
$("#csv_text").val(csv_value);
}
</script>

//php
<?php
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"my-data.csv\"");
$data=stripcslashes($_REQUEST['csv_text']);
echo $data;
?>



[ view entry ] ( 1282 views )   |  print article
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

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


2024 By Angel Cool