Practical Web Programming

Wednesday, November 12, 2008

How to Make a Cyber-Child

A little boy goes to his father and asks 'Daddy, how was I born?'

The father answers, 'Well, son, I guess one day you will need to find out anyway! Your Mom and I first got together in a chat room on Yahoo. Then I set up a date via e-mail with your Mom and we met at a cyber-cafe. We sneaked into a secluded room, where your mother agreed to a download from my hard drive. As soon as I was ready to upload, we discovered that neither one of us had used a firewall, and since it was too late to hit the delete button, nine months later a little Pop-Up appeared that said:

'You got Male!'

Friday, November 07, 2008

Lessons from the PHP Mail Function

Oh man, I got screwed.

Yesterday, I was testing the mail function that I wrote for the Helpdesk web application that I did. I was so sure the function works because I always receive email every time I submit a test ticket entry.

I wrote the function to send an email only to me when it is the development server and send to everybody else in our team when it's in the production. The human (or idiot :)) that I am, I miss to change the $to parameter of the mail function. So instead of only me receiving the email, our entire team for the whole US southern division received more than ten test emails. I only discovered the bug when my fellow developer called my attention that he keeps receiving a test email. Fortunately, those email all contains the title 'Test'.

Lesson learned? First, always test your application like you are in the production server. Had I not put 'Test' as the title, it would have been mistaken for a legit email. Second, don't think you already changed your source code somewhere, double check it, if not triple. Oftentimes, you are so sure you have it right, only to found out it's not.

Thursday, November 06, 2008

PHP: How to Print/Echo HTML Tags More Effeciently

PHP is very good at handling strings. But sometimes you have to help PHP do it's job more efficiently. In my recent web development job, I noticed that I always use PHP to print HTML tags. Here's an example of how I would do it before.


<?php
echo "<table>";
echo "<tr>";
echo "<td>"."How to print/echo HTML tags in PHP."."</td>";
echo "</tr>";
echo "</table>";
?>


Now, here's how I do it now which I think is more efficient and HTML readable.

<?php
echo "
<table
<tr>
<td>"."How to Print/Echo HTML tags in PHP More Effeciently"."</td>
</tr>
</table>
";
?>


Notice the difference? In the first PHP sourcecode, I use the echo five times, while in the second one, I use only once.

Using echo to print HTML tags is not expensive in terms of server resources, but if you're using it the way I did in the first example for all your pages and and you have thousands of users to your website, you'll see the difference.

Sunday, October 26, 2008

The Man Rules

Got this from a chain email, and I thought instead of passing this, I might as well post this in my blog. So here it is - The Man Rules.

These are our rules! Please note.. these are all numbered "1" ON PURPOSE!

1. Men are NOT mind readers.

1. Ask for what you want.
Let us be clear on this one:
Subtle hints do not work!
Strong hints do not work!
Obvious hints do not work!
Just say it!

1. Yes and No are perfectly acceptable answers to almost every question.

1. Come to us with a problem only if you want help solving it. That's what we do. Sympathy is what your girlfriends are for.

1. Anything we said 6 months ago is inadmissible in an argument. In fact, all comments become Null and void after 7 days.

1. If you think you're fat, you probably are. Don't ask us.

1. If something we said can be interpreted two ways and one of the ways makes you sad or angry, we meant the other one

1. You can either ask us to do something Or tell us how you want it done. Not both. If you already know best how to do it, just do it yourself.

1. Whenever possible, Please say whatever you have to say during commercials

1. Christopher Columbus did NOT need directions and neither do we.

1. ALL men see in only 16 colors, like Windows default settings. Peach, for example, is a fruit, not A color. Pumpkin is also a fruit. We have no idea what mauve is.

1. If it itches, it will be scratched. We do that.

1. If we ask what is wrong and you say "nothing," We will act like nothing's wrong. We know you are lying, but it is just not worth the hassle.

1. If you ask a question you don't want an answer to, Expect an answer you don't want to hear.

