Archive for the 'Development' Category

 
May 08
Fri
23

Optional should be OPTIONAL

Posted at 5:48 pm in Development

Yesterday and today, I've been working on some changes to our discount system (at work). The majority of the changes dealt with the (mssql) database ... stored procedures and user defined functions (ufn). One of the changes was to get the shopping cart subtotal based on different criteria ...

  • If the shopping cart had an order-level discount in it, and that discount was set up to override any discount exclusions that were set up, the subtotal calculation needed to only exclude those products where the exclusion wasn't allowed to be overridden.
  • If the shopping cart had an order-level discount in it, and that discount was NOT set up to override any discount exclusions that were set up, the subtotal calculation needed to exclude ALL products that had exclusions set up.

We have a ufn set up to calculate the order subtotal. This ufn is already taking into account the discount exclusions. But with my changes, I needed it to now look at the exclusion overrides. I was hoping that ufn's allowed for optional parameters like php functions do. So I set up a little test to see ...

SQL:
  1. CREATE FUNCTION [dbo].[ufn_MyTest](@Param1 int, @Param2 int = NULL)
  2. RETURNS varchar(500)
  3. AS
  4. BEGIN
  5.     DECLARE @MyVariable varchar(500)
  6.  
  7.     IF @Param1> 10
  8.         SET @MyVariable = 'Incoming parameter #1 is greater than 10.'
  9.     ELSE
  10.         SET @MyVariable = 'Incoming parameter #1 is less than 10.'
  11.  
  12.     IF @Param2 IS NOT NULL
  13.         BEGIN
  14.             IF @Param2> 10
  15.                 SET @MyVariable = @MyVariable + ' Incoming parameter #2 is greater than 10.'
  16.             ELSE
  17.                 SET @MyVariable = @MyVariable + ' Incoming parameter #2 is less than 10.'
  18.         END
  19.  
  20.     RETURN @MyVariable
  21. END

The database let me create the function, so I was hopeful that this was gonna work. So I tried executing my little ufn ...

SQL:
  1. SELECT dbo.ufn_MyTest(8)

But that didn't work. It gave me an error ...

An insufficient number of arguments were supplied for the procedure or function dbo.ufn_myTest.

What? Come on. That second parameter is set up to be optional. So I did some searching and found this tidbit ...

from the MSSQL manual
When a parameter of the function has a default value, the keyword "default" must be specified when calling the function in order to get the default value. This behavior is different from parameters with default values in stored procedures in which omitting the parameter also implies the default value.

Grrr!! :banghead: So I tried this ...

SQL:
  1. SELECT dbo.ufn_MyTest(8, DEFAULT)

THAT worked. But doesn't that kind of defeat the purpose of having an "optional" parameter. When I think "optional", I see that as meaning that the parameter doesn't have to be passed or it can be, depending on the situation. If I have to put 'DEFAULT' in it's place, that doesn't make it optional. I could just as well pass in the "optional" value.

To fix my little conundrum, I created a separate ufn and called the appropriate one based on the parameter that I had wanted to pass in as an optional one. That worked like a charm. I just wish that Microsoft would have set up mssql ufn's to accept truly optional parameters. But I guess I'm not surprised. It is Microsoft after all. :nahnah:

Apr 08
Mon
21

XSL Variable Quirks

Posted at 12:52 pm in Development

I'm still working on our Google Checkout integration at work. I have the web service done, but now I need to process the notifications from Google and update our database accordingly. This morning I was working on translating the XML from Google's 'New Order Notification' into a simpler XML that I can use to insert the order into our database by translating it through an XSL stylesheet. While doing so, I ran into a little quirk in how XSL translations are done.

I was splitting the name into pieces for first name, middle initial, and last name. While going through the various ways the name could be split, I was setting variables with the values. After all of the processing, I was using the variables to set the XML pieces that I needed. But I was getting errors saying that a couple of my variables weren't registered.

So I did some searching to find out what was up since I don't have a lot of experience with XSL (hence the issue I ran into). Apparently variables have VERY LIMITED scope. I was setting the variables with conditional statements inside of a template that was passed the name node to split.

CODE:
  1. <xsl:variable name="ContactFirstName" select="substring-before($name, ' ')" />
  2. <xsl:variable name="rest" select="substring-after($name, ' ')" />
  3. <xsl:choose>
  4.     <xsl:when test='contains($rest, ' ')'>
  5.         <xsl:variable name="ContactMiddleInitial" select="substring-before($rest, ' ')" />
  6.         <xsl:variable name="ContactLastName" select="substring-after($rest, ' ')" />
  7.     </xsl:when>
  8.     <xsl:otherwise>
  9.         <xsl:variable name="ContactMiddleInitial" select="''" />
  10.         <xsl:variable name="ContactLastName" select="$rest" />
  11.     </xsl:otherwise>
  12. </xsl:choose>
  13. <FirstName><xsl:value-of select="$ContactFirstName" /></FirstName>
  14. <MiddleInitial><xsl:value-of select="$ContactMiddleInitial" /></MiddleInitial>
  15. <LastName><xsl:value-of select="$ContactLastName" /></LastName>

