Thursday, February 28, 2008
I always get stuck on one problem with PHP and that is referencing dynamically created array variables. Imagine I have an array of values, say country codes: 'uk,us,de,es' and I want to output data into array variables that have the name $array_countrycode.It took me a while to get to the bottom of this, I knew it had something to do with Variable Variables but the documentation doesn't really cover this kind of example. Eventually I found the solution I was looking for:
${"array_{$code}"}
I have the country codes stored in an array which I loop through:
$country_codes = array("au","de","es","fr","it","nl","uk","us");
foreach($country_codes as $code) {
// then I can reference the array to do whatever I like
${"array_{$code}"}
// would reference $array_au, $array_de etc.
}
This might be old news to some people but it had me scratching my head for quite a while!
Jon 10:22 AM Permalink
Friday, February 01, 2008
It's been quite a long time since the last update but it sounds like Codeigniter is going to become more of a development focus now that Expression Engine 2.0 is nearly ready. With the release of Codeigniter 1.6.0 comes a host of new features including DBForge class which is a complete database utility package.To see what has changed check out the changelog, this has all the new features as well as bug fixes listed. Upgrading from a previous version of Codeigniter is simple just follow these instructions and make sure you back up any modules, plugins or libraries that you have added in case you overwrite anything! To download the new versionI head to the main codeigniter site.
Jon 11:16 AM Permalink
Saturday, January 26, 2008
I just had Ryan post a comment on an old Code Igniter post and I thought it was worthy of a mention in its own right.Ryan has created something called Ignition which is a CRUD creator for Codeigniter. " What you don't like is writing the same mundane things over and over again for each object in your project. For instance, you have to write all the create, update, delete code in your model...for every model..., then all the create, view, list, update, views for those models, and finally a controller to wire them all together".
Ryan has made sure Ignition makes no assumptions, it only handles the really mundane parts of the code and leaves the developers to decide for themselves how to implement authentication, or include AJAX functionality. So if you are fed up with always creating the same bits of code and what to add the "rapid" back into RAD, check it out at ignition.lifewithryan.com.
Jon 2:56 PM Permalink
Wednesday, January 16, 2008
I'm a little late announcing this on freaksauce but I just launched winesocial.com.au which is a joint venture with my wife and something we feel will grow into a fantastic site for Australian and New Zealand wine lovers. The site is a community based wine review site where members can write a quick review and rate a bottle they've recently tried. The idea is that people in Australia and New Zealand can use the site as a resource for new wines to try. The site also features sections such offers, latest releases and gourmet food products which showcase the latest food & wine products on offer as well as accommodation and event information.The site was built using CodeIgniter 1.5.4 and jQuery, I'm actually using version 1.1.4 as the latest version seemed to break the rating plugin that I am using, I hope to rectify this problem at some stage.
Aside from the main winesocial.com.au site there is also a blog which uses Expression Engine 1.6.1 as I figured there was no point reinventing the wheel, I use EE on my other site Guitar Noize and love the flexibility that it provides, especially as I am a php developer.
So if you are an Aussie or Kiwi who loves some home grown wine or if you're simply just curious, check out winesocial.com.au. By the way if you are an Aussie or New Zealand resident you are eligible to enter our competition, it's very easy you just need to sign up as a member to be entered into the draw!
Jon 9:27 PM Permalink
Wednesday, October 03, 2007
Having successfully migrating my Guitar Noize weblog over to Expression Engine I wanted to create a list of links to my favourite guitar related blogs. At first I looked into creating a Blogroll weblog in EE and creating a new template which output the entries which would then be embedded into another template but then I came across this perfect and comprehensive module called Link List. It is very easy to install just download the zip and copy 3 files across to your EE installation, then in your EE control panel Modules tab click on Install next to the Link List module and you're ready to go.From there on its just a case of clicking on the LinkList module, add a load of links and then put some code into your template. The code you will need is:
<ul>
{exp:linklist:entries linklist="blogroll" orderby="random" dynamic="off"}
<li><a href="{linklist:url}">{linklist:url_title}</a></li>
{/exp:linklist:entries}
</ul>
When I created a new Linklist I called it blogroll hence the linklist attribute above. Also I want the links to display randomly but you can order by url_title or desc etc, you can sort ASC or DESC and you can use limit and paginate if you only want to display a few. One thing to be aware of is the dynamic attribute, if this is a static blogroll like mine set it to "off" otherwise it will try and find links based on keywords assigned to them in relation to the url of the page.
Labels: Expression Engine
Jon 12:29 PM Permalink
Saturday, August 25, 2007
Yesterday my wife's powerbook suddenly decided to not let her launch Mail. She was confronted with the error message, "mail cannot update your mailboxes because your home directory is full". I'd never come across this message before in the 5 or 6 years that I have been using OS X so I turned to Google's wisdom to help me fix the problem. It didn't take me long to come across the fix for this error on Hawkwings.net which by the way is a very useful site for tips and tricks on Apple mail.Now the basic fix is mentioned here which involves removing the 'Envelope index' file from your ~/Library/Mail folder however this didn't fix the problem. I then read the comments and found a message from 'Anne', she explained that simply removing the 'Envelope index' file wasn't enough and she needed to also remove the 'Envelope index journal' file in the same directory. I tried this, opened Mail, let the import run and hey presto! Back up and running.
I thought this was worth sharing in case others didn't take the time to scan the comments on Hawkwings' site.
Jon 10:00 PM Permalink
Sunday, August 12, 2007
One of the great things about Code Igniter is the fleixibility of the framework. I have recently used CI for a major project for Tourism Western Australia called WATV which showcases tourism and events in WA through the use of video. The site was built using Code Igniter and jQuery for the ajax calls and visual effects. This is the biggest project that I have worked on using Code Igniter so I learned a lot, but one thing I didn't learn until after we had actually finished the development of the first phase was using the _remap() function in the controllers and instead wrote my own code to deal with my custom routing when all the hard work has already been done and exists in the framework so I will need to revise the controllers for the next phase.When you are using custom routing you often come across a problem, you have specified that any url parameters after a certain controller name will be used by a certain method, now when you try to introduce another method it gets overidden by your routing. For example say you have a users controller and you want any username after /users/ in your url to look up a user. You could set up a custom route such as:
$route['users/:any'] = "users/index";
So now your index method in the users controller will check the url parameter against the user table. Now saw you want to add a method called edit, what will happen?
/users/edit/username
Well your route will send edit as a username to your index controller and break your method, so you have to check that the url parameter isn't "edit" and maybe run a different method in your controller:
if ($username == "edit") {
$user = $this->uri->segment(2);
$this->edit($user);
}
See how messy this would become one you start adding methods? Well luckily the clever coders who devised CI came up with a simple solution, _remap(). Now instead of using the custom routing you can remove that and set up the _remap() function in your controller:
function _remap($method)
{
if ($method == 'edit' || $method == 'update')
{
$this->$method();
}
else
{
$this->default_method();
}
}
Now what happens is you override the default controller action by catching the first url parameter and checking if it is a method name or something else like a username and if so calls another method of your choosing! Easy, and a lot cleaner than manually catching each parameter yourself. Hope this helps anyone faced with a similar problem!
Labels: Code Igniter
Jon 9:38 AM Permalink
Thursday, July 19, 2007
I just came across a problem that drove me crazy for ages. I have an ajax call that updates some content then has to reinitialise the javascript to attach events to the new elements on the page. I use $.getScript() and call the js file, so far so good. The problem I encountered was that everytime I clicked on a tab which called this function and then on a button that I was setting up it was firing multiple times, and incrementing everytime I clicked on a tab which called the $.getScript(). So I figured that somehow the click event was being assigned to my button everytime the script reinitialised. The solution? unbind the problematic event, then when $.getScript() is called the button is reinitialised with the single click event!$("#mybutton").unbind()
Simple when you know how!
Jon 5:17 PM Permalink
Saturday, July 14, 2007
I have just uploaded version 1.1 of GuitarLead.net which now uses my own RSS aggregation method instead of my Yahoo! Pipes version which was always going to be a temporary measure as it just didn't give me the control over individual feeds that I wanted. The latest version now uses php and mysql to gather feeds instead of having to create an xml file from multiple feeds twice a day. This means that I can handle the different types of feeds that I want aggregrate properly as some use Atom, some use RSS 2.0 and others use older versions of the RSS specification a couple of feeds had to be dropped due to no date node!The next step for me is to rebuild GuitarLead.net using Code Igniter so that I can easily extend on site features whenever I want and keep the code nice and tidy. I am currently building 2 major sites with Code Igniter (details soon!) and I'm really enjoying the experience, it not only saves time and repetition of code but it keeps me really organised, something I admit I'm not usually very good at!
Jon 5:04 PM Permalink
Saturday, June 23, 2007
When I think Web 2.0 I don't think about fancy Javascript effects and whether the site was developed with Ruby on Rails I expect a site that delivers a complete solution to a problem. Having recently been referred to Unfuddle by my Soap colleague Shane I was pleasantly suprised how easy hosted SVN could be. For a start the designers have taken the Basecamp approach to the user interface, simple tabs take you from milestones to source code to tickets etc. It is really easy to set up and you just need to install an SVN client (I'm using svnX). Once installed and logged in you're away, you can go to the dashboard to post or read tickets, milestones and messages, you can invite people to contribute to the project, set up new projects and of course most importantly view all code revisions in case you need to roll back or branch your code. Oh and it's pretty cheap too, only $9/month for 10 users working on up to 3 projects so for a small web team this is ideal. You can go unlimited for $99/month but I presume companies with this kind of need will have it's own IT dept. to set up a subversion system for them.Jon 2:39 PM Permalink
Tuesday, June 19, 2007
Thanks to my Soaperhero colleague Ron Guitar Noize has a brand new 'wicked worn' look. I think it looks amazing and it just shows what you can do with Blogger! I also used sIFR to style the headings the same as here on Freaksauce. It's not 100% perfect yet and I haven't even started up Parallels 3 yet to test the site in IE6, IE7 or FF on PC so I apologise if things are looking a little weird at the moment!Jon 8:20 AM Permalink
Saturday, June 16, 2007
I have just put my latest personal project live, Guitar Lead which is inspired by Adobe's MXNA and is a Guitar News Aggregator gathering feeds from across the web and organising them in one easy to read site. Guitar Lead will be constantly evolving over the next few months but I wanted to put it live rather than procrastinating over design features. Anyway I hope the guitarists out there find it useful!Jon 7:22 PM Permalink
Tuesday, June 12, 2007
I often forget how powerful the date object is in PHP and usually refer to the documentation to remind myself of how to format dates but one of the things I always forget to use is the strtotime() function. This function makes life a lot easier if say for example you need to know what the date was last saturday. This is how you do it:$lastSaturday = date('Y-m-d', strtotime('last Saturday'));
How easy is that?! Ok so what other keywords can I use you might ask?
this Saturday
first Saturday
second Saturday
third Saturday
fourth Saturday
last Saturday
next Saturday
But it doesn't stop there, you can also do:
-3 days last Saturday
+2 days Saturday
-2 weeks Saturday
+2 weels Saturday
Or maybe you just need to know:
yesterday
3 days ago
-3 days
tomorrow
+2 days
You get the picture...
Incredibly useful as you can imagine, for instance if you need to query a table based on how many entries from last monday to next sunday you just use the following variables:
$lastMonday = date('Y-m-d', strtotime('last Monday'));
$nextSunday = date('Y-m-d', strtotime('next Sunday'));
Jon 2:58 PM Permalink
Sunday, June 10, 2007
I have recently started a new blog called Guitar Noize, it's looking a bit bland at the moment but the design is on the way, a friend is helping out with design duties and I'll unveil the result (and the designer) as soon as it's finished. I'm hoping it will be a candidate for CSS Remix or CSS Beauty. Also I hope it willl provide a new port of call for guitarists looking for industry news rather than tabs & lessons.Jon 9:09 AM Permalink
Saturday, June 09, 2007
I just came across a this useful article over at DevShed on PHP 5's simpleXML functions. PHP in my opinion is really lacking in XML support which is weird, but having used coldfusion with xml and seeing how easy it is and I presume it is just as easy in .NET I was suprised at just how difficult it is in PHP. Until recently I have been using PHP4 for almost every project, even though PHP4 is now 7 years old mainly because a lot of hosting providers haven't upgraded but with PHP5 turning 3 I can't see any real reason for this, especially when it is open source?!Anyway, ranting aside the SimpleXML functions certainly take some of the pain out of parsing an XML document and this article has a nice XMLParser class for you to use. One thing to note, if you are using their example:
foreach($nodes as $node){
echo 'Postal address: '.$node->address.'
';
}
and want to know how to save the addresses to an array to use later on I found that you have to cast the $node->address as a string like this:
$addresses = array();
foreach($nodes as $node){
echo 'Postal address: '.(string) $node->address.'
';
}
hope this helps.
Jon 2:08 PM Permalink
