PHP Array randomization

I’m a bit perplexed why you can sort an array with keys in PHP and you can randomize an array with no keys, but you cannot randomize an array with keys. Weird…

If you have to randomize an array with keys, here is a way to do it. Would have been nice to have been able to simply say:

shuffle($arrayWithKeys);

but thats not to be as you would lose the keys.

So what you need to do is:

//Make an array without keys of the keys of your associative array
$keys = array_keys($arrayWithKeys);

//Shuffle the keys up
shuffle($keys);

//New array which is the one you would use or return
$newArrayWithKeys = array(); 

foreach($keys as $k) {

    //The array items would be packed into the new array in the order that it was shuffled.
    $newArrayWithKeys[] = $arrayWithKeys[$key];

}

Thanks to Sajith for the tip!

Sharing is caring:These icons link to social bookmarking sites where readers can share and discover new web pages.
  • BlinkList
  • blogmarks
  • del.icio.us
  • digg
  • Fark
  • Ma.gnolia
  • muti
  • NewsVine
  • Reddit
  • Shadows
  • TailRank
  • YahooMyWeb

PHP, Programming Stii

ViM cheatsheet

I found this incredibly useful visual ViM cheatsheet while tracking “vim” on Twitter (Thanx to Rafiq!).

Funny thing is you get so damn used to the keystrokes that once you go back to another text editor you constantly end up putting things like /searchstr or :wq or :24, 60 w newfile.php, etc… into your source code file. Its quite funny after a while. Nope, I’ll stick with ViM, thank you.

To see the image lifesize, click it!

ViM cheatsheet

Another interesting discovery was this vimrc file that will apparently turn ViM into a (mainly) Ruby on Rails IDE. I have not had the time to check it out, but if RoR is your thing, it might be worthwhile to give it a shot!

Sharing is caring:These icons link to social bookmarking sites where readers can share and discover new web pages.
  • BlinkList
  • blogmarks
  • del.icio.us
  • digg
  • Fark
  • Ma.gnolia
  • muti
  • NewsVine
  • Reddit
  • Shadows
  • TailRank
  • YahooMyWeb

Kick-ass Tools, Programming Stii

PHP Smarty date conversion specifiers

Oct 07

I’m not a big fan of Smarty Templates in PHP. This is only the second project in a short space of time I’ve been required to use it due to existing code already implementing Smarty. The project is on a hectic deadline and we’ve been held up by unforeseen circumstances, but I know its no excuse! I assumed (yes… I know what they say about assumptions…) that Smarty’s date specifiers would be the same as PHP’s. *NOTE* That is not the case. PHP got its own style of specifiers while Smarty uses C style specifiers. It is also exactly the same as MySQL’s date specifiers.

Let say you get a datetime value out of your MySQL database and want to print the date as 5 Oct 2007 17:05. The MySQL format is 2007-10-05 17:05:00.

Now in normal PHP it would be:

echo date('j M Y H:i', strtotime($mysqlDate)); //$mysqlDate is the date from the database

Here is the same thing in Smarty:

{$mysqlDate|date_format:"%e %b %Y %H:%M"}

In Mysql it would look like this:

SELECT DATE_FORMAT(`date_column`, '%e %b %Y %H:%M') AS newDate FROM the_table WHERE ...;

I guess one could argue that Smarty should adapt to PHP. If you consider that PHP is developed in C, would it not make more sense for PHP to use C style specifiers?

Here is a list of specifiers for Smarty:
http://www.phpfreaks.com/smarty_manual/page/language.modifier.date.format.html

Here is PHP’s list:
http://www.php.net/manual/en/function.date.php

Here is for MySQL:
http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html

Sharing is caring:These icons link to social bookmarking sites where readers can share and discover new web pages.

PHP, Programming Stii

Scriptaculous / Prototype stupidity

Oct 06

Sometimes, just sometimes, I feel like running into a wall. Head first. If only I was not so unfit. I made a stupid bloody Prototype Ajax mistake. Get so damn irritated with myself! Don’t make the same bloody mistake! My head is clearly at the rugby. That will teach me.

So what I did was the following:

function callAjax() {
    ...
    var myAjaxCall = Ajax.Updater('thediv', '/the/url', {onComplete: function(){ new Effect.Highlight('thediv');}});
    ...
}

Firebug gave me the following error:

this.initialize has no properties
[Break on this error] this.initialize.apply(this, arguments);

This means (translated into the “YouStupidGeek” dialect):

“You stupid dumbass! How the hell can you forget the new keyword before the Ajax.Updater !”

So, don’t forget that… and now its time to see New Zealand get knocked out the World Cup by France!

Sharing is caring:These icons link to social bookmarking sites where readers can share and discover new web pages.

Programming, ajax, javascript, web development Stii

Python could improve its documentation

Oct 03

Python’s documentation is confusing and frustrating. As great as the language is, their documentation could be a lot better. Not the content of their documentation, but the navigability and structure thereof.

