Archive

Archive for the ‘Coding Tips’ Category

CakePHP is a Piece of Cake!

July 6th, 2011 1 comment

I’ve recently become aware of how behind the times my skills are , in particular if you write PHP you are expected to use a framework . I’ve chosen CakePHP and have installed it on my local Apache Server with a few permission hiccups. I’ve then completed the create a blog tutorial and it’s stunning how easy it is. It uses the MVC or Model-View-Controller system for development. To quote their documentation it CakePHP takes the monotony out of web development. We provide you with all the tools you need to get started coding what you really need to get done: the logic specific to your application. Instead of reinventing the wheel every time you sit down to a new project, check out a copy of CakePHP and get started with the real guts of your application.

Categories: Coding Tips, PHP Tags:

How to Remove Home in the Title of New Coppermine Gallery

June 30th, 2011 No comments

Edit include/themes.inc.php

Find the function and modify as follows


function theme_page_title($section)
{
global $CONFIG;
$return = strip_tags(bb_decode($section)) . ' - ' . $CONFIG['gallery_name']; // original code
$return = $CONFIG['gallery_name']; // Section removed
return $return;
}

this change works for cpg1.5.x

Categories: Coding Tips, PHP Tags:

Using Perl for Complex Edits

June 24th, 2011 No comments

All I use Vim 99% of the time whenever I want to do a really complex multiple edit I tend to use Perl.

So what’s the script doing ? It reads in one text file line by line and then operates on certain lines , and then writes out each line to an output file. You are only limited by your imagination as to what you do in the loop.

With a Perl script you can use Perl variables for counts, you can use a hash to store all the text and loop through it as often as required.

#!/usr/local/bin/perl
# pedit.pl
# description : Use for complex edits on a single file, adapt as required
# this trivial example is just incrementing a count
# zzapper
# v1.0
if ($#ARGV >= 0) { $file = $ARGV[0]; }
else
{
print "enter file:"; $file=;
}
chomp $file;
open (R,"$file") or die ("could not open $file ($!)");
$out = "c:/aaa/out.txt";
$out = "$file.txt";
$cnt=100;

open (W,">$out") or die ("could not open $out ($!)",__LINE__);
{
for $l ()
{
$l=~s/david1/david$cnt/gi; # do what you want here
print W $l;
$cnt++;
}
}
close W; close R;
print "\nOutput in cnt: $cnt :: $out\n";

Categories: Coding Tips, vim Tags:

Common Programmer Mistakes

August 26th, 2010 No comments

I for my sins code in PHP,Perl,ColdFusion using database MySQL and also write JavaScript, XML and battle with CSS and Ajax etc. Often I’m working on these projects in parallel which gets kind of stressful. Anyway I let my standards slip recently and made the following mistakes and suffered for it.

  • Not immediately taking my own backup of the files I was working so I could rollback at any time and as importantly prove whether my changes had or had not caused a regression or bug
  • Leaving test files lying about, so I later got confused as to which was the actual version
  • In Perl coded the catastrophic error $restart=0 if $cnt=1; (should be if $cnt==1)
  • Also in Perl had some problems due to not systematically initializing variables in a subroutine
  • To my credit have started to use  git for version control
Categories: Coding Tips Tags: ,

Parsing HTML with preg_match

April 8th, 2010 No comments

preg_match_all(‘#<b>.+?</b>#i’, $html, $matches);
// ? means non-greedy and is absolutely critical

$match[1], $match[2] contain the results

Non greedy stops the match gobbling everything between the first bold tag and the very last. I’ve never quite understood why greedy is the default behavior of RegExp (Regular Expressions) but I guess there’s a good reason for it.

PHP also has the very useful strip_tags function.

Categories: Coding Tips, PHP Tags:

Classic Programming Error: Variables inside a loop

March 25th, 2010 No comments

This for loop is OK

foreach($cargo_data as $cid=>$cargo_record)
{
$cargo_id=$cargo_data[$cid][‘cargo_id’];
$section=$cargo_data[$cid][‘section’];

}

But then later you add a condition cargo_id>6

foreach($cargo_data as $cid=>$cargo_record)
{
if ($cid>6) {$cargo_id=$cargo_data[$cid][‘cargo_id’];}
$section=$cargo_data[$cid][‘section’];

}

Now the bug is that if $cid is less than 6 $cargo_id is not set and will likely inherit the value it was set to in the last loop this type of error may not be immediately noticeable but can cause chaos. In this simple example it is easy to see that their should be an else clause, however the real solution is to systematically initialize all the variables at the beginning of the loop.

Categories: Coding Tips Tags:

Using Git for Document/Software Version Control

January 27th, 2010 No comments

I had been using my own ad-hoc system for years but recently started using CVS to maintain a history of my file and document changes. I was quite happy with CVS but everyone told me I should be using Git instead.  Git seems to be simpler and more intuitive. I can set up git for a folder or project very quickly. You just need the following 5 commands to get going

git init       (set up git for the current directory)
git add *.php   (add all your php files or whatever)
git commit -m “first version”    (also need to commit all your existing php files)
vi index.php (make a few changes)
git commit -a -m “Added new date feature”   (do this periodically to maintain version control)

gitk is a simple TK Gui which allows you to review you versions etc. I am delighted to have version control, all I have to do is remember to do a > git commit -a -m “Added new date feature” every now and again (eg daily)