Practical Web Programming

Tuesday, December 30, 2008

PHP: How to Dynamically Alternate the Table Row Background Color

In PHP, making the <table> row background color alternate dynamically is relatively easy. The key here is PHP's modulo (%) operator. Using the % operator inside a loop construct, you can reference every other row in an HTML <table>. See the example below.

Create the CSS rules.
.alt-color-1{
background-color:red;
}

.alt-color-2{
background-color:green;
}


Create the PHP script.
<?php
print "<table width='200' border='1' cellspacing='1' cellpadding='1'>";
for ($i = 0; $i < 10; $i++)
{
if (($i % 2) == 0)
{
$alt = "alt-color-1";
}
else
{
$alt = "alt-color-2";
}

print "<tr class='".$alt."'>";
print "<td>".$i."</td>";
print "<td>".$i."</td>";
print "</tr>";
}
print "</table>";
?>


Put it all together.
<html>
<head>
<title>Alternate Color in Table Rows</title>

<style>
.alt-color-1{
background-color:red;
}
.alt-color-2{
background-color:green;
}
</style>

</head>
<body>

<?php
print "<table width='200' border='1' cellspacing='1' cellpadding='1'>";

for ($i = 0; $i < 10; $i++)
{
if (($i % 2) == 0) //USE THE MODULO OPERATOR
{
$alt = "alt-color-1";
}
else
{
$alt = "alt-color-2";
}

print "<tr class='".$alt."'>";
print "<td>".$i."</td>";
print "<td>".$i."</td>";
print "</tr>";
}
print "</table>";
?>

</body>
</html>


After executing the PHP script, here's what the webpage will look.

PHP: How to Dynamically Alternate the Table Row Background Color

Friday, December 26, 2008

PHP: How to Display The Current Month in Calendar Style

Displaying the current month in a calendar style is very handy and user-friendly for a website, specially for a blog. Using a calendar-style navigation your visitors can easily navigate through your previous posts and articles.

Fortunately, in PHP, this seemingly difficult task in HTML is relatively easy. Just by using a nested For Loop and PHP's Date functions, use can build your own calendar.

Here's the simple source code.

<?php
$now = getdate(time());
$time = mktime(0,0,0, $now['mon'], 1, $now['year']);
$date = getdate($time);
$dayTotal = cal_days_in_month(0, $date['mon'], $date['year']);

print '<table><tr><td colspan="7"><strong>' . $date['month'] . '</strong></td></tr>';
for ($i = 0; $i < 6; $i++)
{
print '<tr>';
for ($j = 1; $j <= 7; $j++)
{
$dayNum = $j + $i*7 - $date['wday'];
print '<td';
if ($dayNum > 0 && $dayNum <= $dayTotal)
{
print ($dayNum == $now['mday']) ? ' style="background: #aaa;">' : '>';
print $dayNum;
}
else
{
print '>';
}
print '</td>';
}
print '</tr>';
if ($dayNum >= $dayTotal && $i != 6)
{
break;
}
}
print '</table>';
?>

Here's how the calendar will look like.


To make it fancier, all you have to do now is use CSS to add colors and styles.

Sunday, December 21, 2008

My First Ubuntu Machine, and I'm Loving it

My first encounter with Ubuntu was more than a year ago. Back then I was using Windows XP both at work and at home. By then, I tried installing it as a secondary boot in my XP desktop at home but it just wouldn't load after the installation so I didn't force it.

More than two weeks ago, I finally completed my desktop. It took me more than a month to build because I have to wait the parts (LCD monitor, casing, keyboard, mouse and power supply) that I bought from Amazon. I can say that it's worth the wait because this is my first Ubuntu machine and I'm loving it. I got Ubuntu 8.10 installed in it.



Though I am Windows user ever since, it wasn't difficult for to adjust to this new operating system. Like Windows, Ubuntu comes with softwares that you can use for your daily tasks. My problem with this OS is my printer is not working on it. Fortunately, I got a Macbook to print with. Other than that, I'm head over heels with it.

