php

Basic query using ColdFusion or PHP

Following on from our previous post showing the process of removing spaces from strings with both ColdFusion and PHP, this example shows the process for querying some data and returning it to the browser as an unordered list. Again, a very basic example but one which shows how these two technologies handle the same thing.

so…ColdFusion.

in the ColdFusion Administrator set up your datasource – DATA & SERVICES > Data Sources then type in the name of your datasource(we’ll call it something original…so, “test” it is then!), select the correct Driver and press “Add”, then just type in the name of your DB, the server(probably localhost) and the username and password thats your DB available to ColdFusion on your server, all you need to remember while writing your app is the datasource name.

A sample Query looks like so:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<!--- connect to your datasource and select what you want:--->
<cfquery name="listData" datasource="test">
    select dataOne, dataTwo, dataThree
    from data
</cfquery>

<!---and output it in an unordered list:--->
<cfoutput>
<ul>
    <li>#dataOne#</li>
    <li>#dataTwo#</li>
    <li>#dataThree#</li>
</ul>
</cfoutput>

job done!

and so over to PHP…

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?php

// first we need a connection to the DB
mysql_connect("localhost", "username", "password") or die(mysql_error());
mysql_select_db("myDatabase") or die(mysql_error());

// retrieve the required data
$getData = mysql_query("SELECT dataOne, dataTwo, dataThree FROM data")
or die(mysql_error());

// and output into an unordered list
echo "<ul>";
while($row = mysql_fetch_array($getData))
{
    echo "<li>".$row['dataOne']."</li>";
    echo "<li>".$row['dataTwo']."</li>";
    echo "<li>".$row['dataThree']."</li>";
}
echo "</ul>";

?>

and there you have it, the same query returning the same data in both ColdFusion and PHP.

Add comment August 18th, 2008

Removing spaces from a string with ColdFusion or php

This is one of those little problems that pop up frequently and to show how it’s done in CFML as well as our other house language PHP, we’ve put these examples up for you. This method is using a regular expression.

To remove ALL the spaces from a string:

1
2
3
4
5
6
<!--- set up a test variable --->
<cfset mySpaceFilledString = " east coast interactive love coldfusion " />
<!--- remove ALL spaces from the string --->
<cfset mySpaceFreeVariable = ReReplace(mySpaceFilledString, "[[:space:]]","","ALL")>
<!--- check it works --->
<cfdump var = #mySpaceFreeVariable#>

The above returns “eastcoastinteractivelovecoldfusion” – all spaces have been removed

And the PHP version

1
2
3
4
5
6
7
8
<?php
// set up a test variable
$mySpaceFilledString = " east coast interactive love php ";
// remove all spaces from the string
$mySpaceFreeVariable = str_replace(" ", "", $mySpaceFilledString);
// check it works
echo $mySpaceFreeVariable;
?>

The above returns “eastcoastinteractivelovephp” – again, all spaces have been removed

To simply remove the spaces at the start and end of a string:

1
2
3
4
5
6
<!--- set up a test variable --->
<cfset mySpaceFilledString = " east coast interactive love coldfusion " />
<!--- remove spaces from start and end of the string --->
<cfset mySpaceFreeVariable = Trim(mySpaceFilledString)>
<!--- check it works --->
<cfdump var = #mySpaceFreeVariable#>

The above returns “east coast interactive love coldfusion” – spaces at the start and end have been removed only

And the PHP version

1
2
3
4
5
6
7
8
<?php
// set up a test variable
$mySpaceFilledString = " east coast interactive love php ";
// remove spaces from start and end of the string
$myTrimmedVariable = trim($mySpaceFilledString);
//check it works
echo $myTrimmedVariable;
?>

The above returns “east coast interactive love php” – again, all spaces have been removed from the start and end only.

Add comment August 12th, 2008

Timelapse New Years Eve Videos

Here’s some videos made up of frames taken over NYE 06>07 from Aberdeen Council’s 3 webcams.

A little php script grabbed an image each minute and numerically numbered them to make it easy to import as an image sequence into flash video encoder. The ON2 codec allows for quite a big image at 15 frames per second at a reasonable bandwidth (440kbps), albeit with not much movement happening to cause video break up.

I might extend the script further to automatically grab sun-up/sun-down times from the bbc’s weather xml feed, then punt the resulting images into a short video clips courtesy of ffmpeg or similar, though I guess fading in a grey image in the background would do for 90% of the time in Aberdeen.

Add comment March 21st, 2007

Using FFMPEG for online video conversion – introduction

It seems like it’s been part of the fabric of the internet now for ages but in reality the mass acceptance of video use in webpages didn’t occur until early 2006 with the likes of Youtube gaining the publics attention, coinciding with a massive public uptake in broadband connectivity.

Now many people want the capability to add videos easily to their site .. but this demand for that technical capability is often not matched by an understanding of the potential difficulties in implementation, particularly when faced with limited resources in development or hosting provision. The variety of sources which produce video, from 100’s of mobile phone models to webcams to dv-cams, combined with different operating systems, software, codecs etc could potentially be confusing. So how do video sites deal with the all the potential options in their user input?

The battle for the dominant format for web video consumption has been won – Flash video scores very highly on cross platform capabilities, browser compatability and general public ubiquity and acceptance. It also is highly maleable in its ability to be integrated into customised interfaces and branded scenarios unlike more traditional video contenders such as quicktime and windows media, which are more suited when the use case of the media is to be downloaded and viewed on the desktop rather than embedded within a web page.

To convert user uploads to flash video (flv) we have a number of options – we could use Sorenson Squeeze installed on a (windows) server and use its ‘watch folder’ capability to convert files, or similar products such as On2 Flix SDK. These work great ‘out of the box’ but incur ongoing licence fees that are quite expensive except if you are using them at an enterprise level.

On the other hand FFMPEG is an entirely free and open source library that will convert many popular video file formats to flv, but it does require more developer effort to get to a useable solution. Like many open source projects it has its foibles (the documentation is scattered and patchy, the mailing list obtuse and unhelpful, there are complications brought by dependance on other external libraries) but if you persevere you can create a cross platform application that can be hosted on windows or linux servers without any licence fee cost.

The first step in creating a solution is to visualise the processes involved:

upload file-> analyse format of uploaded file -> convert to flv -> display

In stage 2 I’ll describe how to implement these stages using php, an ffmpeg binary and a flash video player.

2 comments March 7th, 2007

Calendar

August 2017
M T W T F S S
« Feb    
 123456
78910111213
14151617181920
21222324252627
28293031  

Categories

Most Recent Posts

Feeds