RSS
 

Archive for August, 2009

HAR panel discussing copyright and file sharing

26 Aug

An interesting discussion about file sharing for those interested in DRM, copyright and patents. Most people agree that the business model based around copyright no longer works in the digital age. The question is, what other options do we have, and how many people are going to get sued along the way?

HAR panel from TorrentFreak on Vimeo.

For information about HAR (Hacking at random) please see their website: https://wiki.har2009.org/

 

Creating JSON data from MySQL in PHP

24 Aug

Here is a quick post that demonstrates how to create some neat JSON data from MySQL using PHP. The script is really simple, and has been working well for me over the last few weeks.

If anything is confusing, it might be worth checking out the previous post about creating XML data from PHP. It explains things in more detail and will help if you are not familiar…

//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/json");
 
$jsonArray->page = $page; 
$jsonArray->total = $totalPages;
$jsonArray->records = $recordCount; 
 
$i=0; 
while ($row = $db->fetch_array($countRows)) {
	$jsonArray->rows[$i]['id']=$row['userAccountId']; 
	$jsonArray->rows[$i]['userName']=$row['userName'];
	$jsonArray->rows[$i]['fullName']=$row['fullName'];
	$jsonArray->rows[$i]['accessCode']=$row['accessCode'];
	$jsonArray->rows[$i]['email']=$row['email'];
	$jsonArray->rows[$i]['addedBy']=$row['addedBy'];
	$jsonArray->rows[$i]['addedDate']=$row['addedDate'];
	$jsonArray->rows[$i]['modifiedBy']=$row['modifiedBy'];
	$jsonArray->rows[$i]['modifiedDate']=$row['modifiedDate'];
	$i++; 
} 
 
echo json_encode($jsonArray);

The above code will create the following style JSON output.

{"page":1,"total":1,"records":"3","rows":[
{"id":"1","userName":"demo","fullName":"Demo User","accessCode":"","email":"demo@myexample.com","addedBy":"1","addedDate":"2009-05-02 00:00:00","modifiedBy":"10","modifiedDate":"2009-06-04 00:00:00"},
{"id":"2","userName":"sam","fullName":"Sam Lasagne","accessCode":"","email":"sam@myexample.com","addedBy":"2","addedDate":"2009-05-02 00:00:00","modifiedBy":"2","modifiedDate":"2009-08-02 00:00:00"},
{"id":"10","userName":"adrian","fullName":"Adrian Spaghetti","accessCode":"","email":"adrian@myexample.com","addedBy":"1","addedDate":"2009-05-02 00:00:00","modifiedBy":"2","modifiedDate":"2009-09-01 00:00:00"}
]}
 
 

Using XML in your jQuery to populate input boxes

11 Aug

In a previous post I wrote about creating XML pages with MySQL data, using PHP. In this article, I’ll explain in simple terms how to parse that XML data and insert some of its values in to your page using jQuery.

I’m going to keep this very simple, as there seem to be very few examples of this on the net. It took me a while to work out a neat way of doing this, and I’m sure someone can improve on things. If so – please let me know in the comments.

First, lets set up an example. A very simple example.

Imagine an invoice. 4 boxes at the top of a grid, with a ‘Save’ button that will add the contents of those boxes to the list below. The fields are such as ‘Item code’, ‘Description’, ‘Quantity’ and ‘Unit Price’. The user enters the values and clicks the ‘Save’ button to add that row to the list and clear the boxes, ready for the next row to be added. For the purposes of what we are doing, we will also want a 5th box (which would usually be hidden) to store the ‘Invoice Detail Id’.

Something like this:

Code Description Quantity Price Hidden ID

Creating this application is all quite trivial for someone experienced in PHP. However, what would be nice is to be able to edit each row without having to re-load the page. Well we can do this quickly and easily using jQuery.

Before we start with the jQuery code, we need to have ready a PHP page that will return valid XML with the details of an invoice detail record. Please see my previous post for some help on how to do this.

This is an example output from the getDetailRow.php file.

<?xml version="1.0" encoding="utf-8" ?>
<invoice>
  <invoiceDetail>
    <invoiceDetailId>10</invoiceDetailId>
    <code>CR282</code>
    <description>Software Support</description>
    <quantity>3</quantity>
    <unitPrice>40</unitPrice>
 </invoiceDetail>
<invoice>

Against each row, add a button labeled ‘Edit’, and attach some jQuery code that calls a function called editDetailRow(detailRowId) – as the grid is built, insert this code in to each button with the parameter of that row’s detail ID.

Assuming that there is only ever one result in the XML output, you could use the following code to insert the values in to your input boxes.

<script type="text/javascript">
function editDetailRow(detailId) {
    //first we need to load the XML data for that detail row
    //if the function is a success it will call the function called processDetail
    $.ajax({
       type: "GET",
       url: "getDetailRow.php?detailId=" + detailId,
       dataType: "xml",
       success: processDetail
     });
}
 
function processDetail(xml) {
    //this function gets the results from the xml file
    //and inserts them in to the boxes
    $(xml).find("invoiceDetail").each(function()   {
        $("#code").val($(this).find("code").text());
        $("#description").val($(this).find("description").text());
        $("#quantity").val($(this).find("quantity").text());
        $("#unitPrice").val($(this).find("unitPrice").text());
        $("#invoiceDetailId").val($(this).find("invoiceDetailId").text());
    });
}
</script>

Yes, it really is that simple. You will need to add a reset button, and make sure you check for a value in the invoiceDetailId when you save (to either call an UPDATE or an INSERT) but thats it.

Hopefully you will find this useful and can use this as the basis for much more complicated XML and jQuery hookup’s.