RSS
 

Archive for July, 2009

Creating XML in PHP

28 Jul

Quite often I need to create some XML via PHP, pulling data from a MySQL database.

I have tried various methods but I always come back to this. I like the way that I get full control over doing it explicitly, so thought I would share the code. I am using the MySQL PHP class from Ricocheting.

There are loads of examples out there already on the web, but I hope the code below is very easy to understand as it uses simple PHP code.

<?php
 
//pick up required variables
$userName = $_GET['username'];
$password = $_GET['password'];
 
//include the required files
require_once('config.php');
require_once("Database.class.php");
 
// create the $db ojbect
$db = new Database($config['server'], 
	$config['user'], 
	$config['pass'], 
	$config['database'], 
	$config['tablePrefix']);
 
// connect to the server
$db->connect();
 
//build the query
$query = "SELECT * FROM ".$db->pre."useraccount "; 
 
//build the where statements
$where = "WHERE (userAccountId > 0)";
 
if ($userName) {
	$where = " AND (userName = '" . $db->escape($userName). "')";
}
 
if ($password) {
	$where = " AND (password = '" . $db->escape($password). "')";
}
 
//get the FULL record count
$sql2 = "SELECT count(0) AS theCount FROM ".$db->pre."useraccount $where";
 
$countRecRows = $db->query($sql2);
while ($countRecRow = $db->fetch_array($countRecRows)) {
	$recordCount = $countRecRow['theCount'];
}
 
//execute the main query
$countRows = $db->query($query . $where);
 
//set up the php headers so that the page doesnt cache etc
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . " GMT" );
header("Cache-Control: no-cache, must-revalidate" );
header("Pragma: no-cache" );
header("Content-type: text/xml");
 
//include the page header info
echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
 
//begin the xml data
echo "<useraccounts>\n";
 
//set up the result info
echo  "<total>$recordCount</total>\n";
 
//loop through all the records
while ($row = $db->fetch_array($countRows)) { 
	echo "<useraccount id=\"".$row['userAccountId'] ."\">\n";
 
	echo "<username>";
	echo $row['userName'];
	echo "</username>\n";
 
	echo "<fullname>";
	echo $row['fullName'];
	echo "</fullname>\n";
 
	echo "<accesscode><![CDATA[";
	echo $row['accessCode'];
	echo "]]></accesscode>\n";
 
	echo "</useraccount>\n";
}
 
echo "</useraccounts>";
 
//free the result
$db->close();
?>
 
 

XML Output From PHP

28 Jul

Just a quick one. If you want to output XML from your PHP page you need to set the Headers correctly.

Here is the code you need to include, and it has to be before anything else is printed.

<?php
   header ("Content-Type:text/xml");
?>
 
 

Google Voice. One number. Free. Forever?

28 Jul

googlevoice

Google have launched a new service called “Google Voice” which is basically an application for your PC or your mobile which works via the internet connection, that then behaves like a telephone.

They supply you with a new phone number, for free, and then you add all of your own phone numbers to it. If someone calls your Google phone number – all of your phones will ring. This means that you will only ever need to give out that one number ever again! This is very useful, especially if you work internationally, as a freelancer or change mobiles often.

The service is only available at the moment go Google GrandCentral users, but should be launching soon to the general public.

The only reason I even heard about this service is that Apple have now blocked the sale of the Google Voice application from their iPhone’s App store, claiming the reason: “it duplicates functionality available with the iPhone” – which surely translates as: “Our partners such as O2 and AT&T will be forced to do something about their prices and they really dont like it“…

Google Voice is a service that gives you one number for all your phones, voicemail that is easy as email, and many enhanced calling features like call blocking and screening, voicemail transcripts, call conferencing, international calls, and more.

Google Voice is currently available for GrandCentral users only, but will be open to new users soon. In the meantime, please leave us your email address and we’ll notify you as soon as Google Voice becomes available. To learn more about Google Voice, check out our feature videos.

Im sure there are probably a few problems with the software at the moment, but knowing Google they will work at it until they have an unrivaled service, just like with their Search tool and Apps. Not sure how they will pay for this, but I heard a roumor that they may eventually play adds during calls?

I’m looking forward to trying it out.

 
 

Writing a component for Joomla!

27 Jul