With this first taste of Linux, I think I will not go back to Windows if given a choice.

Saturday, December 20, 2008

PHP: Checking If Email is Valid with filter_var Function

In PHP 4, checking if an email is valid uses regular expressions. But with PHP 5, you can use the filter_var function. This function returns the filtered data, or false if the filter fails.

Here's the syntax.

mixed filter_var ( mixed $variable [, int $filter [, mixed $options ]] )

Where:
variable = Value to filter.
filter = ID of a filter to use. Defaults to FILTER_SANITIZE_STRING.
options = Associative array of options or bitwise disjunction of flags.


Here's how to use the function.

if (filter_var(trim($email), FILTER_VALIDATE_EMAIL))
{
echo $email . " is valid.";
}
else
{
echo $email . " is invalid.";
}

Friday, December 05, 2008

PHP: A Simple Function to Create an HTML Select Date Options

Here's a simple PHP function to create a select date options that you can use in your HTML forms to let users select a date. This is simple and easy to use. Below is the function definition.

//RETURN DATE OPTIONS IN HTML
function get_date_options($year = 0000, $month = 00, $day = 00)
{
$ret_val = "";

//ASSEMBLE MONTHS
$options = "<option value='00'>MM</option>";
for ($i = 1; $i < 13; $i++)
{
$attribute = "value='".$i."'";
if ($i == $month)
{
$attribute .= " selected ";
}
$options .= "<option ".$attribute.">".str_pad($i,2,0,STR_PAD_LEFT)."</option>";
}
$ret_val .= "<select name='month'>".$options."</select> / ";

//ASSEMBLE DAYS
$options = "<option value='00'>DD</option>";
for ($i = 1; $i < 32; $i++)
{
$attribute = "value='".$i."'";
if ($i == $day)
{
$attribute .= " selected ";
}
$options .= "<option ".$attribute.">".str_pad($i,2,0,STR_PAD_LEFT)."</option>";
}
$ret_val .= "<select name='day'>".$options."</select> / ";

//ASSEMBLE YEARS
$options = "<option value='0000'>YYYY</option>";
for ($i = 2008; $i < 2051; $i++)
{
$attribute = "value='".$i."'";
if ($i == $year)
{
$attribute .= " selected ";
}
$options .= "<option ".$attribute.">".$i."</option>";
}
$ret_val .= "<select name='year'>".$options."</select>";

return $ret_val;
}


Here's a simple way to use it.

Enter your birthday : <?php print get_date_options() ?>


And here's a way to use it to make default to a date.

Enter your birthday : <?php print get_date_options("2008", "12", "05") ?>

Tuesday, December 02, 2008

PHP: Simple String to Date / Date to String Conversion Functions

In PHP even a simple INSERT and UPDATE to the database, MySQL specially, can lead to bugs in DATETIME fields. Whenever I do database insertion, I run into this kind of problem. Instead of the actual date, the string "0000-00-00 00:00:00" appears. This string represents NULL in date.

To combat this problem, I created simple String to Date / Date to String conversion functions. Before inserting/updating date to a MySQL database, convert first the date using text_to_date_format. Then, when querying, use the date_to_text_format to make the date more human readable.


//FORMAT DATE TO STRING
function date_to_string_format($date)
{
if (strtotime($date))
{
return date("M. d, Y h:i A", strtotime($date));
}
else
{
return "";
}
}

//FORMAT STRING TO DATE
function string_to_date_format($date)
{
if (strtotime($date))
{
return date("Y-m-d H:i:s", strtotime($date));
}
else
{
return "0000-00-00 00:00:00";
}
}


Here's what your INSERT SQL statement should look like using the above function.


$sql = "INSERT INTO my_table (
my_id,
my_date
) VALUES (
1, ".
string_to_date_format($var_date).")";

Recent Post