Showing posts with label Programming. Show all posts
Showing posts with label Programming. Show all posts

7/24/2020

PHP on Visual Studio Code

Ubuntu 20.04

Install XDebug
https://xdebug.org/docs/install

PHP Debug Adapter for Visual Studio Code
https://marketplace.visualstudio.com/items?itemName=felixfbecker.php-debug

$ cd /etc/php/7.4/mods-available

$ gedit xdebug.ini

zend_extension=xdebug.so
xdebug.remote_enable=1
xdebug.remote_autostart=1

$ phpenmod xdebug

$ sudo service apache2 start

In Visual Studio Code, launch.json

{
"version": "0.2.0",
"configurations": [
{
"name": "Listen for XDebug",
"type": "php",
"request": "launch",
"port": 9000
},
{
"name": "Launch currently open script",
"type": "php",
"request": "launch",
"program": "${file}",
"cwd": "${fileDirname}",
"port": 9000,
"runtimeExecutable": "/usr/bin/php"
}
]
}

8/08/2014

MS SQL Cursor Template

DECLARE x_cursor CURSOR FOR

-- add select here
SELECT name
FROM MASTER.dbo.sysdatabases
WHERE name NOT IN ('master','model','msdb','tempdb')

OPEN x_cursor
FETCH NEXT FROM x_cursor INTO @name

WHILE @@FETCH_STATUS = 0
BEGIN
       -- do something here

       FETCH NEXT FROM x_cursor INTO @name
END

CLOSE x_cursor
DEALLOCATE x_cursor

2/14/2014

IE animation GIF freezed.

Original Link: http://weblog.west-wind.com/posts/2004/Dec/27/Animated-GIF-images-in-hidden-page-elements

我們常會在 Process 時, popup 一個會動的 icon, 表示在進行中. 這的動作常用 animation gif 來達成.

在 IE 7 以後,常常只顯示出圖片, 但是不會動.  這是 IE 已知的 Bug, 很久都沒有 Fix. 底下這段 Code 是從 Internet 上找到的, 可以 Work.

function ShowWaitDisplay()
{
   document.getElementById('MainForm').style.display='none';
   document.getElementById('WaitNote').style.display='inline';
   Img = document.getElementById('WaitImage');
   Img.style.display="inline";
   Img.src = "images/pleasewait.gif";
   window.scrollTo(0,0);
   return true;     
}

7/10/2013

Get size of all tables in MSSQL 2005 database

Original Doc: http://stackoverflow.com/questions/7892334/get-size-of-all-tables-in-database

SELECT 
    t.NAME AS TableName,
    p.rows AS RowCounts,
    SUM(a.total_pages) * 8 AS TotalSpaceKB, 
    SUM(a.used_pages) * 8 AS UsedSpaceKB, 
    (SUM(a.total_pages) - SUM(a.used_pages)) * 8 AS UnusedSpaceKB
FROM 
    sys.tables t
INNER JOIN      
    sys.indexes i ON t.OBJECT_ID = i.object_id
INNER JOIN 
    sys.partitions p ON i.object_id = p.OBJECT_ID AND i.index_id = p.index_id
INNER JOIN 
    sys.allocation_units a ON p.partition_id = a.container_id
WHERE 
    t.NAME NOT LIKE 'dt%' 
    AND t.is_ms_shipped = 0
    AND i.OBJECT_ID > 255 
GROUP BY 
    t.Name, p.Rows
ORDER BY 
    t.Name

5/15/2013

Similar Text

在 Programming 的時候, 有很多地方我們需要用到字串相似度的檢驗, 在 PHP 裡有現成的 Function 可以使用, 但是在其他地方就未必.

這裡有一個 JavaScript 的 Implementation. Click here!

在 MS SQL 上, 我根據這個 JavaScript 改了一個版本, 勉強可以使用