joomla_logo_horz_color_sloganToday, I have written my first component for the Jooma! CRM system.

Following the tutorials on the developer.joomla.org website was actually very straight forward and I have been able to create a simple component of my own. I will be working on this over the next few days / weeks and will release it to the open source community when its done.

For those who haven’t heard of Joomla!, it is a template based web content management system. Once you get the hang of things you can use it to create entire websites in no time at all. It comes with loads of built in functionality and best of all, you dont need to know how to do any programming. Its almost as easy to create a web page as it is to type an email.

 

Bush hid evidence of global warming

26 Jul

I read this story and think it deserves more attention. Hopefully Obama is on the case now, but as you can see, one year of doing nothing makes a lot of difference in situations like this.

From: http://www.guardian.co.uk/environment/2009/jul/26/climate-change-obama-administration

Photos from US spy satellites declassified by the Obama White House provide the first graphic images of how the polar ice sheets are retreating in the summer. The effects on the world’s weather, environments and wildlife could be devastating.

Graphic images that reveal the devastating impact of global warming in the Arctic have been released by the US military. The photographs, taken by spy satellites over the past decade, confirm that in recent years vast areas in high latitudes have lost their ice cover in summer months.

The pictures, kept secret by Washington during the presidency of George W Bush, were declassified by the White House last week. President Barack Obama is currently trying to galvanise Congress and the American public to take action to halt catastrophic climate change caused by rising levels of carbon dioxide in the atmosphere.

One particularly striking set of images – selected from the 1,000 photographs released – includes views of the Alaskan port of Barrow. One, taken in July 2006, shows sea ice still nestling close to the shore. A second image shows that by the following July the coastal waters were entirely ice-free. Read the rest of this entry »

 

Installing Aptana on Ubuntu Linux 9.04

26 Jul
aptana

Aptana Studio IDE

UPDATE: With the latest version of Aptana 2 and Ubuntu 9.10 it seems you dont need to do this hack anymore. Try it without first of all, then if you get problems – apply the fixes as described.

I have been using the Aptana IDE for PHP and Ajax development for a while now on my Windows system, and have decided that it is much better than the standard Eclipse, at least for me. I wanted to start using it on my Linux machine but encountered problems which prevented it from running.

It took me a while to find out how to get it to work, but thanks to Google and some others who have had this problem before me, I now have a working Aptana Studio on my Ubuntu system.

Here is how to get it working:

  1. Download and extract Aptana Studio for Linux (All in one) from here.
  2. Use Synaptic to install xulrunner (version 1.8).
  3. Use Synaptic to install a JRE.
  4. Create a text file (in your home folder etc) which we will use to set up the Aptana environment and execute the application. Call it startAptana.
  5. Paste the text from below in to the new text file, and be sure to change the bottom line so that it matches the path to your Aptana installation.
  6. Make the startAptana file executable.
  7. Double click the startAptana file to start using Aptana, or create menu icon that links to it.
#!/bin/sh
MOZILLA_FIVE_HOME=/usr/lib/xulrunner
if [ $LD_LIBRARY_PATH ]; then
   LD_LIBRARY_PATH=$MOZILLA_FIVE_HOME:$LD_LIBRARY_PATH
else
   LD_LIBRARY_PATH=$MOZILLA_FIVE_HOME
fi
export MOZILLA_FIVE_HOME LD_LIBRARY_PATH
/home/sam/Apps/aptana/AptanaStudio
 

Lack of action on the environment

23 Jul
Guiding a bus through smog

Guiding a bus through 1950s smog

I have recently seen or heard a few things about the 50s, and it has made me think on how progress is made…

In the 1950′s the air in London was so polluted that sometimes it was literally impossible to see your hand in front of your face. The pollution was largely caused by the high volume and low quality of post-war coal used throughout a cold winter, and new diesel fueled buses that had replaced the tram system. Thousands of people died from smog related illnesses and everyone knew that the pollution was a major problem.

In 1956 the government introduced the ‘Clean Air Act’ which among other things set regulations about what fuels could be used and set a minimum height for chimneys. It also forced power stations to be situated away from residential areas and the result was a much cleaner London.

Even though the people already knew the causes of the deaths and illnesses they did very little about them until the government actually legislated.

