RSS
 

Archive for the ‘Programming’ Category

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");
?>
 
 

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.

 

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
 

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

 

jQuery to toggle read only property

14 Jul

Today I needed to make a change to our web-based software that would allow input to a field, if a specific category was set, and stop entry otherwise.

This is the jQuery that I used to do the job.

if (category == 'Title') {
$('#quoteDetailDescription').removeAttr('readonly');
} else {
$('#quoteDetailDescription').attr('readonly', true);
}
 

A CSV from XML using XSL

10 Jul

I was asked recently to add the ability to export XML data from our web-based facilities management software. This was so our client could write some of their own scripts that would update their Wiki with some information from our database.

This was simple enough, but then I was asked to add CSV functionality to make it easier to code the simple scripts they would be using at their end, which would run nightly via Cron. Once the XML was working, I didn’t want to write another page so instead, I created a XSLT file that transformed the XML in to neatly formatted CSV text.

The code below is all that was really needed (I have chopped it to only use 2 fields). The fields in this example are delimited by double-quotes.

<xsl:for-each select="//media">
"[<xsl:value-of select="@field1"/>]","<xsl:value-of select="@field2"/>"<br/>
</xsl:for-each>