Doing the above code, I got errors that the ContactMiddleInitial and ContactLastName variables weren't registered. I was confused because they were set no matter what from the conditions. But apparently the xsl:variable in each case had a parent of either xsl:when or xsl:otherwise, and they weren't available outside of that. I didn't realize that.

So I changed my code to the following and all was fine:

CODE:
  1. <xsl:variable name="ContactFirstName" select="substring-before($name, ' ')" />
  2. <xsl:variable name="rest" select="substring-after($name, ' ')" />
  3. <xsl:choose>
  4.     <xsl:when test='contains($rest, ' ')'>
  5.         <xsl:variable name="ContactMiddleInitial" select="substring-before($rest, ' ')" />
  6.         <xsl:variable name="ContactLastName" select="substring-after($rest, ' ')" />
  7.         <FirstName><xsl:value-of select="$ContactFirstName" /></FirstName>
  8.         <MiddleInitial><xsl:value-of select="$ContactMiddleInitial" /></MiddleInitial>
  9.         <LastName><xsl:value-of select="$ContactLastName" /></LastName>
  10.     </xsl:when>
  11.     <xsl:otherwise>
  12.         <xsl:variable name="ContactMiddleInitial" select="''" />
  13.         <xsl:variable name="ContactLastName" select="$rest" />
  14.         <FirstName><xsl:value-of select="$ContactFirstName" /></FirstName>
  15.         <MiddleInitial><xsl:value-of select="$ContactMiddleInitial" /></MiddleInitial>
  16.         <LastName><xsl:value-of select="$ContactLastName" /></LastName>
  17.     </xsl:otherwise>
  18. </xsl:choose>

Seems kinda silly to me that you can't do what I tried in the first case. But I guess that's how the language is set up. The first question on this page has more detailed info on this: Q. Can I change the value of an XSLT variable?

This is mostly for my benefit in case I run into this issue again. But maybe it will help someone else out there. See ... I can write tech-related posts.

Jan 07
Mon
08

Weekday plugin

Posted at 7:54 pm in Development, Word Press

When I first moved over to WordPress as a blogging platform in June 2004, I wasn't able to find a way to get the date for my posts to display Today or Yesterday. I had been able to when I used MovableType. What I'm talking about is:

  • If the post was posted today, it will put Today.
  • If the post was posted yesterday, it will put Yesterday.
  • Otherwise it will put the actual weekday (i.e. Monday).

So ... I wrote one of my first WordPress plugins. It's the only one I wrote that I still use.

Download:
Weekday plugin version 1.5

Installation:
Installation is simple. Just unzip the downloaded file and upload it to the /wp-content/plugins/ directory in your WordPress install. Then go to the Plugins page in your WordPress Admin and activate the plugin.

Usage:
In your template where you want to display the date for a post, replace:

PHP:
  1. <?php the_date(); ?>

with

PHP:
  1. <?php weekday_date(); ?>

This will display the date for every post, even if there are multiple posts for the same day. This tag must be used within The Loop

You can customize the format of the date, HTML to put before and after the date (i.e. surround the date in a container), and a separator between the Weekday and the rest of the date. You can also specify if you want to display the date or add it to a string and display it elsewhere:

PHP:
  1. weekday_date($date_format="", $container_start="", $container_end="", $separator="", $echo = true)

$date_format can be anything you want. See the date() function for possibilities.

On this site, I am using the following to display the date:

PHP:
  1. <?php weekday_date('d M Y','','', '<br />'); ?>

Questions?
If you have any questions about the plugin or any problems, please let me know.

I hope someone out there can find this useful. If you do, please let me know. Leave a comment with a link to your site.

I'm a Redneck Woman
Archives

Search
 
I'm a High Tech Broad
Stats
492 Posts & 80 Comments
Since 07 January 2007

46 queries in 0.652 seconds.
Feeds
Advertising


Meta
BlogRoll
Other Stuff
50% High Tech  50% Redneck
100% Woman
Valid CSS  Valid XHTML
This site looks best in Firefox  This site looks best at 1024x768 or higher
Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License
Where You Can Find Me
twitter   flickr   delicious   brightkite   facebook   friendfeed
infield parking   myspace   jaiku   pownce   dopplr
Twitter
  • There. My salad is all ready to go for lunch tomorrow. Only worried about how the cucs will hold up. More time tonite to make it up. 5 hrs ago
  • I'd better grill up some chicken so I have a tasty salad for lunch tomorrow. 6 hrs ago
  • Sucky part of working out when I get home from work: I don't get to eat supper 'til almost 7pm. 7 hrs ago
  • More updates...

Powered by Twitter Tools.

TwitterCounter for @hightechredneck
Affiliates
HostGator.com Affilliate

Get Smart. Get FireFox!

Get Thunderbird!
Other Cool Sites
kirtsy!
Tag Cloud
Recent TwitPic Uploads