CREATE FUNCTION [dbo].[similar_text]
(
    @first   NVARCHAR(100),
    @second  NVARCHAR(100),
    @percent BIT = 1
)
RETURNS DECIMAL(10,3)
AS
BEGIN
    DECLARE @ret DECIMAL(10,3);
  
    DECLARE @s1 NVARCHAR(100);
    DECLARE @s2 NVARCHAR(100);
  
    DECLARE @pos1 INT;
    DECLARE @pos2 INT;
    DECLARE @max  INT;
    DECLARE @fl1  INT;
    DECLARE @fl2  INT;
    DECLARE @p    INT;
    DECLARE @q    INT;
    DECLARE @l    INT;
    DECLARE @sum  INT;

    SET @s1 = ISNULL(@first,'');
    SET @s2 = ISNULL(@second,'');
  
    IF (@s1='') OR (@s2='') BEGIN
        SET @ret = 0.00;
    END
    ELSE BEGIN
        SET @pos1 = 1;
        SET @pos2 = 1;
        SET @max  = 1;  
        SET @fl1  = len(@s1);
        SET @fl2  = len(@s2);
        SET @p    = 1;
      
        WHILE (@p<=@fl1) BEGIN
            SET @q = 1;
            WHILE (@q<=@fl2) BEGIN
                SET @l = 1;
                WHILE (@p+@l<=@fl1) AND (@q+@l<=@fl2) AND (SUBSTRING( @s1,@p+@l,1)=SUBSTRING( @s2,@q+@l,1)) BEGIN
                    IF (@l>=@max) BEGIN
                        SET @max = @l;
                        SET @pos1 = @p;
                        SET @pos2 = @q;
                    END;
                    SET @l = @l+1;
                END;
                SET @q = @q+1;
            END;
            SET @p = @p+1;
        END;
      
        SET @sum = @max;
        IF (@sum>1) BEGIN
            IF (@pos1>=1) AND (@pos2>=1) BEGIN
                SET @sum = @sum + dbo.similar_text(SUBSTRING(@s1,1,@pos2),SUBSTRING(@s2,1,@pos2),0);
            END;
          
            IF (@pos1+@max<=@fl1) AND (@pos2+@max<=@fl2) BEGIN
                SET @sum = @sum + dbo.similar_text(SUBSTRING(@s1,@pos1+@max,@fl1-@pos1-@max),SUBSTRING(@s2,@pos2+@max,@fl2-@pos2-@max),0);
            END;      
        END;
      
        IF (@percent=0)
            SET @ret = @sum;
        ELSE
            SET @ret = (@sum * 200.00) / (@fl1 + @fl2);
    END;
  
    RETURN @ret;
END


GO

3/08/2013

How to get IPV4 from IPV6 Environment?

Reference Link: http://www.4guysfromrolla.com/articles/071807-1.aspx  

using System;
using System.Net;

public class IPNetworking
{
  public static string GetIP4Address()
  {
    string IP4Address = String.Empty;

    foreach (IPAddress IPA in Dns.GetHostAddresses(HttpContext.Current.Request.UserHostAddress))
    {
      if (IPA.AddressFamily.ToString() == "InterNetwork")
      {
        IP4Address = IPA.ToString();
        break;
      }
    }

    if (IP4Address != String.Empty)
    {
      return IP4Address;
    }

    foreach (IPAddress IPA in Dns.GetHostAddresses(Dns.GetHostName()))
    {
      if (IPA.AddressFamily.ToString() == "InterNetwork")
      {
        IP4Address = IPA.ToString();
        break;
      }
    }

    return IP4Address;
  }
}

8/20/2012

Hostgator Session Timeout 太快的問題

最近在 Hostgator 上碰到個問題, User Login 進去後, 很快就 Session timeout, 被踢出來, 不明其原因. 又加上是 Shared Web Hosting, 根本沒有辦法去改到 IIS 的設定.

有一說是因為 Application Pool 不定時回收, Default 設定是五分鐘, 當 Application Pool 回收時, 會自動把所有的 Session Reset, 所以就 Timeout 掉了.

問題來了, 我又不能去改 IIS 設定, 那該如何處理? 