The same story is true today. People are concerned, scientists even more so. Everyone wants to do their bit but it is impossible without government action. We recycle and try to help where possible, despite knowing we cant make any real difference on our own.

If you need evidence that nothing gets done without government action, I was just sent this link to a story on New Scientist about how the efficiency of US automobiles has hardly changed since the Model-T Ford…. and that is a very very long time ago.

http://www.newscientist.com/article/dn17506-us-vehicle-efficiency-hardly-changed-since-model-t.html

I’m not an expert in this field, nor am I any kind of tree-hugger, but it seems obvious to me that if the government were to legislate that all cars sold after Jan 1st 2015 have a maximum of X emissions (no matter what X is) that the industry would adapt and the target would be met. There could be government incentives in order to make this happen: grants, loans, tax breaks, property, equipment…. whatever it takes to make a change. Once this was in place they could even reduce the threshold by 1% every Y months / years and I suspect that the same auto manufacturers would still be selling vehicles for years to come.

As the saying goes, Where there’s s a will, there’s a way – but with governments of the last few decades it seems there is very little will to do anything real about this issue at all. What is stopping them?

 

A question of leadership

22 Jul
The houses of parliament

The houses of parliament

Watching the house of commons is dull. Watching and listening to most politicians is dull. Do they ever just sit down and think on the really big things anymore – or are they just worried about massaging numbers and reaching their targets?

It has been 40 years since the first manned moon landing, and that’s a long time ago. The last time a human set foot on the moon was December 7th, 1972, and that’s a long time ago too. I understand why we haven’t been back. It is largely unnecessary, and flying in to space is dangerous and costs lots and lots of money.

What I cant understand is why nobody in our government seems to be looking at the bigger picture anymore. It seems there is nothing ever planned or discussed by current governments that resembles inspirational leadership… Something that everyone can get behind.

People will always want to be part of something amazing, and are largely willing to change in order to do so. Governments seem over-scared of upsetting the media, and they are not confident enough about making big, important decisions, especially when they may not go down well with big businesses and the newspapers.

There are very few occasions where true leadership is shown, and even fewer cases of people being genuinely inspired by the government and our so called leaders. We feel that we are largely ignored and mostly think of the government as a controlling, tax-taking authority figure who we have little interaction with, full of rich people who are so different to ourselves that they can not possibly relate to us.

What will it take for the UK government to take a stand on something and give us something to look forward to; something to all be a part of (neo-cons: war does not work);  something to really care about – instead of just telling us what we cant do all the time?

all all
 
1 Comment

Posted in Politics

 

What does Tony Blair do these days?

16 Jul
Tony Blair

Tony Blair

At the time of writing this, Tony Blair is 56. He teaches at Yale and is the worlds highest paid public speaker, getting paid upwards of $400,000 per speech. He also has his Faith Foundation, advises JP Morgan Chase and he may soon be the first president of the European Union. He is definitely cashing in.

Vanity Fair have recorded an interview by Editor, Graydon Carter, with Blair talking quite candidly about his time at Number 10, his relationship with George Bush, Iran, Obama, Terrorism, and what he believes the future holds.

The interview is obviously aimed at an American audience and from his answers, it’s clear that Tony Blair knows this. Now that opinion has largely changed on the last American President, it is nice to see Tony squirm a bit when he is asked if he still stands by the nice things he said about Bush at the time.

Personally I have little respect for Blair. With 2 wars based on lies, millions of protesters ignored and liberties taken away as if they are unimportant – even though they have been in place for hundreds of years.

Graydon Carter asks a few difficult questions, most of which are answered with a typical Blair smile, but he also piles on the compliments which makes the whole thing feel too set up. It would be nice to see someone ask him some really tough questions – how about the 45 minutes to launch lie; about Dr. David Kelly’s death; about Bush’s first election ‘win’; about Robin Cook, Claire Short and other so called “rebel” Labour MPs and about the ever increasing use of ‘police state’ tactics and CCTV presence in our country. Anyway, rant over….

Watch the full interview here.

 
 

Notepad2 – Text Editor, replacement for Notepad

15 Jul

NotepadI have recently started using Notepad2 again.

Its a really fast, small, simple text editor -  just like Notepad but with a few added features – such as syntax highlighting. Very useful for editing SQL files, PHP scripts and so on.

http://www.flos-freeware.ch/notepad2.html