1. When we have to go somewhere, absolutely anything you wear is fine... Really .

1. You have enough clothes.

1. You have too many shoes.

1. I am in shape. Round IS a shape!

Saturday, October 11, 2008

Developers Definitely Need a Hug Sometimes

Friday, September 19, 2008

World's Natural Wonders Video, Made with iMovie

This is the first video I made with my MacBook. This one is made with iMovie which is included in Mac OS X. With iMovie making video is really easy. This video took me about 10-20 minutes to make. And what's cool about iMovie is you can upload it directly to YouTube. Check it out.

Monday, August 04, 2008

PHP: How to Display Errors Without Tweaking the Configuration File

Displaying errors in your PHP applications by tweaking the configuration file is sometimes troublesome, especially for most beginners. Luckily, you don't have to get your hand dirty to deal with it.

After installing MAMP (Mac, Apache, MySQL, PHP) in my MacBook, I had a hard time debugging my PHP codes as MAMP's default configuration do not allow showing of error.

In production environment, showing errors is a big mistake you can commit as your website is more vulnerable to hack attacks. But in development environment such as localhost, showing errors save you a lot of time in debugging.

Here's what I did.


ini_set('display_errors', 1);
error_reporting(E_ALL);


Put this two lines of codes in your index file and you'll see all the errors in your PHP codes will show off, if there are any (minus the links, of course).

Thursday, July 31, 2008

Ebook: Database Normalization Explained

If you're a programmer, aside from your favorite EDI and PL, database is one of your everyday companion. Almost all program these days uses a form of database, be it a flat file or RDBMS.

Being a good programmer, it's not enough that you only know the DML and DDL statements. You should also know and master the art of Database Normalization. Database normalization techniques is essential in order to achieve a high performance database design for your programs. If your database design doesn't conform to at least the Third Normal Form, you will have a hard time getting the data you want and it will be harder to achieve a high performance and scalable application.

Finally, there's an e-book that could give you the right knowledge about Database Normalization. This e-book shows you that normalization is a far too easy approach, and it is richly documented with graphical Entity Relationship and Server Diagram examples.

This knowledge-packed e-book is a great resource for DBAs and programmers.

Click here to get a copy of the Database Normalization Ebook.

Friday, February 29, 2008

PHP: How to Get the Current Server Date and Time

Adding date and time to your website gives it an impression of being fresh and updated regularly. In PHP, getting the current server date and time is a no brainer using the getdate() function.

To add date and time, see the PHP script below.

<html>
<head>
<title>DATE and TIME</title>
</head>

<body>
<?php
$date_array = getdate();
print "Server Date : $date_array[month] $date_array[mday], $date_array[year].<BR>";
print "Server Time : $date_array[hours]:$date_array[minutes]:$date_array[seconds]<BR>";
?>
</body>
</html>

Wednesday, February 27, 2008

Types of Looping Construct in Visual Basic

A loop is a sequence of instructions that is continually repeated until a certain condition is reached. It is a fundamental programming idea that is commonly used in writing programs. Without looping in a programming language, hundreds to thousands of repeated computer instructions would be time consuming, if not impossible to perform.

Here are the four types of looping construct in Visual Basic.

For Loop example
Private Sub ForLoop()
Dim intX As Integer

'-->INCREMENTING
For intX = 0 To 10
MsgBox "For Loop #" & intX, vbInformation, _
"Visual Basic Looping"
Next

'-->DECREMENTING
For intX = 10 To 0 Step -1
MsgBox "For Loop Step -1 #" & intX, vbInformation, _
"Visual Basic Looping"
Next
End Sub


Do While Loop example
Private Sub DoWhileLoop()
Dim intX As Integer

intX = 0
Do While intX < 10
MsgBox "Do While Loop #" & intX, vbInformation, _
"Visual Basic Looping"
intX = intX + 1
Loop
End Sub


