| Subcribe via RSS

Alternating Row Color in PHP

June 25th, 2009 | Comments Off | Posted in CSS, PHP, Uncategorized

In ASP.NET setting the background color for alternating rows is very simply. Just update the property on the Gridview and whalah! Crayola just marked up your database output in pretty colors. In PHP, its not quite as simple, but not terribly complicated either. Basically, we are going to set the odd and even row background colors in our css stylesheet then increment our loop value as we cycle through the records and dynamically populate our class value on the tr for the given row.

Stylesheet

PHP Code

This is a JSON result set we are looping through. The key line to focus on here is row 9 in the PHP code. This appends the i value we’ve been incrementing via the loop to the tr class property. This gives us that clean alternating row look within minimal work.

Tags: , ,

How to Find out if a Number is Even/Odd in C#

June 19th, 2009 | Comments Off | Posted in C#

There are several different ways at getting at this, but your simplist is to evaluate the number via the modulus operator (%). The modulus operator calculates the remainder when the first number is divided by the second. If its even, there won’t be a remainder and if its odd — you get the routine. How is this helpful? Say we are dynamically writing out rows in a datagrid and we need to tag them with alternating colors. This makes for an easy solution.

Tags: , ,

PHP Banner Ad Rotator

June 18th, 2009 | Comments Off | Posted in PHP

I seem to be doing a lot of work in WordPress these days and you know what that means — PHP development. Prior to WordPress, I’d never even thought of what PHP code might look like. Now I’m all too familiar with it. Not a bad language I have to say. Back to the subject at hand. I have banner ads at the top of these pages. While its ok to drop an image into a page, I get rather bored looking at the same ad. So what if we wanted to display a bucket of ads, basically in random order. We’d need some kind of ad rotator. We’re going to accomplish this with two files: adrotator.php that houses our php code and ads.txt that holds our series of banner ads and corresponding links.

adrotator.php

So the first line gets our file containing our ads so line 2 can split the contents up into an array breaking when it encounters the tilda. Line 3 queries the array to get our count then selects a random place within the high and low range. Finally we pluck out the banner that lines up with our random value and print it out to the browser. Next let’s take a quick look at the ads.txt file.

ads.txt

Nothing too challenging there. Finally we have to put that banner into our php code. We attach it like any other include file:

Kudos to phpbuddy for the original code on this post was drawn from.

Tags: , ,

Integrating Tooltip Functionality into a Datagrid in ASP.NET 1.0

June 17th, 2009 | Comments Off | Posted in ASP.NET

I know what you are thinking, “why doesn’t he just build this in 2.0 and make it a gridview? It all comes built-in.” Well sometimes we have a small update to make to an old system, and it doesn’t make sense to do the heavy lifting of a system rewrite. Then again, I may just be lazy. So the easiest approach is going to be one based on javascript. A couple years back, I ran across this handy file (wz_tooltip.js) from Walter Zorn that makes this job a breeze. Simply include the downloaded file within the footer of your page:

Now we focus on the datagrid. On the item we are looking to wire up with the tooltip, we’re going to set the onMouseOver attribute within the anchor tag. Within the onMouseOver, we return the escape function with the phrase we are looking to display in the tooltip:

In the previous example, I’m putting the tooltip over an image file, and the text that is displayed is the ‘control’ field pulled from the database. That is it. You were expecting more code? Sorry to disappoint you.

Now I haven’t tested this with images or should you want to do any fancy formatting with your text. You may have to play around with the javascript code or keep searching for another solution if that is the hole you find yourself in.

Tags: , ,

Writing Inline If Statements in CSharp

June 9th, 2009 | Comments Off | Posted in C#
Sometimes you don’t always have room to execute a full if statement block. Think about when you are getting back database output and processing it through the gridview. If you are looking to evaluate the returned value to display information to the user, you can either make a method call or do a quick inline if statement. It does everything a traditional if statement accomplishes, but its streamlined into one line. It follows the format ( ? : ). The following is the inline if statement in practice. Tags: ,

Format DateTime & Currency in ASP.NET from Database Output

June 8th, 2009 | Comments Off | Posted in ASP.NET, Uncategorized

Pretty easy one today. The database is spitting back the datetime stamp as 6/9/2009 12:05:42 AM and you need to drop the time. No problem. We just need to convert the database output to datetime format then set the date format in the ToString function.

This gives us that nice clean format we were looking for. Here are a variety of other options when it comes to formats for the datetime.

If we wanted to format currency, we would place it in the following format:

Tags: , ,