2010-06-11 63 views
1

有誰知道如何在ASP.NET(VB.NET)中編寫以下PHP?將PHP腳本轉換爲ASP.NET

foreach ($_GET['listItem'] as $position => $item) : 
    $sql[] = "UPDATE `table` SET `position` = $position WHERE `id` = $item"; 
endforeach; 

在$ _GET的數據[ '的listItem']看起來像這樣:

項[] = 1 &項[] = 2 &項[] = 3

+0

您能否解釋意圖,也許我可以幫助更多,因爲我對PHP一無所知。謝謝。 – 2010-06-11 12:22:58

回答

1

我知道一點PHP,但我假設$ position是一個從零開始的指數。如果不是,那麼你會交換聲明位置變量的兩行代碼。評論從'角色開始。

'Request.Params accesses parameters from form posts, query string, etc. 
Dim pairs = Request.Params("listItem").Split("&") 

For i As Integer = 0 To pairs.Length - 1 

    Dim token = pairs(i) 
    Dim position = i.ToString() 
    'string position = (i + 1).ToString(); 
    Dim id = Convert.ToInt32(token.Split("=")(1)) 
    Dim sql = "UPDATE table SET position = " + position + " WHERE [id] = " + id.ToString() 
    'This line does the same thing, a bit more eloquently and efficiently 
    'Dim sql = String.Format("UPDATE table SET position = {0} WHERE [id] = {1}", position, id.ToString()) 

Next 

我在C#這樣做首先是因爲我沒有看到你說的VB.net。所以這裏是C#版本。以爲我不妨留下它。 我做了這個有點羅嗦,以演示一些C#的細微差別和清晰。

// Request.Params accesses parameters from form posts, query string, etc. 
string[] pairs = Request.Params["listItem"].Split('&'); 
for (int i = 0; i < pairs.Length; i++) 
{ 
    string token = pairs[i]; 
    string position = i.ToString(); 
    // string position = (i + 1).ToString(); 
    int id = Convert.ToInt32(token.Split('=')[1]); 
    string sql = "UPDATE table SET position = " + position + " WHERE [id] = " + id; 
    // This line does the same thing, a bit more eloquently and efficiently 
    //string sql = String.Format("UPDATE table SET position = {0} WHERE [id] = {1}", position, id.ToString()); 
}