While Wend Loop example
Private Sub WhileWendLoop()
Dim intX As Integer

intX = 0
While intX < 10
MsgBox "While Wend Loop #" & intX, vbInformation, _
"Visual Basic Looping"
intX = intX + 1
Wend
End Sub


Do Loop Example
Private Sub DoLoop()
Dim intX As Integer

intX = 0
Do
MsgBox "Do Loop #" & intX, vbInformation, _
"Visual Basic Looping"
intX = intX + 1
Loop While intX < 10
End Sub

Tuesday, February 26, 2008

PHP: How To Redirect To Another Website

Redirecting to another website in PHP is very simple and straightforward. Just by using the PHP header function and the the URL where to you want to redirect as a parameter, you can accomplish this task.

Here's the example below.
<html>

<header>
<title>PHP Redirection</title>
</header>

<body>

<?php
$url= "http://www.joelbadinas.com/";

/* Redirect browser */
header("Location: $url");

/* Make sure that code below does not
get executed when we redirect. */
exit;
?>

</body>

</html>


To use this script, just copy and paste it to your favorite PHP/HTML text editor, save it with a .php extension and put it in you web server. When you run it, you will be redirected to this blog.

Enjoy, and comments are welcome. (^_^)

Monday, February 25, 2008

How to Get The RGB of a Color Value in Visual Basic

This functions will returns the red, blue and green value of a color value.


'-->RETURNS THE RED COLOR VALUE
Private Function Red(ByVal Color As Long) As Integer
Red = Color Mod &H100
End Function

'-->RETURNS THE GREEN COLOR VALUE
Private Function Green(ByVal Color As Long) As Integer
Green = (Color \ &H100) Mod &H100
End Function

'-->RETURNS THE BLUE COLOR VALUE
Private Function Blue(ByVal Color As Long) As Integer
Blue = (Color \ &H10000) Mod &H100
End Function


Here's how to use this functions (see the image above for the result):

Private Sub Command1_Click()
MsgBox "Red: " & Red(Me.BackColor) & "," & vbNewLine & _
"Blue: " & Blue(Me.BackColor) & "," & vbNewLine & _
"Green: " & Green(Me.BackColor), _
vbInformation, "Form RGB Color"
End Sub

Thursday, February 21, 2008

How to Full Format Date in Visual Basic

This function shows how to full format date in Visual Basic 6.

Here's how to call the function: MsgBox FullFormatDate("02/24/1978")
The result will be: Friday, 24th Mar 1978

Public Function FullFormatDate(ByVal strDate As String) As String
Dim strDay As String

strDay = Format(strDate, "DD")
Select Case strDay
Case 1, 21, 31
strDay = Format(strDay, "#0") & "st"
Case 2, 22
strDay = Format(strDay, "#0") & "nd"
Case 3, 23
strDay = Format(strDay, "#0") & "rd"
Case Else
strDay = Format(strDay, "#0") & "th"
End Select

FullFormatDate = Format(strDate, "DDDD, ") & strDay & _
Format(strDate, " MMM YYYY")
End Function

Monday, February 11, 2008

Get Free Hosting Services at Tribuhost

Tribuhost is a website that offers free hosting services. At present, it is hosting two of my websites, Free Funny Jokes Collections and Online Recipe Collections, and so far, I am contented with there services. The support is fast and very helpful. Here's what Tribuhost offers:

- 250 MB disk space
- 6 GB Monthly transfer
- 5 MySQL databases
- 5 Add-on domains
- 5 Sub domains
- Vista Panel
- Automatic installer (29 scripts)
- Php 5
- FTP account
- File manager (browser upload)
- Web mail
- POP email accounts
- and more...

When you sign-up, you get a sub domain (ex: you.tribuhost.com) and you can create your website immediately. Also, a 468x60 banner ad is placed at the bottom of your website to cover the free hosting services - which I think is too little compared to the good service you get.

Sunday, February 10, 2008