後來找到一篇文章, 上面提到的方式, 可以解決我的問題 ==> 就是它

他的 Solution 就是去 Web.config 裡面加上一組 machinekey, 就解決了.

這裡有個 machinekey generator, 用它產生 Machine Key 然後貼到 Web.config 中的 <System.web> Section 中, 在 Hostgator 被 Timeout 掉的問題就解決了!


8/15/2012

C# : How to Identify a Nullable Type

在 .Net Coding 中, 我常會用 class destructor 把一些 Resource Release 掉.

因為 .Net 本身有 Garbage Collection 的功能, 所以只要把已經 Allocate Resources 的 Object 指定為 Null 或是 Empty, 原來 Allocate 的 Resources 就變成無主孤兒,  將會被 Garbage Collection 回收.

問題來了, 不是每個 Type 都是 Nullable, 要如何判斷 Nullable, MS 在 MSDN 中提供了方法, 裡面還有些 Trick.

請參考: http://msdn.microsoft.com/en-us/library/ms366789.aspx

Use the following code to determine whether a Type object represents a Nullable type. Remember that this code always returns false if the Type object was returned from a call to GetType, as explained earlier in this topic.
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>)) {…}

4/29/2012

xdebug 在 Eclipse 不 work 的狀況

長久以來, 自從 Eclipse 換到 3.5 以後, xdebug 一直不能在 PHP 中使用.

今天偶然發現, 把一個設定加在 /etc/php5/conf.d/xdebug.ini 中即可.


zend_extension=/usr/lib/php5/20090626/xdebug.so
zend_debugger.allow_hosts=127.0.0.1, 192.168.9.0/255
zend_debugger.expose_remotely=always
xdebug.remote_enable=1 
;xdebug.remote_host="127.0.0.1"
;xdebug.remote_port=12345
;xdebug.remote_handler="dbgp"
;xdebug.remote_mode="req"
;xdebug.remote_connect_back=1

上面是從 Internet 找人家的檔案來改, 一個個 try 過之後, 發現只要 xdebug.remmote_enable=1, 就可以使用了.

4/02/2012

ASP.Net Menu Control work in Chrome

當使用 ASP.net Menu 元件, 在 Chrome 中不 work 時, 要在程式中加入下列這段 Code.


protected void Page_PreInit(object sender, EventArgs e) 
{
             // This is necessary because Safari and Chrome browsers don't display the Menu 

                control correctly.
             // All webpages displaying an ASP.NET menu control must inherit this class.
             if (Request.ServerVariables["

http_user_agent"].IndexOf("Safari", StringComparison.CurrentCultureIgnoreCase) != -1)
                 Page.ClientTarget = "uplevel"; 
}

9/24/2011

IE 中 Transparent 元件 Hover 問題解決方法

話說 IE 這個問題多多的東西, 整死我們這些辛苦寫 Code 的 Engineer.

最近碰到一個狀況, 只是很簡單的一個功能, 就是我開一個看不見的 DIV, 放在背景底圖上的某個位置, 當 Mouse 移過去時, 就會自動顯示出外框.

想法如下
 
    .divmap
    {
        .........
        border: none;
        background-color: transparent;
    }
    .divmap:hover
    {
        border: dashed 2px red;
    }

    <div id="logo" class="divmap"></div> 

就這樣簡單的 CSS Code, 在 IE 中, 就是不 work, 只有當 Mouse 移動到 DIV 邊緣時, 才會有動作.  理想中, 應該是移動到 DIV 的範圍內, 外框就該變色了.
經過網路爬文之後, 得知, IE 遇到所有的 Transparent 設定時, 就有這種狀況出現, 真是....

最後用古老的方式來解決, 就是弄一個 1x1 的 transparent image, 去填滿 div 的背景, 這樣就可以順利運作.
     .divmap
    {
        border: none;
        background: url(images/1x1.gif) repeat;
    }
    .divmap:hover
    {
        border: dashed 2px red;
    }
同樣都是 Browser, 就是 IE 問題多多, 唉~