Archive for November, 2010


Adding line breaks to a push button

Have you ever wanted to force the text on an <input> button onto an other line?

So did I today, and this is how:




Converting stored MySQL dates into UK format

You will no doubt be aware that when you save a date in a MySQL database, it must be in the form of Y-m-d.

I had some difficulty wrestling with the date() section of the PHP manual , and experimented for a few minutes on trying the use the SQL SELECT statement itself to get the date data from the database in my preferred UK (d/m/Y) format without sucess.

I was quite pleased therefore when my perseverance paid off, and I was able to work out how to solve my problem with a single line of PHP code:

$ukdate = date('d/m/Y',strtotime($mysqldate));

Tags: , ,

Batch file to perform SSH commands and transfer files from Windows to Linux

At my place of work, I routinely have to update firmware and database files from our office Windows PCs to ARM linux devices.

This used to involve connecting to the mini PC using PuTTY, killing a process, then logging in using an FTP client to transfer files across, finally rebooting the Linux device.

That is, until I automated the process with plink.exe and pscp.exe (available from the PuTTY download site) and a Windows batchfile:

@set /p IP="Enter the IP address of the Linux device, and press enter:"
@echo Killing processx
@C:\Path\plink.exe -ssh -pw password user@%IP% killall processx
@ping %IP% -n 2 -w 1000 > nul
@ping %IP% -n %1% -w 1000> nul
@echo Transferring FileX
@C:\Path\pscp.exe -scp -pw password C:\Path\FileX user@%IP%:/linux/path
@echo Rebooting Controller at %IP%
@C:\Path\plink.exe -ssh -pw password user@%IP% reboot
@echo Your Linux device will be back online shortly.

In this example, you’d have to have your batch file, FileX, plink.exe and pscp.exe in the C:\Path\ directory on your Windows PC. Obviously substitute your real SSH username and password in for ‘user’ and ‘password’.


Curved DIV borders

This is so easy!  I used to use a complex set images for top-right, top-left, bottom-right, bottom-left etc.

Granted these 2 lines of CSS will not produce shadow effects or anything fancy, but the results are pleasing nevertheless.

-moz-border-radius: 20px;
border-radius: 20px;

The -moz prefix is needed to allow this to work in FireFox.


PHP pagination of search results

Displaying data from a database in a webpage is something every web developer needs to do very frequently. I remember my early attempts at displaying a certain number of records per page like a Google search to be immensely frustrating and difficult to get working.

So this is an instructional example I’ve put together,  which is a mishmash of various example scripts I’ve found over the years, and butchered, adapted and refined to fill my requirements. Feel free to use it, or parts of it to help you:

//Get the page number (if the search has already been done) and the users input from the search form (q)
 $pagenum = @$_GET['pagenum'];
$var = @$_GET['q'];

//Slight protection from sql injection

$trimmed = trim($var);