Yahoo to Reject Microsoft's Plans to Buy the Internet Giant


Yesterday, reports came out the Yahoo plans to reject Microsoft's plans to buy the internet giant Yahoo.

Last February 1, Microsoft announced that it made an unsolicited offer to buy Yahoo for $44.6 billion, both in terms of cash and stocks. This is Microsoft's move to counter Google's an unstoppable superior status in the web.

The $44.6 billion bid, according to Yahoo's board, is way too low compared to it's current value.

Count the Forms Loaded in a Visual Basic Project

Sometimes you want to count the forms loaded in your Visual Basic project during runtime.


This simple and ready to use function will return the number of forms loaded in a project.

Public Function FormCount() As Long
Dim frmForm As Form
For Each frmForm In Forms
FormCount = FormCount + 1
Next
End Function


To use, just call the function like this.

MsgBox "# of forms loaded: " & FormCount

Saturday, February 09, 2008

My JoelBadinas.com Domain Is Not Working at the Moment

If you guys are wondering why joelbadinas.com is not working at the moment, it's because I am having issues with my registrar. For the meantime, this blog can be access via kabalweg.blogspot.com.

I'm already working to get my previous domain to work, unfortunately, it may take days before it will be back to normal. So please bear with me for the meantime.

Thursday, February 07, 2008

How to Flash a Minimized Form in the Taskbar Using API in Visual Basic

Here's a simple function to to flash a minimized form in the taskbar. It uses the FlashWindow API function and is written in Visual Basic

'-->API DECLARATION
Public Declare Function FlashWindow Lib "user32" (ByVal hWnd As Long, _
ByVal bInvert As Long) As Long

'-->THIS SUB WILL FLASH THE MINIMIZED FORM IN THE TASKBAR
Public Sub FlashForm(lngHandle As Long, _
Optional intCount As Integer = 1)
Dim intX As Integer

For intX = 0 To intCount
Call FlashWindow(lngHandle, True)
Next
End Sub

Wednesday, February 06, 2008

Free Online Recipes

Get free recipes at Free Online Recipes. This site features hundreds of free and practical yet mouth-watering recipes for homemakers, hobbyists and food enthusiasts or anybody who likes to cook. You can choose from several different recipe categories.

If you are a homemaker who don't know what dish to cook next for you family, a kitchen hobbyist who want to serve unique meals everyday, or just someone who would like to experiment something in the kitchen, this site is for you.

Drag PictureBox at Runtime in Visual Basic

The source codes below shows how you can drag a picturebox at runtime. To test this, copy and paste code to the declaration section of a form with a picturebox on it.

Private dblX As Double, dblY As Double
Private bolMove As Boolean

Private Sub cmdClose_Click()
Unload Me
End Sub

Private Sub Picture1_MouseDown(Button As Integer, _
Shift As Integer, _
X As Single, Y As Single)
If Button = 1 And Not bolMove Then
bolMove = True
dblX = X
dblY = Y
End If
End Sub

Private Sub Picture1_MouseMove(Button As Integer, _
Shift As Integer, _
X As Single, Y As Single)
Dim tmpy As Integer
Dim tmpx As Integer

If bolMove Then
If dblY > Y Then 'scroll up
tmpy = (dblY - Y) '* 100
Me.Picture1.Top = Me.Picture1.Top - tmpy
Else 'scroll down
tmpy = (Y - dblY) '* 100
Me.Picture1.Top = Me.Picture1.Top + tmpy
End If
If dblX > X Then 'scroll right
tmpx = (dblX - X) '* 100
Me.Picture1.Left = Me.Picture1.Left - tmpx
Else 'scroll left
tmpx = (X - dblX) '* 100
Me.Picture1.Left = Me.Picture1.Left + tmpx
End If
End If
End Sub

Private Sub Picture1_MouseUp(Button As Integer, _
Shift As Integer, _
X As Single, Y As Single)
bolMove = False
End Sub

Recent Post