Scripts and functions that I have written or found useful

Resetting owner of files Linux

June 11th, 2008 Posted in linux commands | No Comments »

chown user:group -R *

recursively resets all files the the ownership “user” in the group “group” for all files and folders in current working directory and and sub folders.

Drupal WYSIWYG stops working with my new template

May 31st, 2008 Posted in Drupal | No Comments »

I work on the technical side of Drupal and my colleague works on the design part. At one point in time (without realising when), our WYSIWYG editor stopped working and no others would work in its place. We were using HTMLArea/Xinha but tried the YUI as well as the FCKeditor without any luck. The strange thing was that the WYSIWYG editor would appear in the blocks edit page but not when creating/editing a page.

After many hours of searching the database (thinking it might have been HTMLArea that broke it so trying to find that setting which is trying to load 2 editors), I accidently found out that the editor work except in our new template.

After further hours of trying to compare, I eventually found out that it was down to a simple variable not being called into the template at the bottom…

<?php print $closure ?>

Without this, the WYSIWYG doesn’t activate properly. You will notice that it is trying to because the Body textarea is smaller than when the editor is switched off but the editor doesn’t actually appear.

I hope this saves at least one other person hours of frustrating searching!

Drupal htaccess for SEO friendly URLs

February 27th, 2008 Posted in Drupal, mod_rewrite | No Comments »

I have had some issues myself with drupal websites and getting the SEO friendly URLs to work when I either have no access to the server or the server changes cannot be made (CPANEL does cause a problem)

Attached is a copy of an htaccess that you can use….

Drupal Example htaccess file

strtotime that accepts dates

February 7th, 2008 Posted in PHP | No Comments »

I got tired of writing many lines of code to simply add x amount of time onto a given date due to having to workout the linux timestamp for a certain date, then adding the date, then converting it back for next time so I wrote this…

Basically, it works in the same wasas strtotime() except that it requests a date rather than a timestamp.

//stringtotime(”+2 days”,”2008-02-07″);
function stringtotime($duration,$date)
{
$y = substr($date,0,4);
$m = substr($date,5,2);
$d = substr($date,8,2);
$timestamp = mktime(0,0,0,$m,$d,$y);
$newtimestamp = strtotime(”+ $duration days”,$timestamp);
$newdate = date(”Y-m-d”,$newtimestamp);
return $newdate;

}

Quick Email Function

February 7th, 2008 Posted in PHP | No Comments »

Its not the most secure email function in the world but if you want something quick or simple especially for testing then I use the below…
function send_email($recipient,$subject,$body)
{
$headers = “From: Email System\n”;
$headers .= “Reply-To: me@mydomain.com\n”;
$headers .= “MIME-version: 1.0\n”;
$headers .= “Content-type: multipart/alternative; boundary=\”Message-Boundary\”\n\n”;
$headers .= “This is a multi-part message in MIME format.\n\n”;
$headers .= “–Message-Boundary\n”;
$headers.=”Content-Type: text/plain; charset=iso-8859-1\n”;
$headers.=”Content-Transfer-Encoding: 8bit\n\n”;
$headers.= $body.”\n\n”;
$headers.=”–Message-Boundary–”;
return mail($recipient,$subject,”",$headers);

}
This will send an email given a recipient, subject, and email text and return TRUE if the sending was successful, and FALSE if the sending failed. Note that the return is not if it was received successfully, only that the mail function itself didnt find any fault.

mysql delete duplicate rows

November 5th, 2007 Posted in MYSQL | 2 Comments »

DELETE tablename
FROM tablename,
(SELECT MAX(uid) AS dupid,COUNT(uid) AS dupcnt
FROM tablename
GROUP BY id,url HAVING dupcnt>1)
AS dups
WHERE tablename.uid=dups.dupid;

replace string in a table

October 17th, 2007 Posted in MYSQL | No Comments »

If you want to change certain rows in a database where a query applies … for example an “if” query finding all entries which contain “samplesearchtext”… then run the following script either via mysql/phpmyadmin/php

update mytable set myfield =replace (myfield, ’samplesearchtext’, ’samplereplacetext’);

Shorten string length

September 26th, 2007 Posted in PHP | No Comments »

$query = substr($query,0,-1);

Shortens the string by one character. Change the second value to remove more or see php.net/substr for more information

mysql error codes

September 24th, 2007 Posted in MYSQL | No Comments »

#1046-No database Selected. You are trying to query without choosing a database
#1054 - Unknown Column
#1109 - Unknown Table
#1136 - Column count doesn’t match the value count

Find and replace in mysql

September 10th, 2007 Posted in PHP | No Comments »

To find certain characters or strings and replace them with another value, execute the following query…

update [table_name] set [field_name] = replace([field_name],’[string_to_find]‘,’[string_to_replace]‘);

This is quite useful for finding strange characters that appear instead of letters with accents over them. I usually replace the strange character with the ASCII value (á becomes á)