代码之家  ›  专栏  ›  技术社区  ›  oshirowanen

将PHP脚本转换为ASP.NET

  •  1
  • oshirowanen  · 技术社区  · 15 年前

    有人知道如何在ASP.NET(VB.NET)中编写以下PHP吗?

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

    $\u get['listem']中的数据如下:

    项目[]=1&项目[]=2&项目[]=3

    1 回复  |  直到 15 年前
        1
  •  1
  •   Sean    15 年前

    我知道一点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());
    }