Adventures of a Geek various programming related articles

11Apr/120

Organizing Hierarchical Databases

 

I was researching a better way of organizing the database of one of my projects and came across an interesting article. The article is about organizing hierarchical data in mysql databases. Here it is. The two methods proposed are very well explained and also the advantages and disadvantages of using each method are presented. It helped me get a clear picture of the available methods and pick the best one for my project :)

5Nov/100

How to use Google Maps Street View

My fourth google maps tutorial was published on 1stwebdesigner.com. The tutorial covers another of google maps features, street view.

I have built an app for this tutorial, an interactive visit of Paris. You’ll be able to visit certain locations, walk through Paris or take virtual tours.

You can read the tutorial here and also play with the app.

You can also download the source code.

18Oct/100

How to convert between ANSI and UNICODE strings?

Useful C++ code for converting between ANSI an UNICODE strings:

Ansi to Unicode:

char *ansistr = "Hello";
int a = lstrlenA(ansistr);
BSTR unicodestr = SysAllocStringLen(NULL, a);
::MultiByteToWideChar(CP_ACP, 0, ansistr, a, unicodestr, a);
::SysFreeString(unicodestr);

Unicode to Ansi:

BSTR unicodestr = 0;
SomeCOMFunction(&unicodestr);
int a = SysStringLen(unicodestr)+1;
char *ansistr = new char[a];
::WideCharToMultiByte(CP_ACP,
                        0,
                        unicodestr,
                        -1,
                        ansistr,
                        a,
                        NULL,
                        NULL);
delete[] ansistr;
::SysFreeString(unicodestr);

25Sep/100

Creating an Interactive Travel Map using the Google Maps API

I've played again with the google maps api and started working on an interactive travel map. The idea is that everyone can add new locations and view the locations added by others.

I've also written a tutorial about this, you can read it here.

15Sep/100

How to integrate facebook in your site using the facebook javascript sdk

If you are looking for integrating facebook in your site, you should check out the facebook javascript sdk. I've written a tutorial that shown how to use the sdk. It's been published on 1stwebdesigner.com.

Check it out!

15Aug/100

Other improvements to my distance finder

I've added some more features to my distance finder. You can now share your search results with your friends on twitter. I've used twitter's @anywhere to do this.

If you want to find out how I did this, I've also written a tutorial.

I'm planning to also integrate facebook so you'll be able to share your results on there too :)

15Jul/100

How to prevent contact form spam

Yes, you can also get spammed through the contact form on your site! How can you prevent that? It’s pretty easy! Add a method of checking if the form is filled by one of your site’s visitors and not by a bot.

There are more ways of doing this. One of them is to use a captcha (a small text shown in an image that the user has to fill in the form). Here’s a tutorial on how to use one of these.

I will show you another way of doing this: having the user fill in the result of a mathematical operation. If the correct result is filled in, the form is sent to you.

You can see a demo of this on my website.

Here’s how we’ll do this:

Suppose we have the following contact form (in the file named 'contact.php'):

<form action="send.php" id="contact" name="contact" method="post">
<table align=left>
	<tr>
		<td>Name:</td>
		<td><input type="text" id="name" name="name"></td>
	</tr>
	<tr>
		<td>Email:</td>
		<td><input type="text" id="email" name="email"></td>
	</tr>
	<tr>
		<td>Message:</td>
		<td><textarea rows="5" cols="40"  id = 'message' name = 'message'></textarea></td>
	</tr>
	<tr>
		<td><input type="submit" value="Send message"></td>
	</tr>
</table>
</form>

When the user presses the submit button, the 'send.php' script will be called. This script sends an email with the info that was filled in the form.

We will have to add the part to verify the user.

We will ask the user to fill in the result of a mathematical operation. The user will have to add the randomly generated numbers between 1 and 15.

Here’s the code to generate the two numbers and compute the correct sum:

<?php
	srand(time());
	$nr1 = (rand()%14)+1;
	$nr2 = (rand()%14)+1;
	$sum = $nr1 + $nr2;
?>

We will now display the numbers to the user and ask him/her to fill in the result in a new input field. We’ll add a new row in the table which holds the form:

<tr>
	<td>Are you human?<br/>What is the result of <?php echo $nr1;>+<?php echo $nr2;>?</td>
	<td><input type="text" id="nr" name="nr"></td>
</tr>

We’ll also add a hidden field to the form to hold the correct sum.


These are all the changes we have to make to the contact form.

We’ll also have to modify the send.php script to check the sum before sending the email:

$nr = $_POST[nr];
$sum = $_POST[sum];

if ($nr != $sum)
	header('Location: contact.php?msg=wrong');
else
	// add code to send the mail with the form data

If the sum from the hidden field is equal to the one filled in by the user, the email is sent. If not, the user is redirected to the 'contact.php' page and an error message is shown. Here’s what we’ll have to add to the contact.php file to show the error message:

if(isset($_GET['msg']))
{
	if ($_GET['msg'] == 'wrong')
		echo "<p> <font color=red>The result you entered is wrong</font>";
}

And that’s it!

Let me know if you have questions or comments!

3Jul/100

How to add favicons to your site or wordpress blog

What are favicons? They are the tiny icons that you can see next to a site's title in your browser window. You can also see them when bookmarking a site, next to it's title.

Adding one to your website is really simple! You just have to find an image you like, resize it to 16pixels x 16pixels (the custom size for favicons). Name the image "favicon.ico" and place it in your website's root folder. And you're done!

Adding a favicon to your wordpress blog is a little bit more complicated. You will have to add the icon to your templates main folder (your blog/wp-content/themes/default) and edit the "header.php" file of your current theme. You will have to add this line in the

 <head></head> 

tags:

<link rel="shortcut icon" href="<?php bloginfo('template_directory'); ?>/favicon.ico" />

And that's it!