if ($trimmed=="")
echo "You didn't enter anything to search.";
// Connect to your Database
mysql_connect("localhost", "dbname", "dbpass") or die(mysql_error());
mysql_select_db("dbname") or die(mysql_error());
//This checks to see if there is a page number. If not, it will set it to page 1
if (!(isset($pagenum)))
$pagenum = 1;
//Here we count the number of results
//Edit $data to be your query
$data = mysql_query("SELECT * FROM dbtable WHERE value1 LIKE '%$trimmed%' || value2 LIKE '%$trimmed%' ORDER BY value1") or die(mysql_error());
$rows = mysql_num_rows($data);
//If no rows are returned, tell the user
if ($rows==0)
echo "Sorry, your search: &quot;" . $trimmed . "&quot; returned no results";
//This is the number of results displayed per page
$page_rows = 8;
//This tells us the page number of our last page
$last = ceil($rows/$page_rows);
//This makes sure the page number isn't below one, or more than our maximum pages
if ($pagenum < 1)
$pagenum = 1;
elseif ($pagenum > $last)
$pagenum = $last;
//This sets the range to display in our query
$max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows;
//This is your query again, the same one... the only difference is we add $max into it
$data_p = mysql_query("SELECT * FROM dbtable WHERE value1 LIKE '%$trimmed%' || value2 LIKE '%$trimmed%' ORDER BY value1 $max") or die(mysql_error());
echo "<td align='center'>";
// This shows the user what page they are on, and the total number of pages
echo "$pagenum of $last<br />";
// First we check if we are on page one. If we are then we don't need a link to the previous page or the first page so we put in our "faded" buttons, (no links). If we aren't then we generate links to the first page, and to the previous page.
if ($pagenum == 1)
echo "<img src='../resources/fadefirst.jpg' width='50' alt='First' /><img src='../resources/fadeprevious.jpg' width='50' alt='Previous' />";
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=1&dvalue=$dvalue&q=$trimmed'><img src='../resources/first.jpg' width='50' alt='First' title='First' /></a> ";
echo " ";
$previous = $pagenum-1;
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous&dvalue=$dvalue&q=$trimmed'><img src='../resources/previous.jpg' width='50' alt='Previous' title='Previous' /></a> ";
//just a spacer
echo "&nbsp;&nbsp;&nbsp;";
//This does the same as above, only checking if we are on the last page, and then generating the Next and Last links
if ($pagenum == $last)
echo "<img src='../resources/fadenext.jpg' width='50' alt='Next' /><img src='../resources/fadelast.jpg' width='50' alt='Last' />";
else {
$next = $pagenum+1;
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$next&dvalue=$dvalue&q=$trimmed'><img src='../resources/next.jpg' width='50' alt='Next' title='Next' /></a> ";
echo " ";
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last&dvalue=$dvalue&q=$trimmed'><img src='../resources/last.jpg' width='50' alt='Last' title='Last' /></a> ";
//Set up the headers of the table to display your query results
echo "</td></tr></table></div>
<div><table border='1' width='100%'>
<tr align='center'>
<th width='20%'>Image</th>
<th width='20%'>Item1</th>
<th width='20%'>Item2</th>
<th width='20%'>Item3</th>
<th width='20%'>Item4</th>
//This is where you display your query results
while($info = mysql_fetch_array( $data_p ))
echo "<tr align='center'>";
echo "<td width='20%'> <img src ='$image'> </td>";
echo "<td width='20%'> $v1 </td>";
echo "<td width='20%'> $v2 </td>";
echo "<td width='20%'> $v3 </td>";
echo "<td width='20%'> $v4 </td>";
echo "</tr>";
echo "</table>";
// Same Paigation display as earlier - so the user has next/previous/first/last page navigation above and below the table of search results on each page
echo "<div align='center'>";
if ($pagenum == 1)
echo "<img src='../resources/fadefirst.jpg' width='50' alt='First' /><img src='../resources/fadeprevious.jpg' width='50' alt='Previous' />";
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=1&q=$trimmed'><img src='../resources/first.jpg' width='50' alt='First' title='First' /></a> ";
echo " ";
$previous = $pagenum-1;
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous&q=$trimmed'><img src='../resources/previous.jpg' width='50' alt='Previous' title='Previous' /></a> ";
echo "&nbsp;&nbsp;&nbsp;";
if ($pagenum == $last)
echo "<img src='../resources/fadenext.jpg' width='50' alt='Next' /><img src='../resources/fadelast.jpg' width='50' alt='Last' />";
else {
$next = $pagenum+1;
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$next&q=$trimmed'><img src='../resources/next.jpg' width='50' alt='Next' title='Next' /></a> ";
echo " ";
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last&q=$trimmed'><img src='../resources/last.jpg' width='50' alt='Last' title='Last' /></a> ";
echo "<br />$pagenum of $last</div>";


Tags: ,

Change the style of font inside a search textbox

I wanted to emulate certain sites where the search box is populated with “Search…” in grey coloured italic font, but as soon as you click on it and start typing, your query appears in black normal font.

The examples I was able to find were horribly complex and I was unable to get my head around them in the time available, so as an interim measure to get the effect I wanted I wrote my own:

<input type="text" name="q" style="width: 105px; font-style:italic; color:grey;" value="Product Search..." onFocus="this.value = '';'normal';'black';" onblur="this.value = 'Search...';'italic';'grey';"/>

The drawback to this is that you cannot use a search button to submit the form – as soon as focus of your browser leaves the textbox (i.e. when you click the button) the text box reverts back to the value of “Search…“. The button has been removed from the form where I’ve used this so users must press the enter key on their keyboard to submit their query.

Interestingly, even though people are searching all the time, and this has been live for a day or so now, I’ve not had a single complaint from a user that the search button has gone….

Tags: , ,

Saving individual pages from a .pdf, as a new .pdf

Ever needed to save a specific page of a .pdf document as a new, separate .pdf?

Apparently you can do it if you purchase a full Adobe software licence, or you could use this free online tool:


Automatic MSSQL 2008 Backup Batchfile

I was asked to create a simple process for backups to a MSSQL database. After a little searching around online I came up with a Windows batchfile, and then used scheduled tasks to run it weekly:

set ToDaysDate=%date:~3,2%%date:~0,2%%date:~6,4%

"C:\Program Files\Microsoft SQL Server\90\Tools\binn\sqlcmd.exe" -U sa -P
DISK='C:\Backups\DatabaseName%ToDaysDate%.bak' WITH FORMAT"

This saves a backup file in the folder you specify with the data appended – i.e. DatabaseName161110.bak


JavaScript News Ticker

A site I have been working on has had a Flash banner with scrolling text for a couple of years. The problem is that the page took longer than it should have to load, and visitors couldn’t see the banner at all (company logo etc.) if they didn’t have a flash plugin.

Luckily I was able to use this neat little JavaScript at the top of the main body of the page, and revert back to a standard image for the banner.

I modified it slightly so that it would scroll a little slower, the javascript was contained in an external .js file and I could style it using CSS.

Other than that the original script was used:

My version is at


PDO/SQLite connection

I have been working for a while on a project involving running a PHP web application on on an ARM linux mini PC. There was an existing application on the PC whose SQLite database I needed to manipulate, to feed new values into the program from a web interface. The hardest thing by far was cross-compiling PHP to run in the ARM environment to include --enable-pdo --with-pdo-sqlite, but that would take too long to go into here.

I was able to collect and display data from the SQLite database using the following PHP code:

$connect = new PDO("sqlite:/path/to/sqlite.db");

$sql = "SELECT * FROM sqlite_table";

foreach ($connect->query($sql) as $info)


echo "$val1";

Tags: , , ,