Latest articles

Preventing a loop in Mongrel when compiling Ruby with OCRA

2011-11-10 23:08:07
If you are using OCRA and Mongrel with Ruby and can't compile because of an infinite loop, the simplest way is to prevent the loop running in the first place.

Credit where it is due. I wouldn't have known what to do without StackOverflow

But due to my own lack of understanding, I was wondering how on Earth to go about hacking Mongrel to break the loop. Then I thought about it sensibly. For anyone else who hasn't realised it straight away, this is probably the best option.

if ENV.has_key?('OCRA_EXECUTABLE') == true #will only run on the OCRA compiled build
server = Mongrel::HttpServer.new("127.0.0.1", "2000")
server.register("/", MyServerClass.new)
server.run.join
end

Creating select_one_hash() and select_all_hash() methods for Ruby DBI

2011-11-08 21:04:43
In using Ruby's DBI, I was looking for a way to return the results as a hash, rather than an array, to help me manage the result.
fetch_hash() was useful, in place of fetch()

But then I discovered select_one() and select_all() that are great substitutes for execute(), fetch() and all the associated code.

Unfortunately, there isn't a similar alternative for fetch_hash() - well not one that I have found - I must admit that I have not read all of the documentation and Ruby documentation isn't always complete or up-to-date. As a developer, I understand the pain of having to agonise over explaining the intricacies of something that you built to 'just work'.

I have consulted the documentation at http://ruby-dbi.rubyforge.org/rdoc/index.html, shuffled the code into new methods and come up with something that I believe (but cannot guarantee will work). It's not fully tested, but make of it, what you will.

As a word of caution, there is no guarantee that this won't break future versions of the DBI, especially if those method names are used; so you should test it after every update and you may also want to assign unique method names; e.g. YourName_select_one_hash



class DBI::DatabaseHandle
def select_one_hash(stmt, *bindvars)
raise InterfaceError, "Database connection was already closed!" if @handle.nil?
row = nil
execute(stmt, *bindvars) do |sth|
row = sth.fetch_hash
end
row
end

def select_all_hash(stmt, *bindvars, &p)
raise InterfaceError, "Database connection was already closed!" if @handle.nil?
rows = nil
execute(stmt, *bindvars) do |sth|
if block_given?
sth.each(&p)
else
rows = sth.fetch_all_hash
end
end
return rows
end
end

class DBI::StatementHandle
def fetch_all_hash
raise InterfaceError, "Statement was already closed!" if @handle.nil?
raise InterfaceError, "Statement must first be executed" unless @fetchable

cols = column_names
fetched_rows = []

begin
while row = fetch_hash do
fetched_rows.push(row)
end
rescue Exception
end

@handle.cancel
@fetchable = false

return fetched_rows
end
end

10 Windows keyboard shortcuts you possibly didn't know about

2009-09-22 12:25:52
Keyboard shortcuts are a vital time-saver. Here are ten that you possibly didn' already know about.

Active window Screenshot
ALT + Prt Scn
Holding the Alt and Print Screen buttons simultaneously will enable you to take a screenshot of just the active window, rather than the whole screen. This avoids the need to crop some images.

Lock Computer
Windows + L
Holding the Windows key and the letter L will automatically lock your computer. This protects you if you are leaving your PC unattended, because you will need to enter your password to get back in.

Open Windows Explorer
Windows + E
Holding the Windows key and the letter E will open Windows Explorer. This is basically like My Computer, but the 'Folders' Explorer Bar is turned on by default.

Searh for files and folders
Windows + F
Holding the Windows key and the letter F will open the Windows search feature.

Minimise all windows
Windows + M
Holding the Windows key and the letter M will minimise all open windows. This is great if you need instant access to your desktop. The Show Desktop command (Windows key and the letter D) will also achieve this.

Undo minimise all windows
Windows + SHIFT + M
Holding the Windows key, the Shift key and the letter M will restore any windows that were minimised by the "Minimise all windows" shortcut. The Show Desktop command (Windows key and the letter D) works as a toggle, so can be used to undo the Windows + D shortcut, but it cannot be used to undo Windows + M.

Show System Properties
Windows + BREAK
Holding the Windows key and Break key simultaneously will open the System Properties. This avoids going through the Control Panel or right-clicking My Computer to choose 'Properties'.

Rename file
F2, when a file is selected
Holding the F2 key when you have a file selected within Windows Explorer, will enable you to rename the file.

Prevent autorun
SHIFT, when inserting a CD
Holding the Shift key whilst you insert a CD and the disc is loading will prevent the disc's autorun from being activated.

ALT + F4
Shut down Windows
Holding the Alt key and the F4 key simultaneously will enable you to shut down Windows. You may already know that when you have a program open, Alt + F4 will close the application for you. However, you are less likely to know that the same principle can be applied to shut down Windows. You do this by going to your desktop and holding Alt + F4.

Randomly Selecting Images with PHP

2009-09-07 19:17:18
Have you ever seen an image on a web page and wondered how it is randomly replaced with a different one when you click "reload" in your web browser?

This is a neat little trick that can be done in PHP and it is so easy. This article will explain how to do it.

First of all, you will need to write the PHP part of the code, which will randomly select the image. After that, you will place the PHP variable into the HTML.

<?
$image_array = array("images/1.jpg", "images/2.jpg", "images/3.jpg"); /* the paths of the images - to add extra paths, just follow the existing pattern: encased in quotes and separated by a comma */
$selection = $image_array[array_rand($image_array)]; /*this is the random choice of array value*/