If you compare Python’s documentation to PHP’s documentation, you would see why. PHP got everything in one place. One book so to speak. Python got it all over the show in different “books”. There is nothing wrong with this apart for the fact if you go into the Language Reference book, go to a section in this book, there is no link back to other books like the Library Reference. A couple of links somewhere on every page linking to the other books would work wonders. The module reference is awesome, but as soon as you go into a module’s reference page, you need to click the modules button at the top or bottom to get back to a list of all the modules. Why not simply have a list of modules down the side as a menu, much the same as that PHP does with its function references?

One really frustrating thing is there is no search on hand in the Python docs. PHP got a search box on every documentation page and nothing is faster than simply entering the search phrase if you need to check something on a specific function. Python’s search is powered by Google and very accurate and effective, but you have to go to a separate search page, which is like the rest of the navigation in the documentation, pretty unreachable once you’ve left the page.

Another “nice-to-have” which PHP does and that would be really handy in Python’s documentation is the user examples/comments. PHP’s documentation is not at all as complete as Python’s is, but that simple little feature that allows users to leave some examples or comments makes the world of difference.

This might be a bit pedantic and petty, but it would just make lives simpler if it was done better! Yes, there are workarounds for almost all of the above issues by opening arrays of new browser tabs/windows. For now, workarounds will have to do.

Sharing is caring:These icons link to social bookmarking sites where readers can share and discover new web pages.

PHP, Python Stii

Rails vs. Django - very funny!

Sep 30

This is funny. Seriously, I enjoyed this! It might be a rip-off of the “I’m a Mac, I’m a PC” ads, but it is still very funny nonetheless! I’m sure Rails and Python folks would like this vid by the Railsenvy guys.

Sharing is caring:These icons link to social bookmarking sites where readers can share and discover new web pages.

Funnies, Python, Ruby (on Rails) Stii

Going oldschool with VIM

Sep 20

Lets face it, there is certainly no shortage of editors and IDE’s out there today. One would think that command line editors like VI or VI iMproved, Nano, Emacs, etc. should die a really sudden, quick and painless death. Yet, you’d be sorely mistaken. These editors will stay the editors of choice on any *nix server that does not have a GUI installed. Plus what could be simpler than typing
sudo vi /etc/apache2/httpd.conf
to edit system configuration files?

Me and Charl also had a discussion about how hardcore old school geeks swear by it, doing all their programming in these editors. You’d wonder why. I sure did, so I whipped out vimtutor and sharpened the ol’ VIM skills. Now, working with it more and more and getting used to all the shortcuts and keystrokes, it is becoming clearer by the day why it remains the editor of choice for hardcore geeks. It makes it fast! I’m by no means a hardcore geek, but I do see why they like it.

Admittedly, it is quite a learning curve, but once you are used to the commands its much quicker raking in those keystrokes than it is clicking all over some editor to get your rocks on!

It has got really good syntax highlighting and just in case you are afraid you will sorely miss your IDE’s tabbed open document interface you can easily open as much terminal windows as needed to suit your needs. All in all, I think I’ll stay a little longer with VIM than what I expected…

Multiple windows with VIM
Multiple VIM windows

PHP syntax highlighting
VIM PHP syntax highlighting

Python syntax highlighting
VIM Python syntax highlighting

Sharing is caring:These icons link to social bookmarking sites where readers can share and discover new web pages.

Kick-ass Tools, PHP, Programming, Python Stii

Happy Programmers day!

Sep 13

Today is unofficially Programmers day (thanx, Neville!). Why? Its the 256th day of the year. So in true Programmers spirit:

How many people can celebrate Programmers day if only you and DEAD people can celebrate Programmers day?

Here is a link to the unofficial Programmers Day web site: http://programmerday.info/

Blogged with Flock

Tags:

Sharing is caring:These icons link to social bookmarking sites where readers can share and discover new web pages.

Funnies, Programming Stii

Coming up for air…

Jul 03

After lots of blood, sweat and tears this past two weeks, it seems there is a light at the end of the tunnel and it is more than likely the smouldering coals of the fires we’ve estinguished… I’m starting to breath again, at least! This, I’m afraid, might be very short lived though.

I’ve had a great “crash course” in Python. What an enjoyable experience! Oh, and much thanx to Neville from Muti for all the help and patience! Appreciated mate!

Lots of ppl also told me they’re finding it difficult to open this blog, cause its sloooooow. So I decided since I’m looking to brush up on some Python web developing using CherryPy, I’d move it to a host that is both fast for everyone and supports decent CherryPy/Python. The blog will of course stay Wordpress for now… If I next have a holiday that will allow me to play, I’ll try my hand at re-writing this blog in Python.

CherryPy logo

Python logo

technorati tags:,

Sharing is caring:These icons link to social bookmarking sites where readers can share and discover new web pages.

Python, web development Stii


RSS Feed
Afrigator
Techleader

Recent Posts

My Posse

Filed in

Past Stuff

Meta Stuff