Monday, December 20, 2010
Database access utility in PHP
Wednesday, December 15, 2010
JH-JS-CLASS
Tuesday, December 14, 2010
CalendarPopUP explained.
Calendar PopUp
Monday, December 13, 2010
Form Validation update
Friday, December 10, 2010
Validating checkbox with same name
Wednesday, December 08, 2010
Javascript Regex
Thursday, December 02, 2010
Creating Regex with literal backslash
Wednesday, December 01, 2010
Javascript Form Validation
Javascript Regex with numbers
Friday, November 26, 2010
Deleting whole table with Identity Field
Wednesday, November 24, 2010
PHP PDO Prepare with date field
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);
$sql = "SELECT * FROM booksa";
$q = $conn->query($sql) or die("ERROR: " . implode(":", $conn->errorInfo()));
Tuesday, November 23, 2010
Getting element from DOM tree
Monday, November 22, 2010
Close top window automatically without prompt
Wednesday, November 17, 2010
SetTimeout to temporarily disable buttons
Monday, November 15, 2010
Get date only from ODBC datetime in PHP
Monday, November 08, 2010
Excel connect to database
Monday, November 01, 2010
Brio Working hour calculation
Friday, October 29, 2010
Change Oracle Password
Wednesday, October 27, 2010
Junk Mail Proof Letter Box and SingPost
Regarding the Junk Mail Proof Letter Box and SingPost. Town Council simply ask me to opt out (according to their reply to me). They are not going to stop SingPost.
According to ST reporter Irene Tham in her article "Locked out of Competition?", IDA replies "it is not an area that the IDA policies."
SingPost has a reply in Stomp. They are obligated to deliver "addressed or unaddressed."
So what is the result? No where to turn to.
Well in that case we should be obligated to return it via the "return mail" letter box as the junk mail is "wrongly addressed". Don't throw it away as it is deemed by SingPost as legitimate. If you throw it away you are unethical. You are throwing away an admail which is "not addressed to you" and which SingPost deemed it as legitimate "mail".
Since the admail is not addressed to you, it should be a wrong delivery as it is not addressed to you. You are, therefore, obligated to return the wrongly delivered mail to the sender via SingPost. In this way, you keep the lobby area of your HDB Block clean and did a good civil service. SingPost can't complain that you throw junks into the "return letter" letter box otherwise they slap themselves.
Tuesday, October 26, 2010
SingPost and Junk Proof Letter Box
Thursday, October 21, 2010
Enter Foreign Language for some applications
- Go to "Control Panel".
- Click to open "Regional and Language Options".
- Click "Administrative" tab.
- Under "Language for non-unicode programs 'system locale'", click "change system locale".
- Choose the language of your choice and click "ok".
Wednesday, October 20, 2010
Testing Ajax
Monday, October 18, 2010
SSIS Editing
Friday, October 15, 2010
IF ELSE using Ternary condition.
Dollar Sign function in JS
Tuesday, October 12, 2010
Running a SSIS package from the web.
Monday, October 04, 2010
Javascript associative array testing
Friday, October 01, 2010
Fixed menu again
I have just noticed that when the web page is shown with the SELECT positioned just behind the DIV, it actually works. But when you scroll the SELECT again go on top of the DIV.
I have a stupid but working solution to this. Just do the following.
Create a javascript right at the end of the page which will only appears if the browser is IE 6 and below using the
Wednesday, September 29, 2010
Fixed menu
Sunday, September 26, 2010
Dynamic floating DIV
Besides this issue, the CSS for "position" also caused a lot of headache. "Position:fixed" does not work for IE7 (not sure about IE8 and above). "Position:absolute" has to be used. The problem is that this CSS does not make the DIV stay at the defined position.
I have read from post by others that there is only one choice for IE. Use "Position:absolute" and then add a javascript "window.onscroll" event and set the top and left style property of the DIV element using the scrollTop and scrollLeft offsets respectively.
It is not the best solution for IE as when you scroll the DIV actually jumps a bit. It is still better than nothing right?
Positioning the div itself on the top of the screen is no big issue. What if you want to position the DIV somewhere at the bottom of the page? Surely you have to take into consideration of the height of the DIV to ensure that it does not goes beyond the display.
It will be much more difficult if your DIV changes its size dynamically. Fortunately there is a element.offsetHeight/offsetWidth to help. It is the actual height/width of the element even if you set the style.height to auto.
One more thing. If you want to set the DIV to be at a fixed percentage of the window size instead of a fixed pixle value, you have a problem if your DIV is near the bottom of the window. When you resize the window, the div may shifts accordingly and get hidden or shifts up too high.
Therefore, you will need to offset the percentage with the offsetHeight of the DIV. To do that, you will need to know the height of the window.
The problem is that the height of the window can mean different thing in different browsers. This is especially true for different IE versions and whether it is set to quirk mode.
A easy way out is to do this.
var root = (document.compatMode == "CSS1Compat"?
document.documentElement: document.body);
var myHeight = 0;
if( typeof( window.innerWidth ) == 'number' ) {
myHeight = window.innerHeight;
} else
myHeight=root.clientHeight
Doing offset is then easy if the height of the window is known. First find out what is the pixel by multiplying the window size with the percent. If the pixle value plus the offsetHeight of the DIV is greater than the window size, then just set it to the window size minus the offsetHeight so that the DIV can be shown all the time.
PHP PDO
Friday, September 17, 2010
Dynamic add style to element
document.getElementById("myid").style.cssText="font-size:20px;font-weight:bold;"
This method works for both IE (tested in IE6,IE7) and Firefox (ver 3.6).
Note that you actually replaces the old style with your new style using this method.
Wednesday, September 15, 2010
Dynamic add elements with a twist
Monday, September 13, 2010
Zen Coding
Wednesday, September 08, 2010
Give up with odbc_prepare
odbc_prepare with parameters
Monday, August 23, 2010
Ajax XMLHttpRequest object in IE
Dynamic add option to Select Element
Monday, August 16, 2010
Funny behavior of my IE
Funny thing is that this issue only appears on my IE. I tried on at leat 3 other IE on other machines and they all shows the table without error. What in the world happened?
Since it is system generated error and that I know it was caused by the data in `part description` field, I decided not to `repair` my IE but concentrate on making the field workable with the javascript.
Well, it is quite simple. I simply remove any character that is not allowed by me using preg_replace command. It cut short all the necessity to replace any character that caused the javascript error. The regexp code is [^a-zA-Z0-9\\-\\.,\\/_ &:;] with a `si` parameter.
Obviously, I did not solve my IE problem. But who cares when it does not complain anymore.
Join table from two source but same server
select top 1 case_id,cso_no from otherdb.dbo.tbl_case
inner join
delivery.dbo.cdrecord on case_id =cso_no
Notice that the database name was included with the owner `dbo` indicated also. However, use this method sparingly. It is not efficient to perform queries over different databases. Moreover, you have to make sure that the user rights are exactly the same unless you are, of course, dbo.
Self Join
In a company, I have a employee table listing the employee information. In the employee table I list the employee information plus the manager\'s id ( to save space). Now I want to list the employee information but want to know the manager\'s name instead of id.
Since the manager is also an employee listed in the table, the only way to get the information is via self joins. The following is the sql.
select a.employee_name as employee,b.employee_name as manager from tbl_employee as a inner join tbl_employee as b on a.manager_id = b.employee_id
Here you can see that it is no differen from any other joins except that the same table is used.
Wildcards in SQL Server
Now we do have literal characters that uses the wildchard character. How do we defile the literal characters in the search? it is quite simple. Simply enclose the character between `[]`. That will include the `[` and `]` also. Another character is the single quote. Well, simply add another single quote to it. Finally there is an ESCAPE clause where you could define a escape character. Normally the escape character is a backslash. just add ESCAPE `\' to the end of the where clause.
In Oracle, it has quite similar syntax except that it does not have `[]`. It does uses ESCAPE clause exactly like MSSQL. I think we should use the ESCAPE clause as standard except under special circumstances then we use the `[]` wildcard.
Recursive search
with PartSearch (Part_number,Replacement_part, level) as {
select Part_number,Replacement_part, 0 as level from wfm_parts p
union all
select p.Part_number, p.Replacement_part, ph.level+1 from wfm_parts p
inner join PartSearch ph on ph.part_number = p.Replacement_part }
select Part_number,Replacement_part, level from PartSearch where level > 0
Obviously, you have to use stored_procedures to do it.
PartSearch is a pseudo name created. It can be any name. The above is not tested as I do not have a good database sample to run the example. The syntax should be correct.
Output Clause in MSSQL 2005
Starting fom MSSQL 2005 there is an interesting command which resembles MYSQL mysql_insert_id() function. It has more functionality than MYSQL. It can be used in INSERT, DELETE, UPDATE clause to get the id involved.
For example
insert into tbl_test_insert (case_id) output INSERTED.ID, INSERTED.case_id values(`123456789`)
The query is the same as any INSERT query except there is some extra clause inserted in between. The clause is OUTPUT INSERTED.ID, INSERTED.case_id. This clause tells the query to output values defined by INSERTED. The value is actually the record or the data that was inserted/deleted into the table.
It comes very handy if you have to insert/delete a main table and then insert into a sub table that has the same id as the main record. The id in the main record is auto increment identity column so that it could be an index. You will not know what is the value till the record is inserted. By inserting/deleting a record in the main table you will need to get the id value so that you could perform insert/delete on the sub table.
I used to insert a UUID when inserting the main table then query the main table to get the id by finding the UUID. Now I can just use the insert statement then simply do a odbc_fetch_row() to get the id with just one query. Neat.
Just to let you know this- use DELETED.xx if you are doing delete query. For UPDATE you use INSERTED for those fields you updated and use DELETED for those fields you replaced.
SQL FIRST aggregate function
There are actually tweaks to make it work. Heres how.
select case_id, RowNumber from (select case_id, ROW_NUMBER() over (partition by case_id order by subcase_id) as RowNumber from tbl_case) as tblcase where RowNumber=1
The above sql actually returns the first subcase information since i use ORDER BY SUBCASE_ID in PARTITION BY.
This is one of those odd cases where you do a select from a derived table.
This query is particularly useful when you happen to have duplicate subcase_id. It will return unique rows.
Obviously, if you just do like the example above, it is kind of going the long way. A simple DISTINCT and a WHERE clause can do much better job than that. But if you have duplicate subcase_id then your DISTINCT,WHERE method may not work as per expected. Moreover, you could make use of the derived table to do MAX and MIN aggregate function on other fields across all subcases.
PARTITION BY works similar to BRIOQUERY SUM BY method. It does not require a GROUP BY which is somtimes very obstructing to the AGGREGATE function. For example you want to do an aggregate but also want to include the case_title field. That is impossible for normal query with GROUP BY since case_title is almost unique to each case. After getting the aggregated results then you use the above SELECT FROM DERIVED TABLE method to do further filtering to get only the results you want.
SQL SERVER AGGREGTE OVER CLAUSE
select distinct case_id, max(close_date) over (partition by case_id) as maxdate, min(open_date) over (partition by case_id) as mindate from wfm_subcase
Previously I had to use a derived table to get each of the two dates. Now it can be done in a single clean sql statement.
There is a catch if you uses sql server 2005. You will get error message if you use query designer to edit the query. Since the query designer mode is always the default mode when you create a view, just create a normal query without the over clause then save the view. Next, open the view in edit mode (not design mode) and change the query to a over clause query.
Find duplicate cases in SQL sever
select case_id, count(case_id) as casecnt from wfm_case group by case_id having (count(case_id) >1)
Derived Table
Select * from (select case_id,open_date from tbl_data) AS S
It looked stupid to have such a query which does not help in anything. However, its very much more useful than just that.
For example. You wish to get the last close date from a subcase while retrieving a main case data. This could only be achieved if you create a view to get the last close date or create a temporary table to get the last close date. Once you can get the data from the view or temp table, you join it to main case to get the final result.
Creating temp table means you have to do a number of steps to achieve it and have to remember to remove the temp table. Creating a view is easier as it will not need maintenance after that. However, you have to do one query in two places and you cannot create the query on the run. Is it at all possible to use just one query to get the desired results?
`Derived table` is the answer. The following is a working example from wfm (our server) database.
SELECT M.Case_id, S.close_date
FROM dbo.T_CASE AS M LEFT OUTER JOIN
(SELECT case_acc_id, MAX(close_date) AS close_date
FROM dbo.T_SUBCASE
GROUP BY case_acc_id) AS S ON M.Case_id = S.case_acc_id