$random_image = "<img src='".$selection."' />";
?>


Once that is done, you will need to add the variable $random_image into the page, like so:

<html>
<body>
<? echo $random_image; ?>
</body>
</html>


Once you have used this code, try a few page refreshes. You should see that it displays a random image from the selections.

How to create a simple contact form in PHP

2009-08-30 19:02:52
An online contact form is quite a handy feature for your website. It can be used as an additional way for your visitors to contact you, and if you don't feel like displaying your email address for those nasty spammers to get hold of it, you don't have to. This article explains how you would create a simple contact form for your website.

To create an online contact form, you will need two parts.
1) The HTML for the form that is used by your visitors.
2) The PHP server-side code that interprets the form and sends it as an email.

Your online contact form will look something like this:

<form method='post'>
<table>
<tr>
<td>
<label for='from_name'>Your name: </label>
</td>

<td>
<input name='from_name' type='text' />
</td>
</tr>
<tr>
<td>

<label for='from_mail'>e-mail address: </label>
</td>
<td>
<input name='from_mail' type='text' />

</td>
</tr>
<tr>
<td>
<label for='message'>Message:</label>
</td>
<td>

<textarea name='message'></textarea>
</td>
</tr>
<tr>
<td>
</td>
<td>
<input name='submit' id='submit' type='submit' value='Send message' />

</td>
</tr>
</table>
</form>


So let's take a closer look at the key parts of this:

<form method='post'>

This opens up the form so that the web page knows to treat everything inside as part of the form.
method='post' is used so that the data is sent behind the scenes to the server using the HTTP POST method. The alternative would be to use the HTTP GET method which would make the data visible within the URL. This can be messy, as the user's URL will contain the whole text of the message along with all other submitted information. Also there is a limit to how much data can be sent through GET, whereas POST is not limited.

<label for='from_name'>Your name: </label>

This is a handy little piece of HTML. It will allow the user to click on the words "Your name: " and it will activate the associated input box.

<input name='from_name' type='text' />

This is where the name is entered into the form.

<textarea name='message'></textarea>

This is where the message is entered. Unlike the inputs, this will appear as a box with multiple rows, rather than a single line.

<input name='submit' id='submit' type='submit' value='Send message' />

This is the Submit button. It is a special type of input that will submit the HTML form, according to its method. In this case we chose the POST method. The value will display the words "Send message" onto the button.


Great! That's the HTML sorted. Now onto the PHP...

<?
if($_POST["submit"] == "Send message")
{
$to_address = "your@address.here";

$email_subject = "Subject of the emails";

$email_headers = "Content-type: text/html; charset=iso-8859-1\n";
$email_headers .= "From: ".$_POST["from_name"]."<".$_POST["from_mail"].">\n";

if(mail($to_address, $email_subject, $_POST["message"], $email_headers) == true)
{
$message_confirmation = "Your message has been sent<br/>";
}
else
{
$message_confirmation = "Your message could not be sent<br/>";
}
}
?>



<?

This starts the PHP code

if($_POST["submit"] == "Send message")
{

This is only called if the user clicked the Submit button with value "Send message"


$to_address = "your@address.here";
$email_subject = "Subject of the emails";

Defines the email address that messages will be sent to and the subject that will be given to these messages

$email_headers = "Content-type: text/html; charset=iso-8859-1\n";

Choose the character set. This is the default character set which you would normally use in English language. The \n is the newline character.

$email_headers .= "From: ".$_POST["from_name"]."<".$_POST["from_mail"].">\n";

This sets the message as from the person and email address specified. Once again \n is used as a newline character.
.= is used instead of equals on its own, because the header is being added to the existing $email_headers variable.

if(mail($to_address, $email_subject, $_POST["message"], $email_headers) == true)

This runs the mail function and also an if statement that checks if the mail() function was successful or not

{
$message_confirmation = "Your message has been sent<br/>";
}

If the mail function was successful, the above variable is assigned

else
{
$message_confirmation = "Your message could not be sent<br/>";
}

If the mail function failed, the above variable is assigned.

?>

This closes the PHP code.


The only thing that is left to do now is display the $message_confirmation, which can be done like so:

<? echo $message_confirmation; ?>


So your whole page will be:


<?
if($_POST["submit"] == "Send message")

{
$to_address = "your@address.here";
$email_subject = "Subject of the emails";

$email_headers = "Content-type: text/html; charset=iso-8859-1
";
$email_headers .= "From: ".$_POST["from_name"]."<".$_POST["from_mail"].">

";

if(mail($to_address, $email_subject, $_POST["message"], $email_headers) == true)
{
$message_confirmation = "Your message has been sent<br/>";

}
else
{
$message_confirmation = "Your message could not be sent<br/>";
}
}
?>

<html>
<body>
<? echo $message_confirmation; ?>
<form method='post'>
<table>
<tr>
<td>
<label for='from_name'>Your name: </label>

</td>
<td>
<input name='from_name' type='text' />
</td>
</tr>
<tr>

<td>
<label for='from_mail'>e-mail address: </label>
</td>
<td>
<input name='from_mail' type='text' />

</td>
</tr>
<tr>
<td>
<label for='message'>Message:</label>
</td>
<td>

<textarea name='message'></textarea>
</td>
</tr>
<tr>
<td>
</td>
<td>
<input name='submit' id='submit' type='submit' value='Send message' />

</td>
</tr>
</table>
</form>
</body>
</html>