2010-06-11 69 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()); 
}