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

About this user

Sasha http://nothing-less.net

« Newer Snippets
Older Snippets »
Showing 11-20 of 70 total

Migrate a site to another server

Use the code below in SSH to migrate all the files of a site from one server to another, without having to download/upload them.

wget -rc --level=20 ftp://username:password@olddomain.net/public_html

Migrate a MySQL Database to another server

Use the below in SSH to move a MySQL database between servers. Super handy if the database is larger than 7MB and you can't use phpMyAdmin.

mysqldump -h oldhost -u oldusername -poldpassword olddbname | mysql -h newhost -u newusername -pnewpassword newdbname     


http://wiki.dreamhost.com/index.php/Migrate_MySQL

Allow Admins access to member registration page

Open up mod.member_register.php, and look around line 69:

if ($SESS->userdata(’member_id’) != 1)
{
return $OUT->show_user_error(’general’, array($LANG->line(’mbr_you_are_registered’)));
}


Change the 0 to a 1 like shown above. :)

Remove www from URLs.

This is important to implement for your search engine ranking. Google, for example, thinks www.domain.com is a different site from domain.com, and this can seriously impact where you rank in Google. There are other ways of doing this, but the below is the only one that doesn't spit back a 500 error on my server, for some reason.

RewriteEngine On 
RewriteCond %{HTTP_HOST} ^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [R=301,L]

Loop in PHP Using For (Descending)

For those times you need to do a loop in PHP, but have the outcome be in descending order (ie, start at 10 and end at 1).

$totalcode="10";
for($i=$totalcode; $i>0; $i--){

 echo"$i";
}

Loop in PHP Using For (Ascending)

If you have a set number of times you need to repeat a piece of coding, using the for loop is the easiest way to accomplish this.

$i is the starting number, 1 in this case. This loop will repeat itself until $i reaches 10 - you can change this to any desired number. The bit inbetween the { and } is simply what is repeated - replace this with anything you want. :)

for ($i = 1; $i <= 10; $i++) {
   echo $i;
}

Pad Numbers in PHP

Sometimes I need numbers to always be two digits, even if they're less than 10. The below snippet will format all numbers to be two digits, inserting a 0 if they're under 10 - creating, for example, 09 instead of 9.

$raw = 1;
$formatted = sprintf("%02d", $raw);
echo $formatted;

//outputs 01

GREP searching in BBEdit

I do some GREP searching in BBEdit at times, and am saving the code I use for it here so I don't have to look it up every time!

The following will find any lines that have text like this: /anynumberoflettersortext/ABC - where ABC is any three letters, uppercase or lowercase, but no more than 3.

/[-a-zA-Z0-9]+?/[-a-zA-Z]{3}

Relational Fields: Image Gallery Thumbnail

Use the code below to add a thumbnail version of the image gallery entry related to your current weblog entry (Expression Engine).

{related_entries id="fieldid"}
<img src="{thumb_url}" width="{thumb_width}" height="{thumb_height}" alt="{title}" class="thumb" />
{/related_entries}

Dynamic Dropdown in the Stand Alone Entry Form

The problem: you are hand-coding the stand alone entry form, but it includes several dropdowns that you don't want to hand code. Here's a bit of coding to use for any dropdowns that you want to pull the options as filled in the Custom Fields area of the Admin panel.

In this example, I'm pulling the data for a field that has an ID of 13 - change this as needed. :)

<select name="field_id_13">{exp:query sql="SELECT field_list_items FROM exp_weblog_fields WHERE field_id = '13' "}
<?
$items ="{field_list_items}";
$items = explode("\n", $items); 
$howmany = count($items);
$i = 0; do { 
$item_id = $items[$i];
?>

<option value="<?=$item_id?>"><?=$item_id?></option>
<? 
 $i++; } while ($i < $howmany); ?>
{/exp:query}</select>
« Newer Snippets
Older Snippets »
Showing 11-20 of 70 total